def get_roi_manager(new=False): rm = RoiManager.getInstance() if not rm: rm = RoiManager() if new: rm.runCommand("Reset") return rm # Identify particles img = mask.getImgPlus() labeled_img = ij.op().run("cca", img, StructuringElement.EIGHT_CONNECTED) # Create label regions from particles regions = LabelRegions(labeled_img) region_labels = list(regions.getExistingLabels()) print("%i regions/particles detected" % len(region_labels)) # Now use IJ1 RoiManager to display the detected regions rm = get_roi_manager(new=True) for label in region_labels: region = regions.getLabelRegion(label) # Get the center of mass of the region center = region.getCenterOfMass() x = center.getDoublePosition(0) y = center.getDoublePosition(1)
from net.imglib2.algorithm.labeling.ConnectedComponents import StructuringElement from net.imglib2.roi import Regions from net.imglib2.roi.labeling import LabelRegions # create a log kernel logKernel = ops.create().kernelLog(inputData.numDimensions(), sigma) logFiltered = ops.filter().convolve(inputData, logKernel) # otsu threshold and display thresholded = ops.threshold().otsu(logFiltered) # call connected components to label each connected region labeling = ops.labeling().cca(thresholded, StructuringElement.FOUR_CONNECTED) # get the index image (each object will have a unique gray level) labelingIndex = labeling.getIndexImg() # get the collection of regions and loop through them regions = LabelRegions(labeling) for region in regions: # get the size of the region size = region.size() # get the intensity by "sampling" the intensity of the input image at the region pixels intensity = ops.stats().mean(Regions.sample(region, inputData)).getRealDouble() print "size", size, "intensity", intensity
smooth_shape = CenteredRectangleShape([1, 1], False) ops.run("filter.mean", smoothed, mask, smooth_shape) elif smoothing == "Mean 5x5": smooth_shape = CenteredRectangleShape([2, 2], False) ops.run("filter.mean", smoothed, mask, smooth_shape) # Threshold if required if thresholding == "None": binary = ops.run("threshold.mean", mask) else: binary = ops.run("threshold.%s" % thresholding, smoothed) # Create LabelRegions img_labeling = ops.run("labeling.cca", binary, StructuringElement.EIGHT_CONNECTED) regions = list(LabelRegions(img_labeling)) # Process each LabelRegion for region in regions: # Get a sample view sample = Regions.sample(region, image) # Compute the stats area = ops.run("stats.size", sample) min = ops.run("stats.min", sample) max = ops.run("stats.max", sample) mean = ops.run("stats.mean", sample) median = ops.run("stats.median", sample) stdev = ops.run("stats.stdDev", sample) sum = ops.run("stats.sum", sample)
if debugging: print green_mask ui.show('GREEN_MASK',green_mask) # Analyze particles (minPSize-maxPSize) (default: 4-80) # Create new object mask log('Creating cleaned mask and counting....') green_mask_new = ops.create().img(green_mask) from jarray import array pp = array([0,0],'l') nPunctae=0 green_mask_labeling = ops.labeling().cca(green_mask,StructuringElement.FOUR_CONNECTED) if debugging: log('Green max is : {}'.format(ops.stats().max(green_mask))) ui.show('GREEN_MASK_LABEL',green_mask_labeling.getIndexImg()) for r in LabelRegions(green_mask_labeling): print r.size() if r.size()>=minPSize and r.size()<=maxPSize: nPunctae=nPunctae+1 rc = r.localizingCursor() gc = green_mask_new.randomAccess() while rc.hasNext(): rc.fwd() rc.localize(pp) gc.setPosition(pp) gc.get().set(1) log('Done! Found {} punctae within range'.format(nPunctae)) if debugging: ui.show('RED_MASK',red_mask)
outputMask = False allowSplit = True # H-watershed returns a label map as an ImagePlus labelmap = ops.run("H_Watershed", imp, hMin, thresh, peakFlooding, outputMask, allowSplit) labelmap.show() print "labelmap: ", type(labelmap) # is 32bits # convert to 16-bits as ImgLabeling expects Int not not Float IJ.run("Conversions...", " ") IJ.run(labelmap, "16-bit", "") # fist convert ImagePlus to img as suggested by curtis: # from https://forum.image.sc/t/how-to-wrap-any-kind-of-imageplus-to-an-imglib2-img-floattype/178/6 wrapImg = ImageJFunctions.wrap(labelmap) print "wrapImg: ", type(wrapImg) # then convert img to ImgLabeling (net.imglib2.roi.labeling.ImgLabeling) labeling = ImgLabeling(wrapImg) print "labeling: ", type(labeling) # get regions from ImgLabeling regions = LabelRegions(labeling) print "regions: ", type(regions) print(dir(regions)) # note crash here even though getExistingLabels() is defined... existing_labels = regions.getExistingLabels() print "existingLabels: ", type(existing_labels)