Example #1
0
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 run_straighten(roiWindowsize=4):
    """ Original straightening function based on Nick's macro. Used in final version.
    Returns coordinate string used to make centerline. """
    IJ.run("Set Measurements...", "mean min center redirect=None decimal=3")
    IJ.runMacro("//setTool(\"freeline\");")
    IJ.run("Line Width...", "line=80")
    numPoints = 512 / roiWindowsize
    xvals = []
    yvals = []
    maxvals = []
    counter = 0

    for i in range(0, 512, roiWindowsize):
        IJ.run("Clear Results")
        IJ.makeRectangle(i, 0, roiWindowsize, 512)
        IJ.run("Measure")
        table = RT.getResultsTable()
        xvals.append(i + roiWindowsize / 2)
        yvals.append(RT.getValue(table, "YM", 0))
        maxvals.append((RT.getValue(table, "Max", 0)))

        if maxvals[counter] == 0 and counter > 0:
            yvals[counter] = yvals[counter - 1]

        counter += 1

    coords = ""
    for i in range(numPoints - 1):
        coords += str(xvals[i]) + ", " + str(yvals[i]) + ", "
    coords += str(xvals[numPoints - 1]) + ", " + str(yvals[numPoints - 1])

    IJ.runMacro("makeLine(" + coords + ")")
    IJ.run("Straighten...", "line = 80")
    return coords
def run_straighten(roiWindowsize = 4):
    """ Original straightening function based on Nick's macro. Used in final version.
    Returns coordinate string used to make centerline. """
    IJ.run("Set Measurements...", "mean min center redirect=None decimal=3")
    IJ.runMacro("//setTool(\"freeline\");")
    IJ.run("Line Width...", "line=80");
    numPoints = 512/roiWindowsize
    xvals = []
    yvals = []
    maxvals = []
    counter = 0

    for i in range(0, 512, roiWindowsize):
        IJ.run("Clear Results")
        IJ.makeRectangle(i, 0, roiWindowsize, 512)
        IJ.run("Measure")
        table = RT.getResultsTable()
        xvals.append(i + roiWindowsize/2)
        yvals.append(RT.getValue(table, "YM", 0))
        maxvals.append((RT.getValue(table, "Max", 0)))

        if maxvals[counter] == 0 and counter > 0:
            yvals[counter] = yvals[counter - 1]

        counter += 1

    coords = ""
    for i in range(numPoints - 1):
        coords += str(xvals[i]) + ", " + str(yvals[i]) +", "
    coords += str(xvals[numPoints-1]) + ", " + str(yvals[numPoints-1])

    IJ.runMacro("makeLine("+coords+")")
    IJ.run("Straighten...", "line = 80")
    return coords
def straighten_roi_rotation(roiWindowsize = 8):
    """ Root straightening function that rotates ROI to follow root slope.
    Does not work properly.
    """
    IJ.run("Set Measurements...", "mean standard min center redirect=None decimal=3")
    IJ.runMacro("//setTool(\"freeline\");")
    IJ.run("Line Width...", "line=80");
    #numPoints = 512/roiWindowsize
    xvals = []
    yvals = []
    maxvals = []
    counter = 0
    maxIters = 800/roiWindowsize
    minIters = 10

    imp = IJ.getImage().getProcessor()

    rm = RoiManager()
    if find_first_pixel(0,imp) == None or find_last_pixel(0,imp)[1] == None:
        return
    y = (find_first_pixel(0,imp)[1]+find_last_pixel(0,imp)[1])/2
    roi = roiWindow_(imp, center = (roiWindowsize/2,y), width = roiWindowsize, height = 512)
    xvals.append(roiWindowsize/2)
    yvals.append(y)
    maxvals.append(0)
    roi.findTilt_()
    i = 0
    while i < maxIters and roi.containsRoot_():
    	roi.advance_(roiWindowsize)
        IJ.run("Clear Results")
        IJ.run("Measure")
        table = RT.getResultsTable()

        x  = RT.getValue(table, "XM", 0)
        y = RT.getValue(table, "YM", 0)
        if imp.getPixel(int(x),int(y)) != 0:
            xvals.append(x)
            yvals.append(y)
            maxvals.append((RT.getValue(table, "Max", 0)))


        #roi.advance_(roiWindowsize)
        print "here"
        roi.unrotateRoot_()
        IJ.run("Clear Results")
        IJ.run("Measure")
        roi.restoreCenter_(RT.getValue(table, "XM", 0), RT.getValue(table, "YM", 0))
        #exit(1)
        sleep(.5)
        roi.findTilt_()
        i += 1
    coords = ""
    for i in range(len(xvals)-1):
        coords += str(xvals[i]) + ", " + str(yvals[i]) +", "
    coords += str(xvals[len(xvals)-1]) + ", " + str(yvals[len(xvals)-1])

    IJ.runMacro("makeLine("+coords+")")
    IJ.run("Straighten...", "line = 80")
 def containsRoot_(self):
     IJ.run("Clear Results")
     IJ.run("Measure")
     table = RT.getResultsTable()
     if RT.getValue(table, "Max", 0) == 0:
         return False
     return True
def measure_growth(imgDir, filename = "Fiji_Growth.txt"):
    """ Collects measurement data in pixels and writes to a file. Uses straightened binary images"""
    f = open(imgDir + filename, 'w')
    f.write("Img number\tEnd point (pixels)\n")
    IJ.run("Set Measurements...", "area mean min center redirect=None decimal=3")
    index = "000000000"
    filename = imgDir + "/binary" + "/img_" + index + "__000-padded.tif"
    while path.exists(filename):
		imp = IJ.openImage(filename)
		imp.show()
		IJ.run("Clear Results")
		for i in xrange(800): #hard coded to target length for now
			IJ.makeRectangle(i, 0, 1, 80)
			IJ.run("Measure")
			table = RT.getResultsTable()
			#print "i:", i, "counter:", table.getCounter()
			maxi = RT.getValue(table, "Max", i)
			if maxi == 0:
				f.write(str(int(index)) + "\t" + str(i) + "\n")
				break

		IJ.runMacro("while (nImages>0) {selectImage(nImages);close();}")
		index = to_9_Digits(str(int(index)+1))
		filename = imgDir + "/padded" + "/img_" + index + "__000-padded.tif"
    f.close()
 def containsRoot_(self):
     IJ.run("Clear Results")
     IJ.run("Measure")
     table = RT.getResultsTable()
     if RT.getValue(table, "Max", 0) == 0:
         return False
     return True
def measure_growth(imgDir, filename="Fiji_Growth.txt"):
    """ Collects measurement data in pixels and writes to a file. Uses straightened binary images"""
    f = open(imgDir + filename, 'w')
    f.write("Img number\tEnd point (pixels)\n")
    IJ.run("Set Measurements...",
           "area mean min center redirect=None decimal=3")
    index = "000000000"
    filename = imgDir + "/binary" + "/img_" + index + "__000-padded.tif"
    while path.exists(filename):
        imp = IJ.openImage(filename)
        imp.show()
        IJ.run("Clear Results")
        for i in xrange(800):  #hard coded to target length for now
            IJ.makeRectangle(i, 0, 1, 80)
            IJ.run("Measure")
            table = RT.getResultsTable()
            #print "i:", i, "counter:", table.getCounter()
            maxi = RT.getValue(table, "Max", i)
            if maxi == 0:
                f.write(str(int(index)) + "\t" + str(i) + "\n")
                break

        IJ.runMacro("while (nImages>0) {selectImage(nImages);close();}")
        index = to_9_Digits(str(int(index) + 1))
        filename = imgDir + "/padded" + "/img_" + index + "__000-padded.tif"
    f.close()
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
Example #10
0
    if ".ome" in dL:
        #open the images as virtual stack
        imp = IJ.run(
            "Bio-Formats", "open=[" + dL +
            "] color_mode=Default rois_import=[ROI manager] view=Hyperstack stack_order=XYCZT use_virtual_stack"
        )
        imp = IJ.getImage()
        #get right name for duplication of the desired frame
        if n < 10:
            numstr = "000" + str(n)
        elif n < 100:
            numstr = "00" + str(n)
        elif n > 99:
            numstr = "0" + str(n)
        if choice == 1:
            choose = RT.getValue(metaRT, "Frame selected", n - 1)
        #duplicate the right frame and give it the right title
        impDup = IJ.run(
            "Duplicate...", "title=Image_R" + str(numstr) +
            " duplicate frames=" + str(choose) + "")
        imp.close()
        # add 1 to the counter
        n = n + 1

#get list imageplus objects of all open images
impl = [WM.getImage(id) for id in WM.getIDList()]
end = len(impl)

