distMin = dist return centPart, (x, y) def isSpecialParticle(image, roim, iParticle, boundariesNuc, boundariesMito, pixelSize, z): try: image.setSlice(int(round(z))) image2 = ImagePlus("slice",image.getProcessor()) roim.select(image2, iParticle) image3 = Duplicator().run(image2); image3.show() sp = segment_Particles() image3 = sp.preprocessImage(image3, "MAX", "C1", 0, 0) image3 = sp.segmentParticles(image3, "Triangle", boundariesNuc[0], boundariesNuc[1], 0, 0) if roim.getCount() > 0: roim.select(image3, 0) stats = image3.getStatistics(Measurements.AREA + Measurements.SHAPE_DESCRIPTORS) IJ.log("Area (um2) " + str(stats.area*pixelSize*pixelSize) + "; Aspect Ratio " + str(stats.major/stats.minor)) if stats.area > boundariesMito[0] and stats.area < boundariesMito[1] and stats.major/stats.minor > boundariesMito[2]: return 1 else: return 0 except BaseException, err:
def run(): zk = ZeissKeys() msg = Message() timestart = time.clock() channel = 1 #size in um2 minArea = 20 maxArea = 200 maxMetaArea = 70 minMetaAR = 1.8 method = "Triangle" print "Enter run" detectionNucleus = [minArea, maxArea] detectionMetaphase = [minArea, maxMetaArea] filepath = "" try: filepath = getArgument() image = IJ.openImage(filepath) #arguments provided by runMacro except NameError: try: filepath = newImagePath #argument provided by the macroRunner except NameError: #no file name specified errMsg = "Fiji error segmentNuclei.py: no filepath has been passed to the macro" exitWithError(errMsg) od = OpenDialog("Choose image file", None) if od.getFileName() == None: return filepath = os.path.join(od.getDirectory(), od.getFileName()) # A roimanager is recquired roim = RoiManager.getInstance() if roim == None: roim = RoiManager() #test a last time if roim == None: print 'Fiji error segmentNuclei.py: no RoiManager!' exitWithError('Fiji error segmentNuclei.py: no RoiManager!') try: IJ.run("Close all forced") IJ.run("Clear Results") IJ.log("\\Clear") image = IJ.openImage(filepath) IJ.log("File Path " + filepath) try: image.hide() except: image = IJ.getImage() image.hide() image.show() if image is None: exitWithError("Fiji failed to open image") return 0 #convert um2 to pixels pixelSize = image.getCalibration().pixelWidth detectionNucleus = [area/pixelSize/pixelSize for area in detectionNucleus] detectionMetaphase = [area/pixelSize/pixelSize for area in detectionMetaphase] detectionMetaphase.append(minMetaAR) title = image.getTitle() imageC = None #split colors if needed if image.getNChannels() > 1: chSp = ChannelSplitter() imageC = chSp.split(image) imageSeg = imageC[channel-1].duplicate() else: imageSeg = image.duplicate() imageSeg.setTitle("SEG_"+title) sp = segment_Particles() imageSeg = sp.preprocessImage(imageSeg, "MAX", "C1", 2, 2) imageSeg = sp.segmentParticles(imageSeg, method, detectionNucleus[0], detectionNucleus[1], 2, 2) roim = RoiManager.getInstance() if roim == None: print 'Fiji error segmentNuclei.py: no RoiManager!' exitWithError('Fiji error segmentNuclei.py: no RoiManager!') #compute central object if roim.getCount() > 0: centPart, msg.position = centerParticle(imageSeg, roim) msg.position = msg.position + (focusParticle(image, roim, centPart),) writePositionToRegistry(msg.position) else: IJ.run("Read Write Windows Registry", "action=write location=[HKCU\\"+zk.regkey+"] key="+zk.subkey_codemic+" value=["+msg.CODE_NOTHING+"] windows=REG_SZ") return 0 #create output image if imageC is None: impOut = createOutputImg(image, roim, centPart, 0) else: impOut = createOutputImg(imageC[channel-1], roim, centPart, 0) impOut.show() #save outut image #saveOutputImg(impOut, filepath, 'Analyzed') #check for roi properties to identofy metaphase metaphase = isSpecialParticle(image, roim, centPart,detectionNucleus, detectionMetaphase,pixelSize, msg.position[2]) if os.path.basename(filepath).startswith('TR1_') or '_TR1_' in os.path.basename(filepath): metaphase = 0 if metaphase: IJ.run("Read Write Windows Registry", "action=write location=[HKCU\\"+zk.regkey+"] key="+zk.subkey_codemic+" value=["+msg.CODE_TRIGGER1+"] windows=REG_SZ") else: IJ.run("Read Write Windows Registry", "action=write location=[HKCU\\"+zk.regkey+"] key="+zk.subkey_codemic+" value=["+msg.CODE_FOCUS+"] windows=REG_SZ") IJ.log("time focus " + str(time.clock() - timestart)) IJ.run("Collect Garbage") #IJ.run("Close all forced") #IJ.run("Clear Results") except BaseException, err: