def __add(self, event): if ( not self.__init) : IJ.showMessage("", "please start a new stack") return if ( not self.__initDIA) : IJ.showMessage("", "please select an image for DIA") return if ( not self.__initFLUO) : IJ.showMessage("", "please select an image for FLUO") return self.__widthl = self.__display2.getText() roi = self.__impD.getRoi() if roi == None : IJ.showMessage("", "No selection") return if roi.getType() in [6,7] : nslice = self.__impD.getCurrentSlice() self.__impF.setSlice(nslice) self.__impF.setRoi(roi) elif roi.getType() in [2,4] : nslice = self.__impD.getCurrentSlice() self.__impF.setSlice(nslice) m=Morph(self.__impF, roi) m.setMidParams(10, 2) roi=m.MidAxis if roi == None : self.__display.text = "roi fail" if not self.__skip : IJ.showMessage("", "failed roi, please draw it as polyline") return #if roi.getType() != 6 : self.__impF.setRoi(roi) else : IJ.showMessage("", "This selection is not yet allowed") return self.__impF.setRoi(roi) straightener = Straightener() new_ip = straightener.straighten(self.__impF, roi, int(self.__widthl)) self.__iplist.append(new_ip) self.__labels.append(self.__isF.getShortSliceLabel(nslice)) self.__display.text = self.__name + " cell " + str(len(self.__iplist)) +" width="+str(new_ip.getWidth())+ " height="+ str(new_ip.getHeight()) roi.setPosition(self.__impD.getCurrentSlice()) self.__rm = RoiManager.getInstance() if (self.__rm==None): self.__rm = RoiManager() self.__rm.add(self.__impD, roi, len(self.__iplist)) self.__cellsrois.append((roi, self.__impD.getCurrentSlice())) #self.__rm.runCommand("Show All") IJ.selectWindow(self.__impD.getTitle())
def getIncludeRois(self): self.__yieldRois=[] self.__yieldRois[:]=[] for roi in self.__roisArray: m=Morph(self.__image, roi) state=0 for r in self.__ranges.values(): if r[1]<=m.__getattribute__(r[0])<=r[2]: state=state+0 else: state=state+1 if state<1 : self.__yieldRois.append(roi) return self.__yieldRois
def __measureAll(self,img,measure,changeslice, name, noise) : """ Measures a characteristic for all the cells in a stack. """ dico=self.__dictCells[name] if changeslice == True : img.setSlice(1) for cellname in dico.keys() : roitemp=dico[cellname].getRoi(0) self.__dictMeasures[dico[cellname]][measure]=[] if isinstance(roitemp,Roi) : m=Morph(img,roitemp) if noise is not None : m.setNoise(noise) self.__dictMeasures[dico[cellname]][measure].append(m.__getattribute__(measure)) #self.__dictMeasures[dico[cellname]][measure].append(Morph(img,roitemp).__getattribute__(measure)) else : self.__dictMeasures[dico[cellname]][measure].append(Double.NaN) for i in range(2,img.getImageStackSize()+1) : if changeslice == True : img.setSlice(i) for cellname in dico.keys() : roitemp=dico[cellname].getRoi(i-1) if isinstance(roitemp,Roi) : m=Morph(img,roitemp) if noise is not None : m.setNoise(noise) self.__dictMeasures[dico[cellname]][measure].append(m.__getattribute__(measure)) #self.__dictMeasures[dico[cellname]][measure].append(Morph(img,roitemp).__getattribute__(measure)) else : self.__dictMeasures[dico[cellname]][measure].append(Double.NaN)
def buttonPressed(event): temprois=self.getIncludeRois() for roi in temprois: m=Morph(self.__image, roi) IJ.log("----------------------------------") IJ.log(roi.getName()) for r in self.__ranges.values(): IJ.log(r[0]+" min= "+str(r[1])+" < val="+str(m.__getattribute__(r[0]))+" < max= "+str(r[2])) IJ.run(self.__image, "Remove Overlay", "") o=Overlay() for roi in temprois: o.addElement(roi) self.__image.killRoi() self.__image.setOverlay(o) self.__image.updateAndDraw()
def __measureVelocity(self, img, name) : measure = "velocity" dico=self.__dictCells[name] for cellname in dico.keys() : roitemp=dico[cellname].getRoi(0) xc = self.__dictMeasures[dico[cellname]]["XC"] yc = self.__dictMeasures[dico[cellname]]["YC"] d=[] for i in range(len(xc)-1) : tempseq = [ xc[i], xc[i+1], yc[i], yc[i+1] ] if not any(Double.isNaN(val)==True for val in tempseq ) : d.append(Morph.distMorph([[1, xc[i], xc[i+1]],[1, yc[i], yc[i+1]]])) else : d.append(Double.NaN) d.append(Double.NaN) self.__dictMeasures[dico[cellname]][measure]=d del(d)
def __measurecumulDist(self, img, name) : measure = "cumulatedDist" dico=self.__dictCells[name] for cellname in dico.keys() : roitemp=dico[cellname].getRoi(0) xc = self.__dictMeasures[dico[cellname]]["XC"] yc = self.__dictMeasures[dico[cellname]]["YC"] d=[] d.append(0) for i in range(1,len(xc)-1) : tempseq = [ xc[i], xc[i+1], yc[i], yc[i+1] ] if not any(Double.isNaN(val)==True for val in tempseq ) : lastdist = Morph.distMorph([[1, xc[i], xc[i+1]],[1, yc[i], yc[i+1]]]) d.append(d[-1]+lastdist) else : d.append(Double.NaN) d.append(Double.NaN) self.__dictMeasures[dico[cellname]][measure]=d del(d)
ci = len(xpoints)//2 centres[i]=(xpoints[ci], ypoints[ci]) pos.append(centres[i]) # chgt repere pour centroide x0=pointsA[i-1][0]-centres[i-1][0] y0=pointsA[i-1][1]-centres[i-1][1] x1=xpoints[0]-centres[i][0] y1=ypoints[0]-centres[i][1] x2=xpoints[-1]-centres[i][0] y2=ypoints[-1]-centres[i][1] d1=Morph.distMorph([(1,x0,x1),(1, y0, y1)]) # dist from old A to new extem1 d2=Morph.distMorph([(1,x0,x2),(1, y0, y2)])# dist from old A to new extem2 if d1<=d2 : pointsA[i]=(xpoints[0], ypoints[0]) # new A is the closest from old A pointsB[i]=(xpoints[-1], ypoints[-1]) pointsC[i]=(xpoints[1], ypoints[1]) pointsD[i]=(xpoints[-2], ypoints[-2]) else : pointsB[i]=(xpoints[0], ypoints[0]) pointsA[i]=(xpoints[-1], ypoints[-1]) # new A is the closest from old A pointsC[i]=(xpoints[-2], ypoints[-2]) pointsD[i]=(xpoints[1], ypoints[1]) # ------- coordonees image ------------ xa0=pointsA[i-1][0]
def __addroi(self, event) : if ( not self.__init) : IJ.showMessage("", "please start a new stack") return if ( not self.__initDIA) : IJ.showMessage("", "please select an image for DIA") return if ( not self.__initFLUO) : IJ.showMessage("", "please select an image for FLUO") return twres = TextWindow("measures-"+self.__name, "label\tname\tsol\tarea\tcirc\tAR\tFeret\taxis\traf\tdMajor\tdFeret\tdArea", "", 300, 450) tab="\t" self.__widthl = self.__display2.getText() IJ.selectWindow(self.__impF.getTitle()) self.__rm = RoiManager.getInstance() if (self.__rm==None): self.__rm = RoiManager() if self.__impF.getImageStackSize() > 1 : roisarray =[(roi, self.__rm.getSliceNumber(roi.getName())) for roi in self.__rm.getRoisAsArray()] else : roisarray =[(roi, 1) for roi in self.__rm.getRoisAsArray()] self.__rm.runCommand("reset") #self.__rm.runCommand("Delete") IJ.selectWindow(self.__impF.getTitle()) self.__maxraf=float(self.__display19.text) self.__minraf=float(self.__display20.text) count=1 for roielement in roisarray : roi = roielement[0] pos = roielement[1] lab = self.__impF.getImageStack().getShortSliceLabel(pos) if lab==None : lab=str(pos) if self.__conEllipses : IJ.selectWindow(self.__impF.getTitle()) self.__impF.setSlice(pos) self.__impF.setRoi(roi) self.__rm.runCommand("Add") IJ.run(self.__impF, "Fit Ellipse", "") ellipse=self.__impF.getRoi() params = ellipse.getParams() ferets = ellipse.getFeretValues() imp2 = Duplicator().run(self.__impF,pos,pos) IJ.run(imp2, "Rotate... ", "angle="+str(ferets[1])+" grid=0 interpolation=Bilinear enlarge slice") temproi=Roi((imp2.getWidth()-ferets[0])/2.0,(imp2.getHeight()-ferets[2])/2.0,ferets[0],ferets[2]) imp2.setRoi(temproi) imp3 = Duplicator().run(imp2,1,1) ip3=imp3.getProcessor() if int(self.__display5.text) < ip3.getWidth() < int(self.__display6.text) : self.__iplist.append(ip3) self.__display.text = self.__name + " cell " + str(len(self.__iplist)) fer=Line(params[0],params[1],params[2],params[3]) self.__cellsrois.append((fer, pos)) self.__labels.append(self.__isF.getShortSliceLabel(pos)) m=Morph(self.__impF, roi) twres.append(lab+tab+str(roi.getName())+tab+str(m.Solidity)+tab+str(m.Area)+tab+str(m.Circ)+tab+str(m.AR)+tab+str(m.MaxFeret)+tab+str(fer.getLength())+tab+str(1)+tab+str(0)+tab+str(0)+tab+str(0)) self.__dictCells[count]=(str(roi.getName()), lab, roi) count=count+1 continue if roi.getType() in [6,7] : self.__impF.setSlice(pos) self.__impF.setRoi(roi) self.__rm.runCommand("Add") elif roi.getType() in [2,4] : self.__impF.setSlice(pos) self.__impF.setRoi(roi) m=Morph(self.__impF, roi) m.setMidParams(10, 2) midroi=m.MidAxis if midroi == None : continue raf = m.MaxFeret/midroi.getLength() if (self.__maxraf < raf) or (raf < self.__minraf) : continue maxsol = float(self.__display7.text) minsol = float(self.__display8.text) maxarea = float(self.__display9.text) minarea = float(self.__display10.text) maxcirc = float(self.__display11.text) mincirc = float(self.__display12.text) maxar = float(self.__display13.text) minar = float(self.__display14.text) maxfer = float(self.__display15.text) minfer = float(self.__display16.text) maxmean = float(self.__display17.text) minmean = float(self.__display18.text) maxmferet = float(self.__display21.text) minmferet = float(self.__display22.text) testsol = (minsol< m.Solidity < maxsol) testarea = (minarea< m.Area < maxarea) testcirc = (mincirc< m.Circ < maxcirc) testar = (minar< m.AR < maxar) testfer = (minfer< m.MaxFeret < maxfer) testmean = (minmean < m.Mean < maxmean) testmferet = (minmferet < m.MinFeret < maxmferet) #print minmferet , m.MinFeret , maxmferet test = (testsol+testarea+testcirc+testar+testfer+testmean+testmferet)/7 if test : fmaj, ffmx, fa =[],[],[] for r in m.getMidSegments(10, 40, 0) : if r == None : continue m2=Morph(self.__impF, r) fmaj.append(m2.Major) ffmx.append(m2.MaxFeret) fa.append(m2.Area) diffmajor, diffferet, diffarea = 0,0,0 if len(fa) > 4 : medfmaj = self.listmean(fmaj[1:-1]) medffmx = self.listmean(ffmx[1:-1]) medfa = self.listmean(fa[1:-1]) diffmajor = (max(fmaj[1:-1])-medfmaj)/medfmaj diffferet = (max(ffmx[1:-1])-medffmx)/medffmx diffarea = (max(fa[1:-1])-medfa)/medfa twres.append(lab+tab+str(roi.getName())+tab+str(m.Solidity)+tab+str(m.Area)+tab+str(m.Circ)+tab+str(m.AR)+tab+str(m.MaxFeret)+tab+str(midroi.getLength())+tab+str(m.MaxFeret/midroi.getLength())+tab+str(diffmajor)+tab+str(diffferet)+tab+str(diffarea)) #print lab+tab+str(roi.getName())+tab+str(m.Solidity)+tab+str(m.Area)+tab+str(m.Circ)+tab+str(m.AR)+tab+str(m.MaxFeret)+tab+str(midroi.getLength())+tab+str(m.MaxFeret/midroi.getLength())+tab+str(diffmajor)+tab+str(diffferet)+tab+str(diffarea) self.__impF.setRoi(roi) self.__rm.runCommand("Add") self.__impF.killRoi() self.__impF.setRoi(midroi) #self.__dictCells[str(roi.getName())]=(str(roi.getName()), lab, roi) self.__dictCells[count]=(str(roi.getName()), lab, roi) count=count+1 else : #print "test falls" continue else : print "out loop" continue straightener = Straightener() new_ip = straightener.straighten(self.__impF, midroi, int(self.__widthl)) if int(self.__display5.text) < new_ip.getWidth() < int(self.__display6.text) : self.__iplist.append(new_ip) self.__display.text = self.__name + " cell " + str(len(self.__iplist)) #print "add", roi.getName(), roi.getType() self.__cellsrois.append((midroi, pos)) self.__labels.append(self.__isF.getShortSliceLabel(pos)) #roisarray=self.__rm.getRoisAsArray() #self.__rm.runCommand("reset") #self.__rm.runCommand("Delete") self.__impD.killRoi() self.__impF.killRoi() IJ.selectWindow(self.__impD.getTitle())
def link(img, sliceA, sliceB, RoisA, RoisB, coeffs, distmethod, optionangle, nbdigits, boolnews) : """ This function takes in entry an image, two slices, two arrays of ROIs, a list of coefficients, and a string corresponding to the name of a method. It aims at linking the different ROIs of an image to the ROIs of another image, according to the method and to the coefficients given in entry. It returns 3 lists of tuples : - one in which the tuples correspond to two ROIs linked. - another one in which the tuples correspond to new bacteria - a last one in which the tuples correspond to "lost" bacteria. """ rawcoeffs=[coeffs[i] for i in range(0,len(coeffs), 2)] #maxdists=[0.01*coeffs[i] for i in range(1,len(coeffs), 2)] maxdists=[coeffs[i] for i in range(1,len(coeffs), 2)] # Definition of the different parameters used in the distance. measure=[] measure.append("Area") measure.append("Angle") measure.append("MaxFeret") measure.append("XC") measure.append("YC") #Delata max normalization: coeffdist=[rawcoeffs[i]/(maxdists[i]*sum(rawcoeffs)) for i in range(5)] # Calcul of the constant in order to flag the cells that are out of range (according to the preferences of the user) #maxdisteuclarray=[] #maxdisteuclarray.append(math.sqrt(coeffdist[0]/sum(coeffdist)*(maxdists[0]*img.getWidth()*img.getHeight())**2)) #maxdisteuclarray.append(math.sqrt(coeffdist[1]/sum(coeffdist)*(maxdists[1]*180)**2)) #maxdisteuclarray.append(math.sqrt(coeffdist[2]/sum(coeffdist)*(maxdists[2]*math.sqrt((img.getWidth())**2+(img.getHeight())**2))**2)) #maxdisteuclarray.append(math.sqrt(coeffdist[3]/sum(coeffdist)*(maxdists[3]*img.getWidth())**2)) #maxdisteuclarray.append(math.sqrt(coeffdist[4]/sum(coeffdist)*(maxdists[4]*img.getHeight())**2)) #maxdisteucl=max(maxdisteuclarray) maxdisteuclarray=[math.sqrt(coeffdist[i]*(maxdists[i]**2)) for i in range(5)] #MAXDIST=max(maxdisteuclarray) MAXDIST=math.sqrt(max(maxdists)**2) #if distmethod=="Logarithm distance" : # for i in range(len(maxdists)) : # if maxdists[i]==1 : # maxdists[i]-=10**(-nbdigits) # MAXDIST=max([abs(math.log(1-maxdists[i])) for i in range(5)]) #if distmethod=="Euclidean distance" : # maxdisteuclarray=[] # maxdisteuclarray.append(math.sqrt(coeffdist[0]/sum(coeffdist)*(maxdists[0]*img.getWidth()*img.getHeight())**2)) # maxdisteuclarray.append(math.sqrt(coeffdist[1]/sum(coeffdist)*(maxdists[1]*180)**2)) # maxdisteuclarray.append(math.sqrt(coeffdist[2]/sum(coeffdist)*(maxdists[2]*math.sqrt((img.getWidth())**2+(img.getHeight())**2))**2)) # maxdisteuclarray.append(math.sqrt(coeffdist[3]/sum(coeffdist)*(maxdists[3]*img.getWidth())**2)) # maxdisteuclarray.append(math.sqrt(coeffdist[4]/sum(coeffdist)*(maxdists[4]*img.getHeight())**2)) # MAXDIST=max(maxdisteuclarray) #img.hide() # Creation of the "matrix" of the different distances between all couples of RoisA and RoisB possible. # The "matrix" is a dictionnary with tuples of ROIs (ROISA,ROISB) as key, and tuples (distance,(ROIA,ROIB)) as values listedist={} for roisA in RoisA : morphA=Morph(img, roisA) for roisB in RoisB : morphB=Morph(img, roisB) #if distmethod=="Logarithm distance" : # val=[ [ coeffdist[i]/sum(coeffdist), morphA.__getattribute__(measure[i]), morphB.__getattribute__(measure[i]) ] for i in range(3) ] # val.append([coeffdist[3]/sum(coeffdist),img.getWidth() ,max(morphA.__getattribute__(measure[3]),morphB.__getattribute__(measure[3]))-min(morphA.__getattribute__(measure[3]),morphB.__getattribute__(measure[3]))]) # val.append([coeffdist[4]/sum(coeffdist),img.getHeight() ,max(morphA.__getattribute__(measure[4]),morphB.__getattribute__(measure[4]))-min(morphA.__getattribute__(measure[4]),morphB.__getattribute__(measure[4]))]) #else : val=[ [ coeffdist[i]/sum(coeffdist), morphA.__getattribute__(measure[i]), morphB.__getattribute__(measure[i]) ] for i in range(5) ] rawval=[ [ 1, morphA.__getattribute__(measure[i]), morphB.__getattribute__(measure[i]) ] for i in range(5) ] val=[ [ coeffdist[i], morphA.__getattribute__(measure[i]), morphB.__getattribute__(measure[i]) ] for i in range(5) ] #print rawval #print "----" #for v in val : print math.sqrt(v[0]*(v[2]-v[1])*(v[2]-v[1])) # We make sure there is no null value out="" for i in range(5): if val[i][1]==0 : val[i][1]+=10**(-nbdigits) if val[i][2]==0 : val[i][2]+=10**(-nbdigits) # if abs(rawval[i][2]-rawval[i][1])>maxdists[i]: out = "out of range" #print "area", rawval[0][2], "/", rawval[0][1],">", maxdists[0],"=", abs(rawval[0][2]/rawval[0][1])>maxdists[0] #print "feret", rawval[2][2], "/", rawval[2][1],">", maxdists[2],"=", abs(rawval[2][2]/rawval[2][1])>maxdists[2] if abs(rawval[1][2]-rawval[1][1])>maxdists[1]: out = "out of range" if abs(rawval[3][2]-rawval[3][1])>maxdists[3]: out = "out of range" if abs(rawval[4][2]-rawval[4][1])>maxdists[4]: out = "out of range" if abs(rawval[0][2]/rawval[0][1])>maxdists[0]: out = "out of range" if abs(rawval[2][2]/rawval[2][1])>maxdists[2]: out = "out of range" if optionangle==True : if val[1][1]>=178 : val[1][1]=180-val[1][1] if val[1][2]>=178 : val[1][2]=180-val[1][2] # We check if the value is in the interval defined by the user. #out="" #for i in range(3): # if 1-(min(val[i][1], val[i][2])/max(val[i][1], val[i][2])) > maxdists[i] : # out="out of range" # print "out of range i=",i,"car : delta=",1-(min(val[i][1], val[i][2])/max(val[i][1], val[i][2]))," et maxdists[i] :",maxdists[i] #if distmethod=="Euclidean distance" : # deltax=(max(val[3][1],val[3][2])-min(val[3][1],val[3][2]))/img.getWidth() # deltay=(max(val[4][1],val[4][2])-min(val[4][1],val[4][2]))/img.getHeight() # if deltax > maxdists[3] or deltay > maxdists[4] : # out="out of range" # print "out of range 3-4 car : delta=",deltax," ",deltay," et maxdists3-4 :",maxdists[3]," ",maxdists[4] #if distmethod=="Logarithm distance" : # if val[3][2]/val[3][1] > maxdists[3] or val[4][2]/val[4][1] > maxdists[4] : # out = "out of range" # print "out of range 3-4 car : rapport=",val[3][2]/val[3][1]," ",val[4][2]/val[4][1]," et maxdists3-4 :",maxdists[3]," ",maxdists[4] if out == "" : listedist[(roisA,roisB)]=(Morph.distMorph(val,distmethod),(roisA,roisB)) else : listedist[(roisA,roisB)]=(MAXDIST,(roisA,roisB)) # The mininum of all distances is found in the matrix, and if it is < to a certain value, then the couple of ROIs linked is added in a list "liens". # Then all couples having one of the two ROIs considered just before are deleted from the "matrix". # And while the length of "liens" is not equal to the minimum of the length of RoisA and RoisB, or the mininum is inferior to the value "seuil", we repeat the algorithm. # The list of tuples "liens" is returned by the function. #print listedist liens = [] for i in range(min([len(RoisA),len(RoisB)])) : mindist=min(listedist.values()) #print "(",mindist,";",i,";",min([len(RoisA),len(RoisB)]),")" if mindist[0]>=MAXDIST : break else : minroisA=mindist[1][0] minroisB=mindist[1][1] liens.append((minroisA,minroisB)) for cle in listedist.keys() : if cle[0]==minroisA or cle[1]==minroisB : del listedist[cle] # If any ROI of RoisA doesn't appear in the list of tuples "liens", then the ROI is considered to be "LOST"; and the list of tuples of ROIs lost is returned by the function. lost=[] for roisA in RoisA: marqueurtemp = False for lien in liens : if lien[0] == roisA : marqueurtemp = True break if marqueurtemp == False : lost.append(("LOST",roisA)) # If any ROI of RoisB doesn't appear in the list of tuples "liens", then the ROI is considered to be "NEW"; and the list of tuples of new ROIs is returned by the function. new=[] if boolnews : for roisB in RoisB : marqueurtemp = False for lien in liens : if lien[1] == roisB : marqueurtemp = True break if marqueurtemp == False : new.append(("NEW",roisB)) # The function "link" returns a tuples of all three lists of tuples : "liens", "new", and "lost" return (liens,new,lost)