imp2 = impl[0]
ti = 0
#concatenate all the frames to a time lapse
Example #11
0
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'))
Example #12
0
def fretCalculations(imp1, nFrame, donorChannel, acceptorChannel,
                     acceptorChannel2, table, gfx1, gfx2, gfx3, gfx4, gfx5,
                     originalTitle):
    donorImp = extractChannel(imp1, donorChannel, nFrame)
    acceptorImp = extractChannel(imp1, acceptorChannel, nFrame)
    acceptorImp2 = extractChannel(imp1, acceptorChannel2, nFrame)

    #push donor and acceptor channels to gpu and threshold them both to remove saturated pixels

    gfx4 = clij2.push(donorImp)
    gfx5 = clij2.push(acceptorImp)
    gfx6 = clij2.create(gfx5)

    clij2.threshold(gfx4, gfx2, maxIntensity)
    clij2.binarySubtract(gfx3, gfx2, gfx6)

    clij2.threshold(gfx5, gfx2, maxIntensity)
    clij2.binarySubtract(gfx6, gfx2, gfx3)

    clij2.threshold(gfx3, gfx6, 0.5)
    clij2.multiplyImages(gfx6, gfx4, gfx2)
    clij2.multiplyImages(gfx6, gfx5, gfx4)

    gfx6 = clij2.push(acceptorImp2)

    #donor is gfx2, acceptor FRET is gfx4, segment channel (acceptor normal) is gfx6

    results = ResultsTable()
    clij2.statisticsOfBackgroundAndLabelledPixels(gfx2, gfx1, results)

    donorChIntensity = results.getColumn(13)
    results2 = ResultsTable()
    clij2.statisticsOfBackgroundAndLabelledPixels(gfx4, gfx1, results2)
    acceptorChIntensity = results2.getColumn(13)

    results3 = ResultsTable()
    clij2.statisticsOfBackgroundAndLabelledPixels(gfx6, gfx1, results3)

    #calculate the fret ratios, removing any ROI with intensity of zero
    FRET = []

    for i in xrange(len(acceptorChIntensity)):
        if (acceptorChIntensity[i] > 0) and (donorChIntensity[i] > 0):
            #don't write in the zeros to the results
            FRET.append((1000 * acceptorChIntensity[i] / donorChIntensity[i]))

            table.incrementCounter()
            table.addValue("Frame (Time)", nFrame)
            table.addValue("Label", i)
            table.addValue("Emission ratio",
                           acceptorChIntensity[i] / donorChIntensity[i])

            table.addValue("Mean donor emission",
                           results.getValue("MEAN_INTENSITY", i))
            table.addValue("Mean acceptor emission (FRET)",
                           results2.getValue("MEAN_INTENSITY", i))
            table.addValue("Mean acceptor emission",
                           results3.getValue("MEAN_INTENSITY", i))

            table.addValue("Sum donor emission", donorChIntensity[i])
            table.addValue("Sum acceptor emission (FRET)",
                           acceptorChIntensity[i])
            table.addValue("Sum acceptor emission",
                           results3.getValue("SUM_INTENSITY", i))

            table.addValue(
                "Volume",
                cal.pixelWidth * cal.pixelHeight * cal.pixelDepth *
                results.getValue("PIXEL_COUNT", i))
            table.addValue("Pixel count", results.getValue("PIXEL_COUNT", i))
            table.addValue("x",
                           cal.pixelWidth * results.getValue("CENTROID_X", i))
            table.addValue("y",
                           cal.pixelHeight * results.getValue("CENTROID_Y", i))
            table.addValue("z",
                           cal.pixelDepth * results.getValue("CENTROID_Z", i))
            table.addValue("File name", originalTitle)
        else:
            #must write in the zeros as this array is used to generate the map of emission ratios
            FRET.append(0)

    table.show("Results of " + originalTitle)

    FRET[0] = 0
    FRETarray = array("f", FRET)
    fp = FloatProcessor(len(FRET), 1, FRETarray, None)
    FRETImp = ImagePlus("FRETImp", fp)
    gfx4 = clij2.push(FRETImp)
    clij2.replaceIntensities(gfx1, gfx4, gfx5)
    maxProj = clij2.create(gfx5.getWidth(), gfx5.getHeight(), 1)
    clij2.maximumZProjection(gfx5, maxProj)

    #pull the images

    FRETimp2 = clij2.pull(gfx5)
    FRETProjImp = clij2.pull(maxProj)
    labelImp = clij2.pull(gfx1)

    clij2.clear()
    donorImp.close()
    acceptorImp.close()
    acceptorImp2.close()

    return table, FRETimp2, FRETProjImp, labelImp
Example #13
0
    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()
Example #14
0
				# Check if Column even exists (in case it didn't measure anything)

				if table.getColumnIndex("Area") != -1:

						# Find the ROI with the largest area

					for i, area in enumerate(table.getColumn(table.getColumnIndex("Area"))):
						if area > maxArea:
							index = i

				

				# Writes everything in the output file
				if index != -1:

					diameter = 2* math.sqrt( float(table.getValue("Area", index)) / (2* math.pi)) 
					isOrganoid = table.getValue("Area", index) > area_threshold and table.getValue("Area", index) > round_threshold
					output.write(str(subfolder) + ',' + filename + ',' + str(table.getValue("Feret", index)) + ',' + str(table.getValue("MinFeret", index)) + ',' + str((table.getValue("MinFeret", index)+table.getValue("Feret", index))/2) + ','  + str(table.getValue("Area", index)) + ',' + str(diameter) + ',' + str(table.getValue("Major", index)) + ','+ str(table.getValue("Minor", index)) + ','+ str(table.getValue("Circ.", index)) + ',' +str(table.getValue("Round", index)) + ',' + str(table.getValue("Solidity", index)) + ','  + str(isOrganoid))
				else:
					output.write(str(subfolder) + ',' + filename + ",NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA")

				output.write('\n')
				imp.changes = False
				imp.close()

				roim.reset()
				roim.close()
				
# End of macro

