diff --git a/2d_spots_in_fibers.py b/2d_spots_in_fibers.py index 510df1eebc384510f5935c8756d179ed4234c6a7..181b435b77a3ed2b8d20e560606ed9e70364c2ac 100644 --- a/2d_spots_in_fibers.py +++ b/2d_spots_in_fibers.py @@ -1,5 +1,6 @@ #@ OpService ops #@ CommandService command +#@ RoiManager rm #@ File (label="select image", description="select your input image", style=file) path_to_image #@ Integer (label="select image file series", description="leave 1 if not needed", value=1, min=1, max=20, stepSize=1, persist=false, style=slider) series_number #@ File (label="Fiber Segmentation", description="select the Fiber Segmentation RoiSet .zip file", style=file) fiber_segmentation_roiset @@ -19,6 +20,8 @@ from ij import IJ from ij.plugin import ImageCalculator from ij.plugin.frame import RoiManager from ij.measure import ResultsTable +from ij.measure import Measurements as M +from ij.plugin.filter import Analyzer # Bio-Formats imports from loci.plugins import BF from loci.common import Region @@ -332,37 +335,50 @@ def convert_labelimage_to_binary(label_imp, scale_binary=True): return binary_imp -# TODO: test simpler way using IJ roi measure -def measure_intensity_sum(label_imp, target_imp): - """Measure the sum intensity for each label in a label image on another target image. - Uses imglib2 and ops for this and works with both 2D and 3D label images. + +def measure_intensity_sum(imp, rm): + """ + Measure theraw integrated intensity for all rois in target imp. Parameters ---------- - label_imp : ImagePlus - the input label image - target_imp : ImagePlus - the target image in which to measure + imp : ImagePlus + The image from which the intensity will be measured. + rm : RoiManager + The ROI Manager containing the regions of interest to be analyzed. Returns ------- - array - label id and corresponding sum intensity [label_id, sum_intensity] + label_id : list of str + A list of labels corresponding to each ROI. + sum_intensity : list of int + A list of summed integrated intensities for each ROI. + + Notes + ----- + The results are stored in a `ij.ResultsTable`, + from which the raw integrated density values are extracted. + + Example + ------- + >>> labels, intensities = measure_intensity_sum(image, roi_manager) """ + + rt_ = ResultsTable() + options = M.INTEGRATED_DENSITY + an = Analyzer (imp, options, rt_) + an.setPrecision(0) + label_id = [] sum_intensity = [] - target_img = ImageJFunctions.wrap(target_imp) # convert ImagePlus to img to use ops/region measurements on it - regions = convert_labelimage_to_imglib2regions(label_imp) - - for region in regions: - label_id.append(region.getLabel() + 1) # region.getlabel() starts counting from 0. So the label with int 1 has the index 0. - input = Regions.sample(region, target_img) # returns an iterableInterval - sum = ops.stats().sum(input).getRealDouble() - sum_intensity.append(sum) - # other measurements see https://forum.image.sc/t/can-i-get-to-measurements-using-imagej-ops/4448/5 - result = [label_id, sum_intensity] + + for index, roi in enumerate(rm.getRoisAsArray()): + imp.setRoi(roi) + an.measure() + label_id.append(roi.getName()) + sum_intensity.append(int(rt_.getColumn("RawIntDen")[index])) - return result + return label_id, sum_intensity def add_results_to_resultstable(results_table, column, values):