def getFlipArray(maskPAImgPlus): """Given the PA-aligned mask image, return a list of booleans, indicating whether or not we should flip the corresponding index""" maskPAImgPlus.show() # IJ.run(maskPAImgPlus, "Shape Smoothing", "relative_proportion_fds=15 absolute_number_fds=2 keep=[Relative_proportion of FDs] stack"); IJ.run("Set Measurements...", "shape") IJ.setThreshold(maskPAImgPlus, 1, 1000, "No Update") stk = maskPAImgPlus.getStack() tableLeft = ResultsTable() PA.setResultsTable(tableLeft) IJ.makeRectangle(0, 0, maskPAImgPlus.getWidth() / 2 - 5, maskPAImgPlus.getHeight()) IJ.run(maskPAImgPlus, "Analyze Particles...", "stack") tableRight = ResultsTable() PA.setResultsTable(tableRight) IJ.makeRectangle(maskPAImgPlus.getWidth() / 2 - 5, 0, maskPAImgPlus.getWidth() / 2 + 5, maskPAImgPlus.getHeight()) IJ.run(maskPAImgPlus, "Analyze Particles...", "stack") maskPAImgPlus.hide() IJ.resetThreshold(maskPAImgPlus) leftCirc = [tableLeft.getValue("Circ.", i) for i in range(stk.getSize())] rightCirc = [tableRight.getValue("Circ.", i) for i in range(stk.getSize())] return [leftCirc[i] < rightCirc[i] for i in range(len(leftCirc))]
def test(): newImg = ImagePlus("GrayScaled", imp) newip = newImg.getProcessor() hist = newip.getHistogram() lowTH = Auto_Threshold.IsoData(hist) newip.setThreshold(lowTH, max(hist), ImageProcessor.BLACK_AND_WHITE_LUT) rt = ResultsTable() pa = ParticleAnalyzer(ParticleAnalyzer.SHOW_RESULTS | ParticleAnalyzer.SHOW_OVERLAY_OUTLINES, Measurements.AREA |Measurements.MEAN |\ Measurements.MEDIAN | Measurements.STD_DEV | Measurements.MIN_MAX | Measurements.RECT, rt,50, 200000, 0.5, 1 ) pa.setResultsTable(rt) pa.analyze(newImg) rt.show("Results")
def test(): newImg = ImagePlus("GrayScaled",imp) newip = newImg.getProcessor() hist = newip.getHistogram() lowTH = Auto_Threshold.IsoData(hist) newip.setThreshold(lowTH, max(hist),ImageProcessor.BLACK_AND_WHITE_LUT) rt = ResultsTable() pa = ParticleAnalyzer(ParticleAnalyzer.SHOW_RESULTS | ParticleAnalyzer.SHOW_OVERLAY_OUTLINES, Measurements.AREA |Measurements.MEAN |\ Measurements.MEDIAN | Measurements.STD_DEV | Measurements.MIN_MAX | Measurements.RECT, rt,50, 200000, 0.5, 1 ) pa.setResultsTable(rt) pa.analyze(newImg) rt.show("Results")
def cellSegmentation(srcDir, dstDir, currentDir, filename, keepDirectories): print "Processing:" # Opening the image print "Open image file", filename imp = IJ.openImage(os.path.join(currentDir, dstDir)) # Put your processing commands here! localinput=srcDir.replace("/", "\\") saveDir = localinput.replace(srcDir, dstDir) string="." dotIndex=filename.find(string) localfile= filename[0:dotIndex] print(localfile) IJ.run("New... ", "name="+f+" type=Table") print(f,"\\Headings:Cell\tarea\tCirc\tAR\tRoundness\tMaximum") IJ.run("Bio-Formats", "open=[" + localinput + os.path.sep + filename +"] autoscale color_mode=Default rois_import=[ROI manager] view=Hyperstack stack_order=XYCZT") IJ.open() idd= WM.getIDList(); imageID= idd[0]; IJ.run("Clear Results") WM.getImage(imageID) IJ.run("Duplicate...", "duplicate channels="+str(x)+"") #Nucleus channel #took away x IJ.run("Z Project...", "projection=[Standard Deviation]");#picture for frame detection IJ.run("8-bit"); IJ.run("Duplicate...", "title=IMAGE");#frame IJ.run("Duplicate...", "title=SUBTRACT");#Background subtraction mask (for frame and watershed) imp=IJ.getImage() pixelWidth=imp.getWidth() pixelWidth=pixelWidth/1647.89 pixelHeight= imp.getHeight() #create subtraction mask, applying constraining maximum (step I) IJ.selectWindow("SUBTRACT") nResults=imp.getStatistics() row = nResults rt_exist = WM.getWindow("Results") if rt_exist==None: rt= ResultsTable() else: rt = rt_exist.getTextPanel().getOrCreateResultsTable() rt.setValue("Max ", 0, row.max) #text file rt.show("Results") u=math.floor(row.mean*3) IJ.run("Max...","value="+str(u)) #constraining maximum of 3-fold mean to reduce effect of extreme values during subtraction #gaussian blurring (step II) IJ.run("Gaussian Blur...", "sigma=100 scaled") #blurring for subtraction mask IJ.selectWindow("IMAGE") pxrollrad = cellradius/pixelWidth; #rolling ball radius in pixels needed (= predefined cell radius[µm]/pixelsize[µm/px]) IJ.run("Subtract Background...", "rolling="+str(pxrollrad)+"") IJ.run("Gaussian Blur...", "sigma=2 scaled") #reduces punctate character of grayscale image ' IM=IJ.selectWindow("IMAGE") SUB=IJ.selectWindow("SUBTRACT") ic().run("SUBTRACT", IM, SUB) #just subtracts two images IJ.selectWindow("IMAGE") #see how to call IJ.run("Duplicate...", "title=AND")#watershed IJ.run("Duplicate...", "title=CHECK")#for checking if maxima exist within selection later #Apply threshold to get binary image of cell borders (step IV) IJ.selectWindow("IMAGE") imp = IJ.getImage() # the current image imp.getProcessor().setThreshold(1, 255, ImageProcessor.NO_LUT_UPDATE) IJ.run("Subtract Background...","...") IJ.run("Convert to Mask", "method=Default background=Dark only black") IJ.run("Fill Holes") #Create watershed line image (step V) IJ.selectWindow("AND") IJ.run("Gaussian Blur...", "sigma=2 scaled") imp=IJ.getImage() pixelWidth=imp.getWidth() pixelWidth=pixelWidth/1647.89 pixelHeight= imp.getHeight() # Saving the image nResults=imp.getStatistics() row = nResults rt.setValue("Max ", 1, row.max) #text file nBins = 256 Hist = HistogramWindow("Histogram",imp,nBins) Table = Hist.getResultsTable() Counts = Table.getColumn(1) #mean gray value of pixels belonging to cells needed (i.e. mean of ONLY non-zero pixel) Sum = 0 #all counts CV = 0 #weighed counts (= counts * intensity) for i in range(0, len(Counts)): #starting with 1 instead of 0. -> 0 intensity values are not considered. Sum += Counts[i] CV += Counts[i]*i m = (CV/Sum) m=math.floor(m) l = math.floor(2*m) #Maxima need to be at least twice the intensity of cellular mean intensity IJ.run("Find Maxima...", "noise="+str(l)+" output=[Segmented Particles] exclude") #watershedding #Combine watershed lines and cell frame (step VI) IJ.selectWindow("IMAGE") imp=IJ.getImage() imp.getProcessor().setThreshold(1, 255, ImageProcessor.NO_LUT_UPDATE) IJ.run(imp, "Watershed", "") #useful imp = IJ.getImage() ip = imp.getProcessor() segip = MaximumFinder().findMaxima( ip, 1, ImageProcessor.NO_THRESHOLD, MaximumFinder.SEGMENTED , False, False) segip.invert() segimp = ImagePlus("seg", segip) segimp.show() mergeimp = RGBStackMerge.mergeChannels(array([segimp, None, None, imp, None, None, None], ImagePlus), True) mergeimp.show() pa_exist = WM.getWindow("Results for PA") if pa_exist==None: pa_rt= ResultsTable() else: pa_rt = pa_exist.getTextPanel().getOrCreateResultsTable() ParticleAnalyzer.setResultsTable(pa_rt) IJ.run("Set Measurements...", "area mean perimeter shape decimal=3") IJ.run("Analyze Particles...", "size=" + str(cellradius) + "-Infinity circularity=0.1-1.00 add"); #Cell bodies detected pa_rt.show("Results for PA ") save_all(srcDir, dstDir, filename, localfile, keepDirectories, imageID)
rotateStack(img470PA, angles, xs, ys, IP.BILINEAR) rotateStack(maskPA, angles, xs, ys, IP.NONE) 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()))