cat = """
Example #15
0
class Morph(object):
	"""
		Fourni les mesures principales pour l'analyse des cellules bacteriennes:
		proprietes:
		1-MaxFeret
		2-MinFeret
		3-AngleFeret
		4-XFeret
		5-YFeret
		6-Area
		7-Mean
		8-StdDev
		9-IntDen
		10-Kurt
		11-Skew
		12-Angle
		13-Major
		14-Minor
		15-Solidity
		16-AR
		17-Round
		18-Circ.
		19-XM
		20-YM
		21-X
		22-Y
		23-FerCoord: tuple contenant x1, y1, x2, y2 du MaxFeret
		24-Fprofil: list contenant les valeurs du profil le long de MaxFeret
		25-FerAxis: Line ROI
		26-MidAxis: Polyline ROI de l'axe median par skeletonize
		27-MidProfil: list contenant les valeurs du profil le long de MidAxis
		28-nb Foci
		29-ListFoci: liste des positions des foci par cellule
		30-ListAreaFoci: liste des area des foci
		31-ListPeaksFoci: liste des int max des foci
		32-ListMeanFoci liste des int mean des foci
		
		toute les proprietes mettent a jour l'image cible par: object.propriete=imp
		
		Methodes:
		getFeretSegments(n segments)
		getMidSegments(n segments, radius, tool 0= ligne perpendiculaire, 1= cercle, 2= ligne tangente)
		selectInitRoi: active la ROI initiale
		
		Statics:
		distMorph(liste de coordonees a mesurer= (coefficient, valeur initiale, valeur finale))
		
		setteurs:
		setImage(ImagePlus)
		setImageMeasures(imagePlus) met a jours les mesures avec imagePlus
		setImageMidprofil(imagePlus) met a jours le profil avec imagePlus
		setLineWidth(width) afecte la largeur de ligne pour le profil pour Fprofile et MidProfil defaut = 0
		setshowFprof(True) affiche le graphique de profil Fprofil defaut = False
		setMidParams(longueur mesurer l'angle de l'extremite en pixels defaut=10, coefficient pour prolonger et trouver l'intersection avec le contour defaut=1.3
		
	"""

	def __Measures(self):

		self.__boolmeasures=True
		if (self.__contour is not None) and  (self.__contour.getType() not in [9,10]):
			self.__image.killRoi()
			self.__image.setRoi(self.__contour)
			self.__ip=self.__image.getProcessor()
			self.__rt= ResultsTable()
			analyser= Analyzer(self.__image, Analyzer.AREA+Analyzer.CENTER_OF_MASS+Analyzer.CENTROID+Analyzer.ELLIPSE+Analyzer.FERET+Analyzer.INTEGRATED_DENSITY+Analyzer.MEAN+Analyzer.KURTOSIS+Analyzer.SKEWNESS+Analyzer.MEDIAN+Analyzer.MIN_MAX+Analyzer.MODE+Analyzer.RECT+Analyzer.SHAPE_DESCRIPTORS+Analyzer.SLICE+Analyzer.STACK_POSITION+Analyzer.STD_DEV, self.__rt)
			analyser.measure()
			#self.__rt.show("myRT")
		else:
			self.__rt = ResultsTable()
			analyser = Analyzer(self.__image, Analyzer.AREA+Analyzer.CENTER_OF_MASS+Analyzer.CENTROID+Analyzer.ELLIPSE+Analyzer.FERET+Analyzer.INTEGRATED_DENSITY+Analyzer.MEAN+Analyzer.KURTOSIS+Analyzer.SKEWNESS+Analyzer.MEDIAN+Analyzer.MIN_MAX+Analyzer.MODE+Analyzer.RECT+Analyzer.SHAPE_DESCRIPTORS+Analyzer.SLICE+Analyzer.STACK_POSITION+Analyzer.STD_DEV, self.__rt)
			analyser.measure()
			#self.__rt.show("myRT")
			maxValues=self.__rt.getRowAsString(0).split("\t")
			heads=self.__rt.getColumnHeadings().split("\t")
			for val in heads: self.__rt.setValue(val, 0, Float.NaN)
			#self.__rt.show("myRT")

	# calculate the 1/2 , 1/4 ... 1/n positions for a liste while 1/n >= 1 returns a dict = 0: (0, [0, 0, pos(1/2)]) 1: (1, [-1, -0.5, -pos(1/4)], [0, 0, pos(1/2)], [1, 0.5, pos(1/2)])
	def __Centers(self, line) :
		L=len(line)
		l2=L//2
		l=L
		pos={}
		for i in range(self.log2(L)) : 
			l = l//2
			pos[i]=l
		l=L
		dicPos={}
		jtot=1
		for i in range(self.log2(L)) :
			s=[]
			j=1
			while (l2-j*pos[i])>0 or (l2+j*pos[i])<L :
				s.append((-j,(l2-j*pos[i])))
				s.append((j,(l2+j*pos[i])))
				j=j+1
			s.append((0,l2))
			s.sort()
			if ((len(s)+1)*pos[i]-L)//pos[i] > 0 :
				del s[0]
				del s[-1]

			else : pass
			if len(s) - 1  != 0 : jtot= (( len(s) - 1 ) / 2.00)+1
			else : jtot=1
			centers=[[v[0], v[0]/jtot, v[1]] for v in s]
			dicPos[i]=(i, centers)	
			del(s)
		return dicPos
		
	#calculate angle from the center of the midline to ends
	def __flexAngle(self) :
		try : 
			p1 = self.__midLine[0]
			p3 = self.__midLine[-1]
		except AttributeError : 
			self.__midline()
			p1 = self.__midLine[0]
			p3 = self.__midLine[-1]

		icenter = self.__midCenters[0][1][0][2]
		p2 = self.__midLine[icenter]
		#xpoints = (429,472,466)
		#ypoints = (114,133,99)
		xpoints = [int(p1[0]), int(p2[0]), int(p3[0])]
		ypoints = [int(p1[1]), int(p2[1]), int(p3[1])]
		#print ypoints
		#return ""
		r = PolygonRoi(xpoints, ypoints, 3, Roi.ANGLE)
		return r.getAngle()

	def __NbFoci(self):
		self.__boolFoci=True
		self.__image.killRoi()
		self.__image.setRoi(self.__contour)
		self.__ip=self.__image.getProcessor()
		rt=ResultsTable.getResultsTable()
		rt.reset()
		mf=MaximumFinder()
		mf.findMaxima(self.__ip, self.__noise, 0, MaximumFinder.LIST, True, False)
		self.__listMax[:]=[]
		
		#feret=self.getFercoord()
		#xc=feret[0]-((feret[0]-feret[2])/2.0)
		#yc=feret[1]-((feret[1]-feret[3])/2.0)

		#print xc, yc

		xc=self.getXC()
		yc=self.getYC()

		#print xc, yc
		
		for i in range(rt.getCounter()):
			x=int(rt.getValue("X", i))
			y=int(rt.getValue("Y", i))
			size=self.__localwand(x, y, self.__ip, self.__seuilPeaks, self.__peaksMethod, self.__light)
			coord=[(1, xc, x), (1, yc, y)]
 			d=self.distMorph(coord,"Euclidean distance")
 			d=( d / (self.getMaxF()/2) )*100
 			self.__listMax.append((x, y, size[0], size[1], size[2], size[3], size[4], d))
		rt.reset()
		
			
	def __FeretAxis(self):

		__boolFL=True
		if (self.__contour is not None) and (self.__contour.getType() in range(1,11)):
			self.__image.killRoi()
			self.__image.setRoi(self.__contour)
			if self.__contour.getType() in [1,5,9,10]:
				self.__polygon=self.__contour.getPolygon()
			else:
				self.__polygon=self.__contour.getFloatPolygon()
			points = self.__polygon.npoints
			self.__polx = self.__polygon.xpoints
			self.__poly = self.__polygon.ypoints
			diameter=0.0
			for i in range(points):
				for j in range(i, points):
					dx=self.__polx[i]-self.__polx[j]
					dy=self.__poly[i]-self.__poly[j]
					d=math.sqrt(dx*dx+dy*dy)
					if d>diameter:
						diameter=d
						i1=i
						i2=j
			
			tempDictY={ self.__poly[i1]:(self.__polx[i1],self.__poly[i1],self.__polx[i2],self.__poly[i2]), self.__poly[i2]:(self.__polx[i2],self.__poly[i2],self.__polx[i1],self.__poly[i1]) }			
			
			minY=min((self.__poly[i1],self.__poly[i2]))
			maxY=max((self.__poly[i1],self.__poly[i2]))
			lineTuple=tempDictY[maxY]
			
			self.__x1=lineTuple[0]
			self.__y1=lineTuple[1]
			self.__x2=lineTuple[2]
			self.__y2=lineTuple[3]
			
			self.__line= Line(self.__x1,self.__y1,self.__x2,self.__y2)			
			
			
		elif (self.__contour is not None) and (self.__contour.getType()==0):
			self.__x2=self.__contour.getBounds().x
			self.__y2=self.__contour.getBounds().y
			self.__x1=self.__contour.getBounds().x+self.__contour.getBounds().width
			self.__y1=self.__contour.getBounds().y+self.__contour.getBounds().height
			self.__line= Line(self.__x1,self.__y1,self.__x2,self.__y2)


		else:
			self.__x1="NaN"
			self.__y1="NaN"
			self.__x2="NaN"
			self.__y2="NaN"
			self.__fprofArray="NaN"
		
	def __FeretProfile(self):
		"""
			genere le profile le long du diametre de Feret
		"""
		self.__line.setWidth(self.__lw)
		self.__image.setRoi(self.__line, True)
		self.__fprof= ProfilePlot(self.__image)
		self.__fprofArray=self.__fprof.getProfile()
		if self.__showFpro: self.__fprof.createWindow()
		self.__image.killRoi()
		self.__line.setWidth(0)
		self.__image.setRoi(self.__contour)
		return self.__fprofArray
		
	def __midline(self):
		debug=False
		#print "line 251", self.__boolML
		if self.__boolML :
			ordpoints=self.__midLine[:]
			npoints=len(ordpoints)
			xpoints=[point[0] for point in ordpoints]
			ypoints=[point[1] for point in ordpoints]
			polyOrd=PolygonRoi(xpoints, ypoints, npoints, PolygonRoi.POLYLINE)
			return polyOrd

		#if self.getMaxF()<15 : return None
			#self.__FeretAxis()
			#return self.__line

		self.__boolML=True
		self.__image.killRoi()
		self.__image.setRoi(self.__contour)
		boundRect=self.__contour.getBounds()
		boundRoi=Roi(boundRect)
		xori=boundRect.x
		yori=boundRect.y
		wori=boundRect.width
		hori=boundRect.height
		ip2 = ByteProcessor(self.__image.getWidth(), self.__image.getHeight())
		ip2.setColor(255)
		ip2.setRoi(self.__contour)
		ip2.fill(self.__contour)
		skmp=ImagePlus("ip2", ip2)
		skmp.setRoi(xori-1,yori-1,wori+1,hori+1)
		ip3=ip2.crop()
		skmp3=ImagePlus("ip3", ip3)
		skmp3.killRoi()
		#-------------------------------------------------------------
		if debug : 
			skmp3.show()
			IJ.showMessage("imp3 l287")
		#-------------------------------------------------------------
		IJ.run(skmp3, "Skeletonize (2D/3D)", "")
		#IJ.run(skmp3, "Skeletonize", "")
		#-------------------------------------------------------------
		if debug : 
			skmp3.show()
			IJ.showMessage("imp3 l294")
		#-------------------------------------------------------------
		IJ.run(skmp3, "BinaryConnectivity ", "white")
		ip3.setThreshold(3,4, ImageProcessor.BLACK_AND_WHITE_LUT)
		IJ.run(skmp3, "Convert to Mask", "")
		#-------------------------------------------------------------
		if debug : 
			skmp3.show()
			IJ.showMessage("imp3 l302")
		#-------------------------------------------------------------
		#IJ.run(skmp3, "Skeletonize", "")
		#-------------------------------------------------------------
		if debug : 
			skmp3.updateAndDraw() 
			skmp3.show()
			IJ.showMessage("imp3 l308")
		#-------------------------------------------------------------
		rawPoints=[]
		w=ip3.getWidth()
		h=ip3.getHeight()
		
		rawPoints=[(x+xori,y+yori,self.__sommeVals(x,y,ip3)) for x in range(w) for y in range(h) if ip3.getPixel(x,y)==255]
		tempbouts=[val for val in rawPoints if val[2]==2]

		if len(tempbouts)!=2 : return None
		# test
		#if len(tempbouts)!=2 :
		#	
		#	IJ.run(skmp3, "BinaryConnectivity ", "white")
		#	ip3.setThreshold(3,3, ImageProcessor.BLACK_AND_WHITE_LUT)
		#	IJ.run(skmp3, "Convert to Mask", "")
		#	#-------------------------------------------------------------
		#	if debug==debug : 
		#		skmp3.updateAndDraw() 
		#		skmp3.show()
		#		IJ.showMessage("if test l 328")
		##-------------------------------------------------------------
		#	rawPoints=[(x+xori,y+yori,self.__sommeVals(x,y,ip3)) for x in range(w) for y in range(h) if ip3.getPixel(x,y)==255]
		#	tempbouts=[val for val in rawPoints if val[2]==2]
			
		ip3.setRoi(boundRect)
		if rawPoints==[]: return None
		npoints=len(rawPoints)
		xpoints=[point[0] for point in rawPoints]
		ypoints=[point[1] for point in rawPoints]
		valpoints=[point[2] for point in rawPoints]
		
		bouts={}
		
		if tempbouts==[]: return None
		
		if tempbouts[0][1]>tempbouts[1][1]:
			bouts["A"]=tempbouts[0]
			bouts["B"]=tempbouts[1]
		else:
			bouts["A"]=tempbouts[1]
			bouts["B"]=tempbouts[0]

		rawPoints.remove(bouts["A"])

		rawPoints.remove(bouts["B"])
		rawPoints.append(bouts["B"])

		tempList=[val for val in rawPoints]

		p=bouts["A"]
		Dist={}
		ordPoints=[]
		
		for j in range(len(rawPoints)):
			Dist.clear()
			for i in range(len(tempList)):
				dx=p[0]-tempList[i][0]
				dy=p[1]-tempList[i][1]
				d=math.sqrt(dx*dx+dy*dy)
				Dist[d]=tempList[i]

			distList=Dist.keys()
			mind=min(distList)
			nextpoint=Dist[mind]
			ordPoints.append(nextpoint)
			tempList.remove(nextpoint)
			p=nextpoint

		ordPoints.insert(0, bouts["A"])
		
		npoints=len(ordPoints)
		if npoints < 4 : return None
		xpoints=[point[0] for point in ordPoints]
		ypoints=[point[1] for point in ordPoints]
		polyOrd1=PolygonRoi(xpoints, ypoints, npoints, PolygonRoi.POLYLINE)
		
		f=min(self.__midParams[0], len(xpoints)//2)
		
		angleA1=polyOrd1.getAngle(xpoints[0],ypoints[0], xpoints[1],ypoints[1])
		angleA2=polyOrd1.getAngle(xpoints[1],ypoints[1], xpoints[2],ypoints[3])
		angleA = (angleA1+angleA2)/2.00
		angleA=polyOrd1.getAngle(xpoints[0],ypoints[0], xpoints[f],ypoints[f])
		angleA=angleA*(math.pi/180)
		
		angleB1=polyOrd1.getAngle(xpoints[-2],ypoints[-2], xpoints[-1],ypoints[-1])
		angleB2=polyOrd1.getAngle(xpoints[-3],ypoints[-3], xpoints[-2],ypoints[-2])
		angleB = (angleB1+angleB2)/2.00
		angleB=polyOrd1.getAngle(xpoints[-f],ypoints[-f], xpoints[-1],ypoints[-1])
		angleB=angleB*(math.pi/180)

		coef=self.__midParams[1]
		
		xa = xpoints[0]-coef*f*math.cos(angleA)
		ya = ypoints[0]+coef*f*math.sin(angleA)
		xb = xpoints[-1]+coef*f*math.cos(angleB)
		yb = ypoints[-1]-coef*f*math.sin(angleB)

		lineA=Line(xpoints[0],ypoints[0], xa, ya)
		lineB=Line(xpoints[-1],ypoints[-1], xb, yb)
		lineA.setWidth(0)
		lineB.setWidth(0)
		lineA.setStrokeWidth(0) 
		lineB.setStrokeWidth(0)
		
		ip2.setColor(0)
		ip2.fill()
		ip2.setColor(255)
		ip2.setRoi(lineA)
		lineA.drawPixels(ip2)
		ip2.setRoi(lineB)
		lineB.drawPixels(ip2)

		ip2.setRoi(self.__contour)
		ip2.setColor(0)
		ip2.fillOutside(self.__contour)
		ip2=ip2.crop()
		imb=ImagePlus("new-ip2", ip2)
				
		#-------------------------------------------------------------
		if debug : 
			imb.show()
			IJ.showMessage("imb l416")
		#-------------------------------------------------------------
		w2=ip2.getWidth()
		h2=ip2.getHeight()
		ip4 = ByteProcessor(w2+2, h2+2)
		im4=ImagePlus("im4", ip4)

		for i in range(w2):
			for j in range(h2):
				ip4.set(i+1,j+1,max([ip2.getPixel(i,j),ip3.getPixel(i,j)]))
		#im4.show()
		#-------------------------------------------------------------
		if debug : 
			im4.show()
			IJ.showMessage("im4 l430")
		#-------------------------------------------------------------
		im4.killRoi()
		#IJ.run(im4, "Skeletonize (2D/3D)", "")
		#IJ.run(skmp3, "Skeletonize", "")
		#-------------------------------------------------------------
		if debug : 
			imb.show()
			IJ.showMessage("imb l300")
		#-------------------------------------------------------------
		#IJ.run(skmp3, "Skeletonize", "")
		ip4=im4.getProcessor()
		
		rawPoints2=[]
		w4=ip4.getWidth()
		h4=ip4.getHeight()
		

		rawPoints2=[(x+xori-2,y+yori-2,self.__sommeVals(x,y,ip4)) for x in range(w4) for y in range(h4) if ip4.getPixel(x,y)==255]
		self.__MidBouts=[val for val in rawPoints2 if val[2]==2]

		# test
		if len(self.__MidBouts)!=2 : 
			IJ.run(im4, "BinaryConnectivity ", "white")
			ip4.setThreshold(3,3, ImageProcessor.BLACK_AND_WHITE_LUT)
			IJ.run(im4, "Convert to Mask", "")
			rawPoints2=[(x+xori-2,y+yori-2,self.__sommeVals(x,y,ip4)) for x in range(w4) for y in range(h4) if ip4.getPixel(x,y)==255]
			self.__MidBouts=[val for val in rawPoints2 if val[2]==2]
		
		ordpoints=[]
		p0=self.__MidBouts[0]
		rawPoints2.remove(p0)
		c=0
		
		while p0!=self.__MidBouts[1]:
			if c<len(rawPoints2):
				point=rawPoints2[c]
			else: break
			if abs(point[0]-p0[0])<2 and abs(point[1]-p0[1])<2:
				p0=point
				ordpoints.append(point)
				rawPoints2.remove(point)
				c=0
			else: c=c+1

		ordpoints.insert(0, self.__MidBouts[0])
		self.__midLine=ordpoints[:]
		self.__midCenters = self.__Centers(self.__midLine)
		npoints=len(ordpoints)
		xpoints=[point[0] for point in ordpoints]
		ypoints=[point[1] for point in ordpoints]

		polyOrd=PolygonRoi(xpoints, ypoints, npoints, PolygonRoi.POLYLINE)

		
		#print self.__midLine
		#print self.__MidBouts
		#print xpoints
		#print ypoints

		return polyOrd
		
	def __sommeVals(self, x, y, ip):

		return (ip.getPixel(x,y)+ip.getPixel(x-1,y-1)+ip.getPixel(x,y-1)+ip.getPixel(x+1,y-1)+ip.getPixel(x-1,y)+ip.getPixel(x+1,y)+ip.getPixel(x-1,y+1)+ip.getPixel(x,y+1)+ip.getPixel(x+1,y+1))/255

	def __localwand(self, x, y, ip, seuil, method, light):
		self.__image.killRoi()
		ip.snapshot()
		if method == "mean" : 
			peak=ip.getPixel(x,y)
			tol = (peak - self.getMean())*seuil
			w = Wand(ip)
			w.autoOutline(x, y, tol, Wand.EIGHT_CONNECTED)
			#print "method=", method, tol, peak
			
		elif method == "background" : 
			radius = self.getMinF()/4 
			bs = BackgroundSubtracter()
			#rollingBallBackground(ImageProcessor ip, double radius, boolean createBackground, boolean lightBackground, boolean useParaboloid, boolean doPresmooth, boolean correctCorners) 
			bs.rollingBallBackground(ip, radius, False, light, False, True, False)
			peak=ip.getPixel(x,y)
			tol = peak*seuil
			w = Wand(ip)
			w.autoOutline(x, y, tol, Wand.EIGHT_CONNECTED)
			ip.reset()
			#print "method=", method, tol, radius, peak
			
		else : 
			peak=ip.getPixel(x,y)
			tol = peak*seuil
			w = Wand(ip)
			w.autoOutline(x, y, tol, Wand.EIGHT_CONNECTED)
			#print "method=", method, tol

		peak=ip.getPixel(x,y)
		temproi=PolygonRoi(w.xpoints, w.ypoints, w.npoints, PolygonRoi.POLYGON)
		self.__image.setRoi(temproi)
		#self.__image.show()
		#time.sleep(1)
		#peakip=self.__image.getProcessor()
		#stats=peakip.getStatistics()
		temprt = ResultsTable()
		analyser = Analyzer(self.__image, Analyzer.AREA+Analyzer.INTEGRATED_DENSITY+Analyzer.FERET, temprt)
		analyser.measure()
		#temprt.show("temprt")
		rtValues=temprt.getRowAsString(0).split("\t")
		area=float(rtValues[1])
		intDen=float(rtValues[4])
		feret=float(rtValues[2])
		mean=intDen/area
		#time.sleep(2)
		temprt.reset()
		self.__image.killRoi()
		return [peak, area, mean, intDen, feret]
					
		
	
	def __MidProfil(self):
		if not self.__boolML :  self.__midline()
		ip=self.__image.getProcessor()
		line=Line(self.__midLine[0][0],self.__midLine[0][1],self.__midLine[-1][0],self.__midLine[-1][1])
		line.setWidth(self.__lw)

		self.__MprofArray=[]
		self.__MprofArray[:]=[]
		for i in range(0,len(self.__midLine)-1):
			templine=Line(self.__midLine[i][0],self.__midLine[i][1],self.__midLine[i+1][0],self.__midLine[i+1][1])
			templine.setWidth(self.__lw)
			self.__image.setRoi(templine)
			#time.sleep(0.5)
			temprof= ProfilePlot(self.__image)
			temparray=temprof.getProfile()
			self.__MprofArray+=temparray
		templine.setWidth(0)
		#if self.__showMidpro: self.__fprof.createWindow()	 
		return
		
	def getFeretSegments(self, n):
		self.__boolFS=True
		if(not self.__boolFL):
			self.__FeretAxis()
		radius=self.getMinF()
		lsegment=self.__line.getLength()/((n-1)*2)
		xo=self.__x1
		yo=self.__y1
		xf=self.__x2
		yf=self.__y2
		angle1=self.getAngF()*(math.pi/180)
		angle2=self.getAngF()*(math.pi/180)+(math.pi/2)
		avancex=(lsegment*2)*math.cos(angle1)
		avancey=(lsegment*2)*math.sin(angle1)
		delta90x=(radius)*math.cos(angle2)
		delta90y=(radius)*math.sin(angle2)
		self.__line.setWidth(int(lsegment*2))
		#self.__image.setRoi(self.__line)
		tempcontour=self.__contour.clone()
		shapeContour=ShapeRoi(tempcontour)
		segsRoi=[]
		for i in range(n):
			tempLine=Line(xo-delta90x, yo+delta90y, xo+delta90x, yo-delta90y)
			tempLine.setWidth(int(lsegment*2))
			poly=tempLine.getPolygon()
			roipol=PolygonRoi(poly, Roi.POLYGON)
			shapePoly= ShapeRoi(roipol)
			interShape=shapePoly.and(shapeContour)
			segsRoi.append(interShape.shapeToRoi())
			xo=xo+avancex
			yo=yo-avancey

		
		#self.__image.setRoi(self.__contour, True)
		#time.sleep(0)
		self.__line.setWidth(0)
		#self.__image.setRoi(tempcontour)
		
		#self.__image.updateAndDraw() 
		
		return segsRoi # return Roi array

	def getMidSegments(self, n=10, r=5, tool=0):
		self.__boolMS=True
		if(not self.__boolML):
			self.__midline()
		lsegment=int(len(self.__midLine)/n)
		if lsegment<2:lsegment=2
		ls2=int(len(self.__midLine)/(2*n))
		if ls2<1: ls2=1
		ip=self.__image.getProcessor()
		#print(len(self.__midLine), lsegment, ls2)
		xo=self.__MidBouts[0][0]
		yo=self.__MidBouts[0][1]
		xf=self.__MidBouts[1][0]
		yf=self.__MidBouts[1][1]
		line1=Line(xo,yo,xf,yf)
		line1.setWidth(0)
		angles=[line1.getAngle(self.__midLine[i][0], self.__midLine[i][1], self.__midLine[i+lsegment][0], self.__midLine[i+lsegment][1]) for i in range(0,len(self.__midLine)-lsegment,lsegment)]
		points=[self.__midLine[i] for i in range(0,len(self.__midLine),lsegment)]
		lastangle=line1.getAngle(self.__midLine[-ls2][0],self.__midLine[-ls2][1],self.__midLine[-1][0],self.__midLine[-1][1])
		angles.append(lastangle)
		tempcontour=self.__contour.clone()
		shapeContour=ShapeRoi(tempcontour)
		angles=[angle*(math.pi/180)+(math.pi/2) for angle in angles]
		line1.setWidth((ls2+1)*2)
		segsRoi=[]
		linesRois=[]
		cRois=[]
		for i in range(len(angles)):
			x=points[i][0]
			y=points[i][1]
			cRois.append(PointRoi(x,y))
			if tool==0: # ligne perpendiculaire d'epaiseur  (ls2+1)*2
				line1.setWidth((ls2+1)*2)
				x1=x+r*math.cos(angles[i])
				y1=y-r*math.sin(angles[i])
				x2=x-r*math.cos(angles[i])
				y2=y+r*math.sin(angles[i])
				#print(x, y, x1, y1, x2, y2)
				tempLine=Line(x1,y1,x2,y2)
				linesRois.append(tempLine)
				tempLine.setWidth((ls2+1)*2)
				#self.__image.setRoi(tempLine, True)
				#time.sleep(0.3)
				poly=tempLine.getPolygon()
				roipol=PolygonRoi(poly, Roi.POLYGON)
				shapePoly= ShapeRoi(roipol)
			elif tool==1:
				#r1=r*0.7
				x1=x+r
				y1=y-r
				x2=x-r
				y2=y+r
				ellipse=EllipseRoi(x1, y1, x2, y2, 1)
				linesRois.append(ellipse)
				#print(x, y, x1, y1, x2, y2)
				#self.__image.setRoi(ellipse, True)
				shapePoly= ShapeRoi(ellipse)
				#time.sleep(0.3)
			else:
				x1=x
				y1=y
				line1.setWidth(r)
				if (i+1)<len(points):
					x2=points[i+1][0]
					y2=points[i+1][1]
				else:
					#x1=x+lsegment*math.cos(angles[i]-(math.pi/2))
					#y1=y-lsegment*math.sin(angles[i]-(math.pi/2))
					x2=x+lsegment*math.cos(angles[i]-(math.pi/2))
					y2=y-lsegment*math.sin(angles[i]-(math.pi/2))
					#x2=xf
					#y2=yf
				
				tempLine=Line(x1,y1,x2,y2)
				linesRois.append(tempLine)
				tempLine.setWidth(r)
				#self.__image.setRoi(tempLine, True)
				#time.sleep(0.5)
				poly=tempLine.getPolygon()
				roipol=PolygonRoi(poly, Roi.POLYGON)
				shapePoly= ShapeRoi(roipol)
			
			interShape=shapePoly.and(shapeContour)
			interRoi=interShape.shapeToRoi()
			segsRoi.append(interShape.shapeToRoi())
		line1.setWidth(0)
		return (segsRoi, linesRois, cRois)

	def selectInitRoi(self):
		self.__image.killRoi()
		self.__image.setRoi(self.__contour)
		time.sleep(0)

	@staticmethod
	def distMorph(coord,distmethod="Euclidean distance"):
		if distmethod == "Euclidean distance" :
			s=[val[0]*(val[2]-val[1])*(val[2]-val[1]) for val in coord]
			#print s
			#print sum(s)
			return math.sqrt(sum(s))
		if distmethod == "Logarithm distance" :
			s=[val[0]*abs(math.log(val[2]/val[1])) for val in coord]
			return sum(s)
			
	@staticmethod
	def log2(n) : return math.log(n)/math.log(2)
	
	def Out(self): print("out")

	

#------ end methodes---------------
#------ constructeur -------------

	def __init__(self, imp, roi):
		self.__lw=0
		self.__showFpro=False
		self.__Feret=[] 
		self.__image=imp
		self.__cal=imp.getCalibration()
		self.__contour=roi.clone()
		self.__boolmeasures=False
		self.__boolFP=False
		self.__boolFL=False
		self.__boolML=False
		self.__boolMP=False
		self.__boolFS=False
		self.__boolMS=False
		self.__boolFoci=False
		self.__midParams=[10, 1.3]
		self.__listMax=[]
		self.__noise=150
		self.__seuilPeaks=0.75
		self.__peaksMethod="mean"
		self.__light=False
		self.__distot=0.00
		self.__flexangle=0.00
		#print "dropbox MorphoBactProject"
	
#---------- end constructor---------
#---------- getteurs----------------
	
	def getMaxF(self):
		if(not self.__boolmeasures): self.__Measures()
		return self.__rt.getValue("Feret", 0)
	def getMinF(self):
		if(not self.__boolmeasures): self.__Measures()
		return self.__rt.getValue("MinFeret", 0)
	def getXF(self):
		if(not self.__boolmeasures): self.__Measures() 
		return self.__rt.getValue("FeretX", 0)
	def getYF(self): 
		if(not self.__boolmeasures): self.__Measures()
		return self.__rt.getValue("FeretY", 0)
	def getAngF(self):
		if(not self.__boolmeasures): self.__Measures()
		return self.__rt.getValue("FeretAngle", 0)
	def getArea(self): 
		if(not self.__boolmeasures): self.__Measures()
		return self.__rt.getValue("Area", 0)
	def getMean(self):
		if(not self.__boolmeasures): self.__Measures()
		return self.__rt.getValue("Mean", 0)
	def getKurt(self):
		if(not self.__boolmeasures): self.__Measures()
		return self.__rt.getValue("Kurt", 0)
	def getSkew(self):
		if(not self.__boolmeasures): self.__Measures()
		return self.__rt.getValue("Skew", 0)
	def getIntDen(self):
		if(not self.__boolmeasures): self.__Measures()
		return self.__rt.getValue("IntDen", 0)
	def getStdDev(self):
		if(not self.__boolmeasures): self.__Measures()
		return self.__rt.getValue("StdDev", 0)
	def getAngle(self): 
		if(not self.__boolmeasures): self.__Measures()
		return self.__rt.getValue("Angle", 0)
	def getMajor(self): 
		if(not self.__boolmeasures): self.__Measures()
		return self.__rt.getValue("Major", 0)
	def getMinor(self): 
		if(not self.__boolmeasures): self.__Measures()
		return self.__rt.getValue("Minor", 0)
	def getSolidity(self): 
		if(not self.__boolmeasures): self.__Measures()
		return self.__rt.getValue("Solidity", 0)
	def getAR(self): 
		if(not self.__boolmeasures): self.__Measures()
		return self.__rt.getValue("AR", 0)
	def getRound(self): 
		if(not self.__boolmeasures): self.__Measures()
		return self.__rt.getValue("Round", 0)
	def getCirc(self): 
		if(not self.__boolmeasures): self.__Measures()
		return self.__rt.getValue("Circ.", 0)
	def getXM(self): 
		if(not self.__boolmeasures): self.__Measures()
		return self.__rt.getValue("XM",0)
	def getYM(self): 
		if(not self.__boolmeasures): self.__Measures()
		return self.__rt.getValue("YM",0)
	def getXC(self): 
		if(not self.__boolmeasures): self.__Measures()
		return self.__rt.getValue("X",0)
	def getYC(self): 
		if(not self.__boolmeasures): self.__Measures()
		return self.__rt.getValue("Y",0)

	def getNfoci(self):
		if(not self.__boolFoci):self.__NbFoci()
		return len(self.__listMax)

	def getListFoci(self):
		if(not self.__boolFoci):self.__NbFoci()
		xy=[]
		for val in self.__listMax:
			xy.append((val[0], val[1]))
		return xy

	def getListPeaksFoci(self):
		if(not self.__boolFoci):self.__NbFoci()
		peaks=[]
		for val in self.__listMax:
			peaks.append(val[2])
		return peaks

	def getListAreaFoci(self):
		if(not self.__boolFoci):self.__NbFoci()
		areas=[]
		for val in self.__listMax:
			areas.append(val[3])
		return areas

	def getListMeanFoci(self):
		if(not self.__boolFoci):self.__NbFoci()
		means=[]
		for val in self.__listMax:
			means.append(val[4])
		return means

	def getListIntDenFoci(self):
		if(not self.__boolFoci):self.__NbFoci()
		ints=[]
		for val in self.__listMax:
			ints.append(val[5])
		return ints

	def getListFeretFoci(self):
		if(not self.__boolFoci):self.__NbFoci()
		ferets=[]
		for val in self.__listMax:
			ferets.append(val[6])
		return ferets

	def getListDistsFoci(self):
		if(not self.__boolFoci):self.__NbFoci()
		dists=[]
		for val in self.__listMax:
			dists.append(val[7])
		return dists

	def getFercoord(self):
		"""
			FerCoord: tuple contenant x1, y1, x2, y2 du MaxFeret
		"""
		if (not self.__boolFL): self.__FeretAxis()
		return (self.__x1,self.__y1,self.__x2,self.__y2)
	def getFprofil(self):
		"""
			Fprofil: list contenant les valeurs du profil le long de MaxFeret
		"""
		if (not self.__boolFL): self.__FeretAxis()
		self.__FeretProfile()
		return self.__fprofArray
	def getFerAxis(self):
		"""
			FerAxis: Line ROI
		"""
		if(not self.__boolFL): self.__FeretAxis()
		return self.__line
		
	def getMidAxis(self):
		"""
			MidAxis: Polyline ROI de l'axe median par skeletonize
		"""
		if(not self.__boolML): return self.__midline()

	def getMidSegs(self, n, r, tool):
		"""
			Rois des segments 0 = rois , 1 = points, 2 = lines ou ellipses
		"""
		if(not self.__boolMS): return self.getMidSegments(n, r, tool)
		
	def getMidProfil(self):
		"""
			MidProfil: list contenant les valeurs du profil le long de MidAxis
		"""
		self.__MidProfil()
		return self.__MprofArray

	def getMidPoints(self) :
		"""
			MidPoints : list of two extreme points of mid Axis
		"""
		if(not self.__boolML): self.__midline()
		return self.__MidBouts

	def getCenters(self) :
		if(not self.__boolML): self.__midline()
		return self.__midCenters

	def getFlexAngle(self) :
		return self.__flexAngle()


#------ setteurs --------

	def setImage(self, imp):
		self.__image=imp

	def setImageMeasures(self, imp):
		self.__image=imp
		self.__Measures()
		
	def setImageFprofil(self, imp):
		self.__image=imp
		self.__FeretProfile()

	def setImageMidprofil(self, imp):
		self.__image=imp
		self.__MidProfil()

	def setLineWidth(self, lw):
		self.__lw=lw

	def setshowFpro(self, fpshow):
		self.__showFpro=fpshow

	def setMidParams(self, lseg, coeff):
		self.__midParams[:]=[]
		self.__midParams.append(lseg)
		self.__midParams.append(coeff)

	def setNoise(self, noise):
		self.__noise=noise

	def setSeuilPeaks(self, seuil):
		self.__seuilPeaks=seuil

	def setpeaksMethod(self, method):
		self.__peaksMethod=method

	def setlight(self, light):
		self.__light=light

		
#------- properties -----------------------
	
	MaxFeret=property(getMaxF, setImageMeasures, doc="caliper max Feret=")
	MinFeret=property(getMinF, setImageMeasures, doc="caliper min Feret=")
	AngleFeret=property(getAngF, setImageMeasures, doc="angle Feret=")
	XFeret=property(getXF, setImageMeasures, doc="X Feret=")
	YFeret=property(getYF, setImageMeasures, doc="Y Feret=")
	Area=property(getArea, setImageMeasures, doc="Area=")
	Mean=property(getMean, setImageMeasures, doc="Mean=")
	Kurt=property(getKurt, setImageMeasures, doc="Kurtosis=")
	Skew=property(getSkew, setImageMeasures, doc="Skewness=")
	IntDen=property(getIntDen, setImageMeasures, doc="Integrated Intensity=")
	StdDev=property(getStdDev, setImageMeasures, doc="Standard Deviation=")
	Angle=property(getAngle, setImageMeasures, doc="Angle=")
	Major=property(getMajor, setImageMeasures, doc="Major ellipse axis=")
	Minor=property(getMinor, setImageMeasures, doc="Minor ellipse axis=")
	Solidity=property(getSolidity, setImageMeasures, doc="Solidity area/convexHull=")
	AR=property(getAR, setImageMeasures, doc="Major axis/Minor axis=")
	Round=property(getRound, setImageMeasures, doc="Area/(pi*Major*Major)=1/AR=")
	Circ=property(getCirc, setImageMeasures, doc="(4*pi*Area)/(perimeter*perimeter)=")
	XM=property(getXM, setImageMeasures, doc="X center of Mass=")
	YM=property(getYM, setImageMeasures, doc="Y center of Mass=")
	XC=property(getXC, setImageMeasures, doc="X of centroid=")
	YC=property(getYC, setImageMeasures, doc="Y of centroid=")
	
	NFoci=property(getNfoci, setImageMeasures, doc="nb foci in the roi=")
	ListFoci=property(getListFoci, setImageMeasures, doc="list of foci coordinates=")
	ListPeaksFoci=property(getListPeaksFoci, setImageMeasures, doc="list of foci peaks=")
	ListAreaFoci=property(getListAreaFoci, setImageMeasures, doc="list of foci areas=")
	ListMeanFoci=property(getListMeanFoci, setImageMeasures, doc="list of foci means=")
	ListIntDenFoci=property(getListIntDenFoci, setImageMeasures, doc="list of foci IntDen=")
	ListFeretFoci=property(getListFeretFoci, setImageMeasures, doc="list of foci Ferets=")
	ListDistsFoci=property(getListDistsFoci, setImageMeasures, doc="list of foci distances=")
	
	FerCoord=property(getFercoord, doc="x1, y1, x2, y2 of Feret diameter =")
	Fprofil=property(getFprofil, setImageFprofil, doc="Profil along Feret diameter") 	# return array values
	FerAxis=property(getFerAxis, doc="ROI along Feret diameter")				# return Roi
	
	MidAxis=property(getMidAxis, doc="ROI along the median axis")				# return Roi
	MidProfil=property(getMidProfil, setImageMidprofil, doc="profile values of mid axis")	# return array values
	MidPoints=property(getMidPoints, doc= "extreme points of mid line")

	Centers=property(getCenters, doc= "list of positions (tuples) of 1/2 of the midaxis, 1/4, 1/8 ...") # return a list of tuples
	FlexAngle=property(getFlexAngle, doc="angle of the center of midline to ends")	
Example #16
0
class previewLabelerAndListeners(ActionListener, AdjustmentListener):
    '''Class which unique function is to handle the button clics'''
    def __init__(self, imp1, slider1, slider2, gd):
        self.height = imp1.getHeight()
        self.width = imp1.getWidth()
        self.depth = imp1.getStackSize()

        self.stats = StackStatistics(imp1)
        self.src = clij2.push(imp1)
        self.cal = imp1.getCalibration()
        self.size = int(self.stats.max)
        self.labelValues = [1] * int(self.size + 1)
        self.labelValues[0] = 0

        self.nucleiLabels = range(self.size + 1)
        self.results = ResultsTable()

        self.renderPreview(1)
        #self.results.show("results")
        self.labelDict = {
            0: [0],
            1: sorted(self.nucleiLabels[1:]),
            2: [],
            3: [],
            4: [],
            5: [],
            6: [],
            7: [],
            8: [],
            9: [],
            10: []
        }

        self.identifiers = []
        self.xs = []
        self.ys = []
        self.zs = []
        self.bbz = []
        self.bbze = []

        for i in range(int(self.size + 1)):
            try:

                self.xs.append(int(self.results.getValue("CENTROID_X", i)))
                self.ys.append(int(self.results.getValue("CENTROID_Y", i)))
                self.zs.append(int(self.results.getValue("CENTROID_Z", i)))
                self.bbz.append(self.results.getValue("BOUNDING_BOX_Z", i) + 1)
                self.bbze.append(
                    self.results.getValue("BOUNDING_BOX_END_Z", i) + 1)
                self.identifiers.append(self.results.getValue("IDENTIFIER", i))
            except:
                print i
                self.identifiers.append(self.results.getValue("IDENTIFIER", i))
                self.xs.append(int(width / 2))
                self.ys.append(int(height / 2))
                self.zs.append(int(depth / 2))
                self.bbz.append(1)
                self.bbze.append(1)

        self.nucLoc = map(lambda i: self.ys[i] * width + self.xs[i],
                          range(len(self.xs)))
        """labelPreviewImp -  label image preview; maxZPreviewImp - maxZ label preview; maxYPreviewImp - maxY label preview"""

        self.slider1 = slider1
        self.slider2 = slider2
        self.gd = gd
        self.current = self.labelPreviewImp.getCurrentSlice()

    def renderPreview(self, runStats):
        try:
            self.labelPreviewImp.close()
            self.maxZPreviewImp.close()
            self.maxYPreviewImp.close()
        except:
            print "imps already closed"

        fp = ShortProcessor(len(self.labelValues), 1, self.labelValues, None)
        labelerImp = ImagePlus("labeler", fp)
        src2 = clij2.push(labelerImp)
        dst = clij2.create(self.src)
        labelerImp.close()

        clij2.replaceIntensities(self.src, src2, dst)
        self.labelPreviewImp = clij2.pull(dst)
        previewDisplaySettings(self.labelPreviewImp, "label preview", 100,
                               self.cal)

        try:
            self.labelPreviewImp.setSlice(self.current)
        except:
            pass
        if runStats:
            clij2.statisticsOfBackgroundAndLabelledPixels(
                dst, self.src, self.results)
        dst2 = clij2.create(width, height, 1)
        clij2.maximumZProjection(dst, dst2)
        self.maxZPreviewImp = clij2.pull(dst2)
        previewDisplaySettings(self.maxZPreviewImp, "maxZ label preview", 50,
                               self.cal)

        dst3 = clij2.create(width, depth, 1)
        clij2.maximumYProjection(dst, dst3)
        self.maxYPreviewImp = clij2.pull(dst3)
        previewDisplaySettings(self.maxYPreviewImp, "maxY label preview", 50,
                               self.cal)

        dst3.close()
        dst.close()
        dst2.close()
        src2.close()

        labelWindow = self.labelPreviewImp.getWindow()
        x = labelWindow.getLocation().x
        y = labelWindow.getLocation().y

        maxZPreviewWindow = self.maxZPreviewImp.getWindow()
        maxZPreviewWindow.setLocation(x, y + height + 50)
        maxYPreviewWindow = self.maxYPreviewImp.getWindow()
        maxYPreviewWindow.setLocation(x + width / 2, y + height + 50)
        print labelWindow

    def actionPerformed(self, event):
        """event: actionlistener does stuff on buttonpress"""

        Source = event.getSource()  # returns the Button object
        self.current = self.labelPreviewImp.getCurrentSlice()

        if Source.label == "Set top":
            self.top = self.labelPreviewImp.getCurrentSlice()
            self.bottom = self.slider2.getValue()
            self.slider1.setValue(int(self.top))
            if (self.bottom < self.top):
                self.bottom = self.top
                self.slider2.setValue(int(self.top))
            return

        if Source.label == "Set bottom":
            self.top = self.slider1.getValue()
            self.bottom = self.labelPreviewImp.getCurrentSlice()
            self.slider2.setValue(int(self.bottom))
            if self.bottom < self.top:
                self.slider1.setValue(int(self.bottom))
                self.top = self.bottom
                print "B"
            return

        if Source.label == "Whole stack":
            self.top = 1
            self.bottom = depth
            self.slider1.setValue(int(self.top))
            self.slider2.setValue(int(self.bottom))
            return

        if Source.label == "Just slice":
            print "current slice"
            self.top = self.labelPreviewImp.getCurrentSlice()
            self.bottom = self.labelPreviewImp.getCurrentSlice()
            self.slider2.setValue(int(self.top))
            self.slider1.setValue(int(self.bottom))
            return

        if Source.label[:5] == "label":
            [int(s) for s in Source.label.split() if s.isdigit()]
            s = int(s)
            print "s " + str(s)
            #try:
            roi1 = self.labelPreviewImp.getRoi()
            selectedPixels = roi1.getContainedPoints()
            roiPixelLoc = map(lambda i: i.y * width + i.x, selectedPixels)

            pixels2 = filter(lambda i: self.nucLoc[i] in roiPixelLoc,
                             xrange(len(self.nucLoc)))

            #on first run set self.top and self.bottom to top and bottom of the stack
            try:
                print self.top
            except:
                self.top = 1
            try:
                print self.bottom
            except:
                self.bottom = self.labelPreviewImp.getNSlices()
            choice = self.gd.getChoices().get(0)

            if choice.getSelectedItem() == "Slice":
                top = self.current
                bottom = self.current
            else:
                top = self.top
                bottom = self.bottom

            pixels2 = filter(
                lambda i: max(self.bbze[i], bottom) - min(top, self.bbz[i]) <=
                (self.bbze[i] - self.bbz[i]) + (bottom - top), pixels2)
            self.nucleiLabels = map(lambda i: i, pixels2)

            #print(map(lambda i:self.zs[i], pixels2))
            #print len(pixels2)

            self.labelDict[s] = self.nucleiLabels + self.labelDict[s]
            self.labelDict[s] = sorted(list(set(self.labelDict[s])))

            for key in self.labelDict:
                if key != s:
                    self.labelDict[key] = filter(
                        lambda x: x not in self.labelDict[s],
                        self.labelDict[key])
            #print labelDict
            for key in self.labelDict:
                for value in self.labelDict[int(key)]:
                    self.labelValues[value] = int(key)
        self.labelValues[0] = 0
        self.labelDict[0] = [0]
        for key in self.labelDict.keys()[1:]:
            self.labelDict[key] = filter(lambda x: x != 0, self.labelDict[key])
        self.renderPreview(0)

        #except:
        #	print "No roi"
        return
        # Do an action depending on the button clicked
    def adjustmentValueChanged(self, event):
        """ event: an AdjustmentEvent with data on the state of the scroll bar. """

        #return if the user is still dragging

        if event.getValueIsAdjusting():
            return
        Source = event.getSource()

        try:
            self.slider1 = self.gd.getSliders().get(0)
        except:
            print "huh?"
        try:
            self.slider2 = self.gd.getSliders().get(1)
        except:
            print "huh?2"

        self.top = self.slider1.getValue()
        self.bottom = self.slider2.getValue()

        #make top and bottom sliders autocorrect for each other
        if Source == self.slider1:
            if self.top > self.bottom:
                self.slider2.setValue(int(self.top))
                self.bottom = self.top
                print "A"
        if Source == self.slider2:
            if self.bottom < self.top:
                self.slider1.setValue(int(self.bottom))
                self.top = self.bottom
                print "B"
def import_and_straighten(imgDir):
    """
    Core function. Opens images in given directory in series and calls a straightening function.
    Thresholds using weka if stddev of pixel intensities is too high (bright field), otherwise uses
    histogram based segmentation.
    """
    targetWidth = 800 #adjustable
    make_directory(imgDir)
    index = "000000000"
    filename = imgDir + "/img_" + index + "__000.tif"
    if path.exists(filename):
        weka = Weka_segmentor(IJ.openImage(filename))
    while path.exists(filename):
        IJ.run("Set Measurements...", "mean standard min center redirect=None decimal=3")
        IJ.run("Clear Results")
        imp = IJ.openImage(filename)
        imp.show()
        IJ.run("Rotate 90 Degrees Left")
        IJ.run("Measure")
        table = RT.getResultsTable()
        stddev = RT.getValue(table, "StdDev", 0)

        if stddev < 20:
            segmented = weka.getThreshold(imp)
            segmented.show()
            IJ.run("8-bit")
            IJ.run("Invert")
            imp.close()
            imp = segmented

        else:
            IJ.run(imp, "Auto Threshold", "method=Li white") #threshold

        imp = preProcess_(imp)

        #straighten_roi_rotation()
        coords = run_straighten()

        newImp = IJ.getImage()
        """
        fs = FileSaver(newImp)
        fs.saveAsTiff(imgDir + "/straightened" + "/img_" + index + "__000-straight.tif")
        """

        IJ.run("Image Padder", "pad_right="+str(targetWidth - newImp.getWidth()))
        paddedImp = IJ.getImage()
        fs = FileSaver(paddedImp)
        fs.saveAsTiff(imgDir + "/binary" + "/img_" + index + "__000-padded.tif")

        IJ.runMacro("while (nImages>0) {selectImage(nImages);close();}")

        #use same centerline for original greyscale image
        imp = IJ.openImage(filename)
        imp.show()
        IJ.run("Rotate 90 Degrees Left")
        IJ.run("8-bit")
        IJ.runMacro("makeLine("+coords+")")
        IJ.run("Straighten...", "line = 80")
        newImp = IJ.getImage()
        IJ.run("Image Padder", "pad_right="+str(targetWidth - newImp.getWidth()))
        paddedImp = IJ.getImage()
        IJ.run("8-bit")
        fs = FileSaver(paddedImp)
        fs.saveAsTiff(imgDir + "/greyscale" + "/img_" + index + "__000-padded.tif")
        IJ.runMacro("while (nImages>0) {selectImage(nImages);close();}")

        index = to_9_Digits(str(int(index)+1))
        filename = filename = imgDir + "/img_" + index + "__000.tif"
            consol.addValue("foci_count", 0)
            rowcount = 1

            # loop over all cells, add cell number to the "cell" column
            for count in range(cell):
                consol.setValue("cell", count, count)

            # loop over all foci
            for count in range(rt.size()):
                # get in which cell that foci is and increase the
                # counter on the summary results table
                currcell = int(rt.getValue("cell", count))

                consol.setValue(
                    "foci_count", currcell,
                    int(consol.getValue("foci_count", currcell)) + 1)
            for count in range(1, cell):
                foci = consol.getValue("foci_count", count)
                if foci <= 3:
                    cellsperfoci[foci] = cellsperfoci[foci] + 1
                else:
                    cellsperfoci["more"] = cellsperfoci["more"] + 1
            print(cellsperfoci)
            # close the results window
            IJ.selectWindow("Results")
            IJ.run("Close")

        # this segment only runs if a mCherry channel is present
        if (linechannel > 0):
            # generate image with mCherry channel
            ImagePlus("stack", linefoci_stack).show()
Example #19
0
                    # Find the ROI with the largest area

                    for i, area in enumerate(
                            table.getColumn(table.getColumnIndex("Area"))):
                        if area > maxArea:
                            index = i

                if thresholdMode:
                    imp.show()

                # Writes everything in the output file
                if index != -1:

                    diameter = 2 * math.sqrt(
                        float(table.getValue("Area", index)) / (2 * math.pi))
                    isOrganoid = table.getValue(
                        "Area", index) > area_threshold and table.getValue(
                            "Area", index) > round_threshold
                    output.write(
                        str(subfolder) + ',' + filename + ',' +
                        str(table.getValue("Feret", index)) + ',' +
                        str(table.getValue("MinFeret", index)) + ',' +
                        str((table.getValue("MinFeret", index) +
                             table.getValue("Feret", index)) / 2) + ',' +
                        str(table.getValue("Area", index)) + ',' +
                        str(diameter) + ',' +
                        str(table.getValue("Major", index)) + ',' +
                        str(table.getValue("Minor", index)) + ',' +
                        str(table.getValue("Circ.", index)) + ',' +
                        str(table.getValue("Round", index)) + ',' +
Example #20
0
    for value in test.labelDict[key]:
        listOfNames[value] = names[key]

if imageOrTable == "Results table":
    rt = ResultsTable.getResultsTable(resultsName)

else:
    measureImp = WM.getImage(imageName)
    src2 = clij2.push(measureImp)
    rt = ResultsTable()
    clij2.statisticsOfBackgroundAndLabelledPixels(src2, test.src, rt)
    src2.close()
    resultsName = "Results table"
collumnNumber = rt.getLastColumn() + 1
for i in range(len(listOfNames)):
    try:
        j = rt.getValue("Identifier", i)

    except:
        try:
            j = rt.getValue("Label", i)
        except:
            j = i
    rt.setValue("Label name", i, listOfNames[int(j)])
    rt.setValue("Label value", i, test.labelValues[int(j)])

rt.show(resultsName + " with labels")
clij2.clear()
labelColorBarImp.close()
def import_and_straighten(imgDir):
    """
    Core function. Opens images in given directory in series and calls a straightening function.
    Thresholds using weka if stddev of pixel intensities is too high (bright field), otherwise uses
    histogram based segmentation.
    """
    targetWidth = 800  #adjustable
    make_directory(imgDir)
    index = "000000000"
    filename = imgDir + "/img_" + index + "__000.tif"
    if path.exists(filename):
        weka = Weka_segmentor(IJ.openImage(filename))
    while path.exists(filename):
        IJ.run("Set Measurements...",
               "mean standard min center redirect=None decimal=3")
        IJ.run("Clear Results")
        imp = IJ.openImage(filename)
        imp.show()
        IJ.run("Rotate 90 Degrees Left")
        IJ.run("Measure")
        table = RT.getResultsTable()
        stddev = RT.getValue(table, "StdDev", 0)

        if stddev < 20:
            segmented = weka.getThreshold(imp)
            segmented.show()
            IJ.run("8-bit")
            IJ.run("Invert")
            imp.close()
            imp = segmented

        else:
            IJ.run(imp, "Auto Threshold", "method=Li white")  #threshold

        imp = preProcess_(imp)

        #straighten_roi_rotation()
        coords = run_straighten()

        newImp = IJ.getImage()
        """
        fs = FileSaver(newImp)
        fs.saveAsTiff(imgDir + "/straightened" + "/img_" + index + "__000-straight.tif")
        """

        IJ.run("Image Padder",
               "pad_right=" + str(targetWidth - newImp.getWidth()))
        paddedImp = IJ.getImage()
        fs = FileSaver(paddedImp)
        fs.saveAsTiff(imgDir + "/binary" + "/img_" + index +
                      "__000-padded.tif")

        IJ.runMacro("while (nImages>0) {selectImage(nImages);close();}")

        #use same centerline for original greyscale image
        imp = IJ.openImage(filename)
        imp.show()
        IJ.run("Rotate 90 Degrees Left")
        IJ.run("8-bit")
        IJ.runMacro("makeLine(" + coords + ")")
        IJ.run("Straighten...", "line = 80")
        newImp = IJ.getImage()
        IJ.run("Image Padder",
               "pad_right=" + str(targetWidth - newImp.getWidth()))
        paddedImp = IJ.getImage()
        IJ.run("8-bit")
        fs = FileSaver(paddedImp)
        fs.saveAsTiff(imgDir + "/greyscale" + "/img_" + index +
                      "__000-padded.tif")
        IJ.runMacro("while (nImages>0) {selectImage(nImages);close();}")

        index = to_9_Digits(str(int(index) + 1))
        filename = filename = imgDir + "/img_" + index + "__000.tif"
def straighten_roi_rotation(roiWindowsize=8):
    """ Root straightening function that rotates ROI to follow root slope.
    Does not work properly.
    """
    IJ.run("Set Measurements...",
           "mean standard min center redirect=None decimal=3")
    IJ.runMacro("//setTool(\"freeline\");")
    IJ.run("Line Width...", "line=80")
    #numPoints = 512/roiWindowsize
    xvals = []
    yvals = []
    maxvals = []
    counter = 0
    maxIters = 800 / roiWindowsize
    minIters = 10

    imp = IJ.getImage().getProcessor()

    rm = RoiManager()
    if find_first_pixel(0, imp) == None or find_last_pixel(0, imp)[1] == None:
        return
    y = (find_first_pixel(0, imp)[1] + find_last_pixel(0, imp)[1]) / 2
    roi = roiWindow_(imp,
                     center=(roiWindowsize / 2, y),
                     width=roiWindowsize,
                     height=512)
    xvals.append(roiWindowsize / 2)
    yvals.append(y)
    maxvals.append(0)
    roi.findTilt_()
    i = 0
    while i < maxIters and roi.containsRoot_():
        roi.advance_(roiWindowsize)
        IJ.run("Clear Results")
        IJ.run("Measure")
        table = RT.getResultsTable()

        x = RT.getValue(table, "XM", 0)
        y = RT.getValue(table, "YM", 0)
        if imp.getPixel(int(x), int(y)) != 0:
            xvals.append(x)
            yvals.append(y)
            maxvals.append((RT.getValue(table, "Max", 0)))

    #roi.advance_(roiWindowsize)
        print "here"
        roi.unrotateRoot_()
        IJ.run("Clear Results")
        IJ.run("Measure")
        roi.restoreCenter_(RT.getValue(table, "XM", 0),
                           RT.getValue(table, "YM", 0))
        #exit(1)
        sleep(.5)
        roi.findTilt_()
        i += 1
    coords = ""
    for i in range(len(xvals) - 1):
        coords += str(xvals[i]) + ", " + str(yvals[i]) + ", "
    coords += str(xvals[len(xvals) - 1]) + ", " + str(yvals[len(xvals) - 1])

    IJ.runMacro("makeLine(" + coords + ")")
    IJ.run("Straighten...", "line = 80")