def quantify(quantgfx, labelgfx, table, nFrame, originalTitle): results = ResultsTable() clij2.statisticsOfBackgroundAndLabelledPixels(gfx4, gfx5, results) for i in range(results.size()): table.incrementCounter() table.addValue("Frame (Time)", nFrame) table.addValue("Label", i) table.addValue("MEAN_INTENSITY", results.getValue("MEAN_INTENSITY", i)) table.addValue("SUM_INTENSITY", results.getValue("SUM_INTENSITY", i)) table.addValue("MINIMUM_INTENSITY", results.getValue("MINIMUM_INTENSITY", i)) table.addValue("MAXIMUM_INTENSITY", results.getValue("MAXIMUM_INTENSITY", i)) table.addValue("STANDARD_DEVIATION_INTENSITY", results.getValue("STANDARD_DEVIATION_INTENSITY", i)) table.addValue("PIXEL_COUNT", results.getValue("PIXEL_COUNT", i)) table.addValue("CENTROID_X", results.getValue("CENTROID_X", i)) table.addValue("CENTROID_Y", results.getValue("CENTROID_Y", i)) table.addValue("CENTROID_Z", results.getValue("CENTROID_Z", i)) table.addValue("File name", originalTitle) return table
else: rm.reset() #IJ.run(imp,"Analyze Particles...", "size=112-Infinity exclude add") rt=ResultsTable() pa=ParticleAnalyzer(EXCLUDE_EDGE_PARTICLES | ADD_TO_MANAGER, 0, rt, 112/4, 200, 0.0, 1.0) pa.analyze(imp) # time.sleep(2) print 'Size of results: ',rt.size() # rm.runCommand("select","all") # rm.runCommand("Fill","3") save_path=saving_dir+"\\mask_%s" % (image[image.rindex('\\')+1:]) # print(save_path) impMask = IJ.createImage("Mask", "8-bit grayscale-mode", imp.getWidth(), imp.getHeight(), imp.getNChannels(), imp.getNSlices(), imp.getNFrames()) impMask.show() IJ.setForegroundColor(255, 255, 255) rm.runCommand(impMask,"Deselect") rm.runCommand(impMask,"Draw") if doFileSave and FileSaver(impMask).saveAsTiff(save_path): print 'Saved Image ',image else:
def process(self,imp): # extract nucleus channel, 8-bit and twice binned imp.setC(self.nucleusChannel) ip = imp.getChannelProcessor().duplicate() ip = ip.convertToByteProcessor() ip = ip.bin(4) nucleus = ImagePlus("nucleus_channel", ip) # threshold image and separate clumped nuclei IJ.run(nucleus, "Auto Threshold", "method=Otsu white setthreshold show"); IJ.run(nucleus, "Make Binary", "thresholded remaining black"); IJ.run(nucleus, "Watershed", ""); directory = imp.getTitle() directory = directory.replace(" ", "_")\ .replace(",", "_")\ .replace("#", "_series")\ .replace("...", "")\ .replace(".","_") directory = os.path.join(self.exportDir, directory) sliceDirectory = os.path.join(directory, "slices") print directory print sliceDirectory if not os.path.exists(sliceDirectory): os.makedirs(sliceDirectory) # Create a table to store the results table = ResultsTable() # Create a hidden ROI manager, to store a ROI for each blob or cell #roim = RoiManager(True) # remove small particles and border particles pa = ParticleAnalyzer(\ ParticleAnalyzer.ADD_TO_MANAGER | ParticleAnalyzer.EXCLUDE_EDGE_PARTICLES,\ Measurements.CENTER_OF_MASS,\ table,\ self.minArea, self.maxArea,\ 0.0,1.0) if pa.analyze(nucleus): print "All ok, number of particles: ", table.size() else: print "There was a problem in analyzing", imp, nucleus table.save(os.path.join(directory, "rt.csv")) # read the center of mass coordinates cmx = table.getColumn(0) cmy = table.getColumn(1) if self.debug: imp.show() i=0 for i in range(0, min(self.nCells,table.size())): # ROI around the cell cmx = table.getValue("XM",i) cmy = table.getValue("YM",i) x = 4 * cmx - (self.boxSize - 1) / 2 y = 4 * cmy - (self.boxSize - 1) / 2 if (x < self.edge or y < self.edge or x > imp.getWidth() - self.edge or y > imp.getHeight() - self.edge): continue roi = Roi(x,y,self.boxSize,self.boxSize) imp.setRoi(roi, False) cellStack = ImageStack(self.boxSize, self.boxSize) for z in range(1, imp.getNSlices() + 1): imp.setSlice(z) for c in range(1, imp.getNChannels() + 1): imp.setC(c) # copy ROI to stack imp.copy() impSlice = imp.getClipboard() cellStack.addSlice(impSlice.getProcessor()) if self.slices: sliceTitle = "cell_%s_z%s_c%s" % (str(i).zfill(4), str(z).zfill(3), str(c)) print sliceTitle IJ.saveAsTiff(impSlice, os.path.join(sliceDirectory, sliceTitle)) impSlice.close() title = "cell_" + str(i).zfill(4) cell = ImagePlus(title, cellStack) # save ROI image IJ.saveAsTiff(cell, os.path.join(directory, title)) cell.close() if self.debug: imp.updateAndDraw() wait = Wait("particle done") wait.show()
### Compute area fraction of oriented regions ### # Compute area of oriented mask newMaskImp = ImageJFunctions.wrapUnsignedByte(newMask, "New mask"); newMaskImp.getProcessor().setThreshold(1.0, 1.5, False); # [0.5, 1.0] does not work due to rounding problems rt = ResultsTable(); analyzer = Analyzer(newMaskImp, Measurements.AREA | Measurements.LIMIT, rt); analyzer.measure(); # Compute area of overall selection mask energyMaskImp = ImageJFunctions.wrapUnsignedByte(overallSelectionMask, "Energy mask"); energyMaskImp.getProcessor().setThreshold(1.0, 1.5, False); # [0.5, 1.0] does not work due to rounding problems rtEnergy = ResultsTable(); analyzerEnergy = Analyzer(energyMaskImp, Measurements.AREA | Measurements.LIMIT, rtEnergy); analyzerEnergy.measure(); # Print Area% (through SciJava OUTPUT, see L5) if IJ.debugMode: print("Coherency area: "+str(rt.getValueAsDouble(rt.getColumnIndex("Area"), rt.size()-1))); print("Energy area: "+str(rtEnergy.getValueAsDouble(rtEnergy.getColumnIndex("Area"), rtEnergy.size()-1))); areaCoherency = rt.getValueAsDouble(rt.getColumnIndex("Area"), rt.size()-1); areaEnergy = rtEnergy.getValueAsDouble(rtEnergy.getColumnIndex("Area"), rtEnergy.size()-1); areaFraction = str(areaCoherency/areaEnergy*100.0)+"%"; # Close "S-Mask" if not IJ.debugMode: maskImp.close(); # Close "Energy" if not IJ.debugMode and useEnergy: energyImp.close();
# save the mCherry results table rt.save(directory + "/" + filename + "_mcherry.csv") # do the same foci counting procedure as for GFP rowcount = 1 #for count in range(cell): #consol.setValue("cell",count,count) #consol.setValue("foci_count",count,0) #for count in range(rt.size()): #currcell = int(rt.getValue("cell",count)) #print(currcell, "old value", consol.getValue("foci_count", currcell), "new value", int(consol.getValue("foci_count", currcell))+1) #consol.setValue("foci_count", currcell, int(consol.getValue("foci_count", currcell))+1) IJ.selectWindow("Results") IJ.run("Close") for count in range(consol.size()): inrange = consol.getValue("is_in_range", count) if (inrange == 1): count_range = count_range + 1 foci = consol.getValue("foci_count", count) if foci <= 3: cellsperfoci_range[foci] = cellsperfoci_range[foci] + 1 else: cellsperfoci_range[ "more"] = cellsperfoci_range["more"] + 1 # save the summary results table consol.save(directory + "/" + filename + "_summary.csv") # reset the ROI Manager, close it and go to next file (if there is one) rm.runCommand("Reset") rm.close()
flipIfNecessary(getFlipArray(maskPA), [img410PA, img470PA, maskPA]) img410PA.show() img470PA.show() # MORPHOLOGICAL MEASUREMENTS # These are done on the rotated image mask scales = { "4x4": 2.58, "2x2": 1.29 } morphTable = ResultsTable() PA.setResultsTable(morphTable) maskPA.show() IJ.run(maskPA, "Select None", "") IJ.run(maskPA, "Set Scale...", "distance=1 known=" + str(scales[binning]) + " pixel=1 unit=μm"); #TODO scale? IJ.run(maskPA, "Set Measurements...", "area centroid center perimeter bounding fit shape feret's median skewness kurtosis scientific redirect=None decimal=7"); IJ.run(maskPA, "Analyze Particles...", "size=0-Infinity circularity=0.00-1.00 show=Nothing stack"); morph_headings = morphTable.getColumnHeadings().strip(' ').split('\t') for m_heading in morph_headings[1:]: # because of formatting, the first is empty addValues(dataTable, m_heading, [morphTable.getValue(m_heading, i) for i in range(morphTable.size())]) for imPlus in [img410PA, img470PA, maskPA]: cropStack(imPlus) IJ.saveAsTiff(imPlus, os.path.join(PARENT_DIR, imPlus.getTitle())) dataTable.show("Measurements") dataTable.save(os.path.join(PARENT_DIR, 'measurements.csv'))
imp2 = IJ.openImage(sys.argv[2]) # stabilize stack = ImageStack(imp1.width, imp1.height) stack.addSlice('', imp1.getProcessor()) stack.addSlice('', imp2.getProcessor()) stackimp = ImagePlus('stack', stack) WindowManager.setTempCurrentImage(stackimp) IJ.run( stackimp, "Image Stabilizer", "transformation=Affine maximum_pyramid_levels=4 template_update_coefficient=0.90 maximum_iterations=20000 error_tolerance=0.0000001" ) stack = stackimp.getImageStack() stabilized1 = ImagePlus('stabilized1', stack.getProcessor(1)) stabilized2 = ImagePlus('stabilized2', stack.getProcessor(2)) # subtract ic = ImageCalculator() diff = ic.run('Subtract create', stabilized1, stabilized2) # smudge diff.getProcessor().blurGaussian(2) # enhance ;) hysteresis = ImagePlus('hysteresis', hyst(diff.getProcessor(), HYST_HI, HYST_LO)) # invert hysteresis.getProcessor().invert() # count worms rt = ResultsTable() pa = ParticleAnalyzer(SHOW_NONE, 0, rt, MIN_SIZE, MAX_SIZE, MIN_CIRC, MAX_CIRC) pa.analyze(hysteresis) sys.stdout.write('%d' % rt.size()) IJ.run('Quit')