def measureTumor(original, locations):
	'''Returns the area from the original image with the 
	highest kurtosis which generally corresponds to the most
	in focus image. Also saves an image corresponding to a mask
	of the measurement.'''
	# Prevent ROI manager from appearing
	roiM = RoiManager(True)
	ParticleAnalyzer.setRoiManager(roiM)
	# Locate particles above a minimum size and with a desired circularity
	IJ.run(locations, "Analyze Particles...", "size=" + str(minimumCancerArea) +"-" + str(maxCancerArea) +" circularity=" + str(circularityCutoff) + "-1.00 show=Nothing exclude add stack");
	# Choose ROI with the highest kurtosis
	maxKurtosis = None
	area = None
	selectedROI = None
	for roi in roiM.getRoisAsArray():
		original.setRoi(roi)
		stats = original.getStatistics(Measurements.KURTOSIS, Measurements.AREA)
		currentKurtosis = stats.kurtosis
		if currentKurtosis > maxKurtosis:
			maxKurtosis = stats.kurtosis
			area = stats.area
			selectedROI = roi
	original.killRoi() # Remove the remaining ROI
	roiM.runCommand("Reset")
	return (area, selectedROI)
Beispiel #2
0
def keep_blobs_bigger_than(imp, min_size_pix=100):
    """remove all blobs other than the largest by area"""
    imp.killRoi()
    rt = ResultsTable()
    if "Size_filtered_" in imp.getTitle():
        title_addition = ""
    else:
        title_addition = "Size_filtered_"
    out_imp = IJ.createImage("{}{}".format(title_addition, imp.getTitle()),
                             imp.getWidth(), imp.getHeight(), 1, 8)
    out_imp.show()
    IJ.run(out_imp, "Select All", "")
    IJ.run(out_imp, "Set...", "value=0 slice")
    mxsz = imp.width * imp.height
    roim = RoiManager()
    pa = ParticleAnalyzer(ParticleAnalyzer.ADD_TO_MANAGER,
                          ParticleAnalyzer.AREA | ParticleAnalyzer.SLICE, rt,
                          min_size_pix, mxsz)
    pa.setRoiManager(roim)
    roim.reset()
    rt.reset()
    pa.analyze(imp)
    rt_areas = rt.getColumn(rt.getColumnIndex("Area")).tolist()
    #	print("Number of cells identified: {}".format(len(rt_areas)));
    for idx in range(len(rt_areas)):
        roim.select(out_imp, idx)
        IJ.run(out_imp, "Set...", "value=255 slice")
    mx_ind = rt_areas.index(max(rt_areas))
    roim.reset()
    roim.close()
    imp.changes = False
    imp.close()
    return out_imp
def keep_largest_blob(imp):
    """remove all blobs other than the largest by area"""
    rt = ResultsTable()
    mxsz = imp.width * imp.height
    roim = RoiManager(False)
    pa = ParticleAnalyzer(ParticleAnalyzer.ADD_TO_MANAGER,
                          ParticleAnalyzer.AREA | ParticleAnalyzer.SLICE, rt,
                          0, mxsz)
    pa.setRoiManager(roim)

    for idx in range(1, imp.getImageStackSize() + 1):
        roim.reset()
        rt.reset()
        imp.setPosition(idx)
        pa.analyze(imp)
        rt_areas = rt.getColumn(rt.getColumnIndex("Area")).tolist()
        mx_ind = rt_areas.index(max(rt_areas))
        indices_to_remove = [
            a for a in range(0, len(rt_areas)) if a != mx_ind
        ]
        indices_to_remove.reverse()
        for rem_idx in indices_to_remove:
            roim.select(imp, rem_idx)
            IJ.run(imp, "Set...", "value=0 slice")
    imp.killRoi()
    roim.reset()
    roim.close()
def particleRemover(image, minArea):
	'''Send in a thresholded image, remove the small spots'''
	numSlices = image.getImageStackSize()
	particleRoiM = RoiManager(True)
	ParticleAnalyzer.setRoiManager(particleRoiM)
	IJ.run(image, "Analyze Particles...", "size=0-" + str(minArea) + " circularity=0.00-1.00 include show=Nothing add stack");
	# Set the fill color to black before filling
	IJ.setForegroundColor(0, 0, 0);
	particleRoiM.runCommand("Fill");
	particleRoiM.runCommand("Reset");
Beispiel #5
0
def generate_cell_rois(seg_binary_imp):
    """generate rois from which cell shape information will be gleaned"""
    seg_binary_imp.killRoi()
    mxsz = seg_binary_imp.width * seg_binary_imp.height
    roim = RoiManager(False)
    pa_options = ParticleAnalyzer.AREA | ParticleAnalyzer.PERIMETER | ParticleAnalyzer.SHAPE_DESCRIPTORS
    pa = ParticleAnalyzer(ParticleAnalyzer.ADD_TO_MANAGER, pa_options, None,
                          1000, mxsz)
    pa.setRoiManager(roim)
    roim.reset()
    pa.analyze(seg_binary_imp)
    rois = roim.getRoisAsArray()
    roim.reset()
    roim.close()
    return rois
def process(inputpath, outputpath):

    imp = IJ.openImage(inputpath)
    IJ.run(
        imp, "Properties...",
        "channels=1 slices=1 frames=1 unit=um pixel_width=0.8777017 pixel_height=0.8777017 voxel_depth=25400.0508001"
    )

    IJ.setThreshold(imp, t1, 255)
    #imp.show()
    #WaitForUserDialog("Title", "Look at image").show()
    IJ.run(imp, "Convert to Mask", "")
    IJ.run(imp, "Watershed", "")

    # Counts and measures the area of particles and adds them to a table called areas. Also adds them to the ROI manager

    table = ResultsTable()
    roim = RoiManager(True)
    ParticleAnalyzer.setRoiManager(roim)
    pa = ParticleAnalyzer(ParticleAnalyzer.ADD_TO_MANAGER, Measurements.AREA,
                          table, 50, 9999999999999999, 0.2, 1.0)
    pa.setHideOutputImage(True)
    pa.analyze(imp)

    imp.changes = False
    imp.close()

    areas = table.getColumn(0)

    summary = {}

    if areas:

        summary['Image'] = filename
        summary['Nuclei.count'] = len(areas)
        summary['Area.Covered'] = sum(areas)

    fieldnames = list(summary.keys())

    with open(outputpath, 'a') as csvfile:

        writer = csv.DictWriter(csvfile,
                                fieldnames=fieldnames,
                                extrasaction='ignore',
                                lineterminator='\n')
        if os.path.getsize(outputDirectory + "/" + outputname + ".csv") < 1:
            writer.writeheader()
        writer.writerow(summary)
Beispiel #7
0
def get_no_nuclei_fully_enclosed(roi, full_nuclei_imp, overlap_threshold=0.65):
    """for a given cell roi and ImagePlus with binary nuclei, return how many nuclei lie ENTIRELY within the cell"""
    bbox = roi.getBounds()
    full_nuclei_imp.setRoi(roi)
    cropped_nuc_imp = full_nuclei_imp.crop()
    roi.setLocation(0, 0)
    cropped_nuc_imp.setRoi(roi)
    cropped_nuc_imp.killRoi()
    roim = RoiManager(False)
    mxsz = cropped_nuc_imp.getWidth() * cropped_nuc_imp.getHeight()
    pa = ParticleAnalyzer(
        ParticleAnalyzer.ADD_TO_MANAGER, ParticleAnalyzer.AREA
        | ParticleAnalyzer.SLICE | ParticleAnalyzer.CENTROID, None, 0, mxsz)
    pa.setRoiManager(roim)
    pa.analyze(cropped_nuc_imp)
    cell_imp = IJ.createImage("Cell binary", cropped_nuc_imp.getWidth(),
                              cropped_nuc_imp.getHeight(), 1, 8)
    IJ.run(cell_imp, "Select All", "")
    IJ.run(cell_imp, "Set...", "value=0 slice")
    cell_imp.setRoi(roi)
    IJ.run(cell_imp, "Set...", "value=255 slice")
    no_enclosed_nuclei = 0
    for idx, nuc_roi in enumerate(roim.getRoisAsArray()):
        test_imp = Duplicator().run(cell_imp)
        test_imp.setRoi(nuc_roi)
        IJ.run(test_imp, "Set...", "value=255 slice")
        test_imp.killRoi()
        IJ.run(test_imp, "Create Selection", "")
        IJ.run(test_imp, "Make Inverse", "")
        test_roi = test_imp.getRoi()
        test_roi_stats = test_roi.getStatistics()
        cell_roi_stats = roi.getStatistics()
        nuc_roi_stats = nuc_roi.getStatistics()
        if test_roi_stats.area < (
                cell_roi_stats.area +
            (1 - overlap_threshold) * nuc_roi_stats.area
        ):  # i.e. if more than (100*overlap_threshold)% of nucleus is inside cell...
            no_enclosed_nuclei += 1
        test_imp.changes = False
        test_imp.close()
    roi.setLocation(bbox.getX(), bbox.getY())
    cropped_nuc_imp.changes = False
    cropped_nuc_imp.close()
    cell_imp.changes = False
    cell_imp.close()
    return no_enclosed_nuclei
Beispiel #8
0
def countParticles(imp, roim, minSize, maxSize, minCircularity,
                   maxCircularity):
    # Create a table to store the results
    table = ResultsTable()

    # Create the particle analyzer
    pa = ParticleAnalyzer(
        ParticleAnalyzer.ADD_TO_MANAGER,
        Measurements.AREA | Measurements.MEAN | Measurements.ELLIPSE, table,
        minSize, maxSize, minCircularity, maxCircularity)
    pa.setRoiManager(roim)
    pa.setHideOutputImage(True)

    if pa.analyze(imp):
        print "All ok"
    else:
        print "There was a problem in analyzing", blobs

    areas = table.getColumn(0)
    intensities = table.getColumn(1)
    majors = table.getColumn(2)
def countParticles(imp, roim, minSize, maxSize, minCircularity, maxCircularity):
	# Create a table to store the results
	table = ResultsTable()
	
	# Create the particle analyzer
	pa = ParticleAnalyzer(ParticleAnalyzer.ADD_TO_MANAGER, Measurements.AREA|Measurements.MEAN, table, minSize, maxSize, minCircularity, maxCircularity)
	#pa = ParticleAnalyzer(ParticleAnalyzer.ADD_TO_MANAGER, Measurements.AREA|Measurements.MEAN, table, 10, Double.POSITIVE_INFINITY, 0.5, 1.0)
	#pa = ParticleAnalyzer(ParticleAnalyzer.ADD_TO_MANAGER, Measurements.AREA|Measurements.MEAN, table, 5, 6, 0.5, 1.0)
	pa.setRoiManager(roim)
	pa.setHideOutputImage(True)

	if pa.analyze(imp):
		print "All ok"
	else:
 		print "There was a problem in analyzing", blobs

 	areas = table.getColumn(0)
	intensities = table.getColumn(1)

	if ( (areas!=None) and (intensities!=None)):
 		for area, intensity in zip(areas,intensities): print str(area)+": "+str(intensity)
Beispiel #10
0
def AnalyzeParticle(IMP):
    rm = RoiManager().getInstance2()
    rt = ResultsTable()

    #再現性確保のために最終的には実装
    #IJ.run("Set Measurements...","area  centroid fit redirect=None decimal=3")

    #https://imagej.nih.gov/ij/developer/api/constant-values.html#ij.plugin.filter.ParticleAnalyzer.SHOW_RESULTS
    #表示オプション無し、resultは全部選択
    PA = ParticleAnalyzer(0 , 1043199 , rt, 10000, 300000, 0.2, 1.0)
    PA.setRoiManager(rm)
    PA.analyze(IMP)

    #IJ.run(IMP, "Analyze Particles...", "display clear include add")
    rm.runCommand("Save", "C:/Users/For  Programming/Documents/Python Scripts/OutletHDD/aaa.zip")
    rt.saveAs("C:/Users/For  Programming/Documents/Python Scripts/OutletHDD/aaa.csv")


    #最後に全ての結果をCloseする。
    #写真を先に消さないとバグる。
    IMP.close()
    rm.close()
    rt.reset()
Beispiel #11
0
# 1. options (could be SHOW_ROI_MASKS, SHOW_OUTLINES, SHOW_MASKS, SHOW_NONE, ADD_TO_MANAGER, and others; combined with bitwise-or)
# 2. measurement options (see [http://imagej.net/developer/api/ij/measure/Measurements.html Measurements])
# 3. a ResultsTable to store the measurements
# 4. The minimum size of a particle to consider for measurement
# 5. The maximum size (idem)
# 6. The minimum circularity of a particle
# 7. The maximum circularity

minSize = 30.0
maxSize = 10000.0
opts = ParticleAnalyzer.EXCLUDE_EDGE_PARTICLES | ParticleAnalyzer.SHOW_OVERLAY_OUTLINES
print(opts)
meas = Measurements.AREA | Measurements.MEAN | Measurements.CENTER_OF_MASS
print(meas)
pa = ParticleAnalyzer(opts, meas, results_table, minSize, maxSize)
# pa.setHideOutputImage(False)
pa.setRoiManager(roim)

if pa.analyze(imp_work):
    imp_out = pa.getOutputImage()
    # imp_out.show()
    roim.runCommand(blobs, "Show All with labels")
    blobs.show()
    results_table.show("Results")
    roim.show()
    print "All ok"
else:
    print "There was a problem in analyzing", blobs

# The measured areas are listed in the first column of the results table, as a float array:
areas = results_table.getColumn(0)
Beispiel #12
0
def process(subDir, subsubDir, outputDirectory, filename):

    subFolder = subDir + "/" + subsubDir

    # Opens the d0 image and sets default properties

    imp = IJ.openImage(inputDirectory + subFolder + '/' + filename)
    IJ.run(
        imp, "Properties...",
        "channels=1 slices=1 frames=1 unit=um pixel_width=0.8777017 pixel_height=0.8777017 voxel_depth=25400.0508001"
    )

    # Sets the threshold and watersheds. for more details on image processing, see https://imagej.nih.gov/ij/developer/api/ij/process/ImageProcessor.html

    ic = ImageConverter(imp)
    ic.convertToGray8()
    imp.updateAndDraw()
    dup = imp.duplicate()
    IJ.run(
        dup, "Convolve...",
        "text1=[-1 -1 -1 -1 -1\n-1 -1 -1 -1 -1\n-1 -1 24 -1 -1\n-1 -1 -1 -1 -1\n-1 -1 -1 -1 -1\n] normalize"
    )
    stats = dup.getStatistics(Measurements.MEAN | Measurements.MIN_MAX
                              | Measurements.STD_DEV)
    dup.close()
    blurry = (stats.mean < 18 and stats.stdDev < 22) or stats.max < 250

    IJ.setThreshold(imp, lowerBounds[0], 255)

    IJ.run(imp, "Convert to Mask", "")
    IJ.run(imp, "Watershed", "")
    if displayImages:
        imp.show()
        WaitForUserDialog("Title", "Look at image").show()

    # Counts and measures the area of particles and adds them to a table called areas. Also adds them to the ROI manager

    table = ResultsTable()
    roim = RoiManager(True)
    ParticleAnalyzer.setRoiManager(roim)
    pa = ParticleAnalyzer(ParticleAnalyzer.ADD_TO_MANAGER, Measurements.AREA,
                          table, 15, 9999999999999999, 0.2, 1.0)
    pa.setHideOutputImage(True)
    pa.analyze(imp)

    if not displayImages:
        imp.changes = False
        imp.close()

    areas = table.getColumn(0)

    # This loop goes through the remaining channels for the other markers, by replacing the d0 at the end with its corresponding channel
    # It will save all the area fractions into a 2d array called areaFractionsArray

    areaFractionsArray = []
    areaMeansArray = []
    means = []
    totalAreas = []
    for chan in channels:
        v, x = chan
        # Opens each image and thresholds

        imp = IJ.openImage(inputDirectory + subFolder + '/' +
                           filename.replace("d0.TIF", "d" + str(x) + ".TIF"))
        IJ.run(
            imp, "Properties...",
            "channels=1 slices=1 frames=1 unit=um pixel_width=0.8777017 pixel_height=0.8777017 voxel_depth=25400.0508001"
        )
        ic = ImageConverter(imp)
        ic.convertToGray8()
        imp.updateAndDraw()

        stats = imp.getStatistics(Measurements.MEAN)
        means.append(stats.mean)

        areaMeans = []
        for roi in roim.getRoisAsArray():
            imp.setRoi(roi)
            stats = imp.getStatistics(Measurements.MEAN)
            areaMeans.append(stats.mean)

        IJ.setThreshold(imp, lowerBounds[x], 255)
        IJ.run(imp, "Convert to Mask", "")

        if displayImages:
            imp.show()
            WaitForUserDialog("Title", "Look at image").show()

        stats = imp.getStatistics(Measurements.AREA_FRACTION)
        totalAreas.append(stats.areaFraction)

        # Measures the area fraction of the new image for each ROI from the ROI manager.
        areaFractions = []

        for roi in roim.getRoisAsArray():
            imp.setRoi(roi)
            stats = imp.getStatistics(Measurements.AREA_FRACTION)
            areaFractions.append(stats.areaFraction)

        # Saves the results in areaFractionArray

        areaFractionsArray.append(areaFractions)
        areaMeansArray.append(sum(areaMeans) / len(areaMeans))

        if not displayImages:
            imp.changes = False
            imp.close()
    roim.close()

    # Figures out what well the image is a part of

    ind = filename.index("p00_0_")
    row = filename[ind + 6:ind + 7]
    column = str(int(filename[ind + 7:ind + 9]))

    # Creates the summary dictionary which will correspond to a single row in the output csv, with each key being a column

    summary = {}

    # Finds the name of the well from the nameArray 2d array

    if row in nameArray:
        if column in nameArray[row]:
            summary['Name'] = nameArray[row][column]

    summary['Image'] = filename
    summary['Directory'] = subDir
    summary['SubDirectory'] = subsubDir
    summary['Row'] = row
    summary['Column'] = column

    # Adds usual columns

    summary['size-average'] = 0
    summary['#nuclei'] = 0
    summary['all-negative'] = 0

    summary['too-big-(>' + str(tooBigThreshold) + ')'] = 0
    summary['too-small-(<' + str(tooSmallThreshold) + ')'] = 0

    summary['image-quality'] = blurry

    # Creates the fieldnames variable needed to create the csv file at the end.

    fieldnames = [
        'Name', 'Directory', 'SubDirectory', 'Image', 'Row', 'Column',
        'size-average', 'image-quality',
        'too-big-(>' + str(tooBigThreshold) + ')',
        'too-small-(<' + str(tooSmallThreshold) + ')', '#nuclei',
        'all-negative'
    ]

    # Adds the columns for each individual marker (ignoring Dapi since it was used to count nuclei)

    for chan in channels:
        v, x = chan
        summary[v + "-positive"] = 0
        summary[v + "-intensity"] = means[x]
        summary[v + "-area"] = totalAreas[x]
        summary[v + "-intensity-in-nuclei"] = areaMeansArray[x]
        summary[v + "-area-fraction-in-nuclei"] = sum(
            areaFractionsArray[x]) / len(areaFractionsArray[x])
        fieldnames.append(v + "-positive")
        fieldnames.append(v + "-intensity")
        fieldnames.append(v + "-area")
        fieldnames.append(v + "-intensity-in-nuclei")
        fieldnames.append(v + "-area-fraction-in-nuclei")

    # Adds the column for colocalization between first and second marker

    if len(channels) > 2:
        summary[channels[1][0] + '-' + channels[2][0] + '-positive'] = 0
        fieldnames.append(channels[1][0] + '-' + channels[2][0] + '-positive')

    # Adds the columns for colocalization between all three markers

    if len(channels) > 3:
        summary[channels[1][0] + '-' + channels[3][0] + '-positive'] = 0
        summary[channels[2][0] + '-' + channels[3][0] + '-positive'] = 0
        summary[channels[1][0] + '-' + channels[2][0] + '-' + channels[3][0] +
                '-positive'] = 0

        fieldnames.append(channels[1][0] + '-' + channels[3][0] + '-positive')
        fieldnames.append(channels[2][0] + '-' + channels[3][0] + '-positive')
        fieldnames.append(channels[1][0] + '-' + channels[2][0] + '-' +
                          channels[3][0] + '-positive')

    # Loops through each particle and adds it to each field that it is True for.

    areaCounter = 0

    if not (areas is None):
        for z, area in enumerate(areas):
            if not (area is None or summary is None):
                if area > tooBigThreshold:
                    summary['too-big-(>' + str(tooBigThreshold) + ')'] += 1
                elif area < tooSmallThreshold:
                    summary['too-small-(<' + str(tooSmallThreshold) + ')'] += 1
                else:

                    summary['#nuclei'] += 1
                    areaCounter += area

                    temp = 0
                    for y, chan in enumerate(channels):
                        v, x = chan
                        if areaFractionsArray[y][z] > areaFractionThreshold:
                            summary[chan[0] + '-positive'] += 1
                            if x != 0:
                                temp += 1

                    if temp == 0:
                        summary['all-negative'] += 1

                    if len(channels) > 2:
                        if areaFractionsArray[1][z] > areaFractionThreshold:
                            if areaFractionsArray[2][z] > areaFractionThreshold:
                                summary[channels[1][0] + '-' + channels[2][0] +
                                        '-positive'] += 1

                    if len(channels) > 3:
                        if areaFractionsArray[1][z] > areaFractionThreshold:
                            if areaFractionsArray[3][z] > areaFractionThreshold:
                                summary[channels[1][0] + '-' + channels[3][0] +
                                        '-positive'] += 1
                        if areaFractionsArray[2][z] > areaFractionThreshold:
                            if areaFractionsArray[3][z] > areaFractionThreshold:
                                summary[channels[2][0] + '-' + channels[3][0] +
                                        '-positive'] += 1
                                if areaFractionsArray[1][
                                        z] > areaFractionThreshold:
                                    summary[channels[1][0] + '-' +
                                            channels[2][0] + '-' +
                                            channels[3][0] + '-positive'] += 1

    # Calculate the average of the particles sizes

    if float(summary['#nuclei']) > 0:
        summary['size-average'] = round(areaCounter / summary['#nuclei'], 2)

    # Opens and appends one line on the final csv file for the subfolder (remember that this is still inside the loop that goes through each image)

    with open(outputDirectory + "/" + outputName + ".csv", 'a') as csvfile:

        writer = csv.DictWriter(csvfile,
                                fieldnames=fieldnames,
                                extrasaction='ignore',
                                lineterminator='\n')
        if os.path.getsize(outputDirectory + "/" + outputName + ".csv") < 1:
            writer.writeheader()
        writer.writerow(summary)
Beispiel #13
0
def merge_incorrect_splits_and_get_centroids(imp,
                                             centroid_distance_limit=100,
                                             size_limit=100):
    """if particles are found with centroids closer than centroid_distance_limit and both have size<size_limit, get average centroid"""
    imp.killRoi()
    rt = ResultsTable()
    out_imp = IJ.createImage("Nuclei centroids from {}".format(imp.getTitle()),
                             imp.getWidth(), imp.getHeight(), 1, 8)
    out_imp.show()
    IJ.run(out_imp, "Select All", "")
    IJ.run(out_imp, "Set...", "value=0 slice")
    out_imp.show()
    cal = imp.getCalibration()
    mxsz = imp.width * cal.pixelWidth * imp.height * cal.pixelHeight
    print("mxsz = {}".format(mxsz))
    roim = RoiManager()
    imp.show()
    pa = ParticleAnalyzer(
        ParticleAnalyzer.ADD_TO_MANAGER, ParticleAnalyzer.AREA
        | ParticleAnalyzer.SLICE | ParticleAnalyzer.CENTROID, rt, 0,
        size_limit)
    pa.setRoiManager(roim)
    roim.reset()
    rt.reset()
    pa.analyze(imp)
    MyWaitForUser("paise",
                  "pause post-merge incorrect splits particel analysis")
    rt_xs = rt.getColumn(rt.getColumnIndex("X")).tolist()
    rt_ys = rt.getColumn(rt.getColumnIndex("Y")).tolist()
    centroids = [(x, y) for x, y in zip(rt_xs, rt_ys)]
    print("centroids = {}".format(centroids))
    centroids_set = set()
    for c in centroids:
        ds = [
            math.sqrt((c[0] - cx)**2 + (c[1] - cy)**2)
            for (cx, cy) in centroids
        ]
        close_mask = [d < centroid_distance_limit for d in ds]
        # if no other centroids are within centroid_distance_limit, add this centroid to the output set
        # otherwise, add the average position of this centroid and those within centroid_distance_limit to the output set
        centroids_set.add(
            (sum([msk * b[0]
                  for msk, b in zip(close_mask, centroids)]) / sum(close_mask),
             sum([msk * b[1] for msk, b in zip(close_mask, centroids)]) /
             sum(close_mask)))
    roim.reset()
    rt.reset()
    pa = ParticleAnalyzer(
        ParticleAnalyzer.ADD_TO_MANAGER, ParticleAnalyzer.AREA
        | ParticleAnalyzer.SLICE | ParticleAnalyzer.CENTROID, rt, size_limit,
        mxsz)
    pa.setRoiManager(roim)
    pa.analyze(imp)
    MyWaitForUser("paise",
                  "pause post-merge incorrect splits particel analysis 2")
    if rt.columnExists("X"):
        rt_xs = rt.getColumn(rt.getColumnIndex("X")).tolist()
        rt_ys = rt.getColumn(rt.getColumnIndex("Y")).tolist()
    centroids = [(x, y) for x, y in zip(rt_xs, rt_ys)]
    for c in centroids:
        centroids_set.add(c)
    centroids = list(centroids_set)
    cal = imp.getCalibration()
    centroids = [(c[0] / cal.pixelWidth, c[1] / cal.pixelHeight)
                 for c in centroids]
    print("new number of nuclei identified = {}".format(len(centroids)))
    roim.reset()
    roim.close()
    for idx, c in enumerate(centroids):
        roi = OvalRoi(c[0], c[1], 10, 10)
        out_imp.setRoi(roi)
        IJ.run(out_imp, "Set...", "value={} slice".format(idx + 1))
    imp.changes = False
    #imp.close();
    return out_imp
from ij.measure import ResultsTable
from ij.measure import Measurements
from ij.plugin.frame import RoiManager
from ij.plugin.filter import ParticleAnalyzer
from ij import IJ

# get active image
imp=IJ.getImage()

# set up first ROI manager
table1 = ResultsTable()
roim1=RoiManager()
pa1 = ParticleAnalyzer(ParticleAnalyzer.ADD_TO_MANAGER, Measurements.AREA|Measurements.MEAN|Measurements.ELLIPSE, table1, 0, 100, 0, 1)
pa1.setRoiManager(roim1)
pa1.analyze(imp)

# set up second ROI manager
table2 = ResultsTable()
# Pass true to second ROI manager so it will not be seen
roim2=RoiManager(True)
pa2 = ParticleAnalyzer(ParticleAnalyzer.ADD_TO_MANAGER, Measurements.AREA|Measurements.MEAN|Measurements.ELLIPSE, table2, 100, 500, 0, 1)
pa2.setRoiManager(roim2)
pa2.analyze(imp)

print "rois from first manager:"
for roi in roim1.getRoisAsArray(): print roi

print 
print "rois from second manager:"
for roi in roim2.getRoisAsArray(): print roi
Beispiel #15
0
imgMask = WindowManager.getImage(d.getNextChoice())
img470 = WindowManager.getImage(d.getNextChoice())
img410 = WindowManager.getImage(d.getNextChoice())

binning = d.getNextRadioButton()

PARENT_DIR = img410.getOriginalFileInfo().directory

# setup data collection object
dataTable = ResultsTable()

##################################################
# INITIAL MEASUREMENTS
table = ResultsTable()
roiManager = RoiManager(True) # Boolean => Don't Display
PA.setRoiManager(roiManager)

# Measure Median 410 and 470
addValues(dataTable, 'Median 410', getMedians(img410))
addValues(dataTable, 'Median 470', getMedians(img470))

# Make the ROIs based on the mask
IJ.setThreshold(imgMask, 1, 255, "Red")
IJ.run(imgMask, "Analyze Particles...", "size=200 exclude add stack")
roiManager.runCommand("Show None")

maskStk = imgMask.getStack()
im410Stk = img410.getStack()
im470Stk = img470.getStack()

areas = []
def generate_background_rois(input_mask_imp,
                             params,
                             membrane_edges,
                             dilations=5,
                             threshold_method=None,
                             membrane_imp=None):
    """automatically identify background region based on auto-thresholded image, existing membrane edges and position of midpoint anchor"""
    if input_mask_imp is None and membrane_imp is not None:
        segmentation_imp = Duplicator().run(membrane_imp)
        # do thresholding using either previous method if threhsold_method is None or using (less conservative?) threshold method
        if (threshold_method is None
                or not (threshold_method in params.listThresholdMethods())):
            mask_imp = make_and_clean_binary(segmentation_imp,
                                             params.threshold_method)
        else:
            mask_imp = make_and_clean_binary(segmentation_imp,
                                             threshold_method)
        segmentation_imp.close()
    else:
        input_mask_imp.killRoi()
        mask_imp = Duplicator().run(input_mask_imp)

    rois = []
    IJ.setForegroundColor(0, 0, 0)
    roim = RoiManager(True)
    rt = ResultsTable()

    for fridx in range(mask_imp.getNFrames()):
        mask_imp.setT(fridx + 1)
        # add extra bit to binary mask from loaded membrane in case user refined edges...
        # flip midpoint anchor across the line joining the two extremes of the membrane,
        # and fill in the triangle made by this new point and those extremes
        poly = membrane_edges[fridx].getPolygon()
        l1 = (poly.xpoints[0], poly.ypoints[0])
        l2 = (poly.xpoints[-1], poly.ypoints[-1])
        M = (0.5 * (l1[0] + l2[0]), 0.5 * (l1[1] + l2[1]))
        Mp1 = (params.manual_anchor_midpoint[0][0] - M[0],
               params.manual_anchor_midpoint[0][1] - M[1])
        p2 = (M[0] - Mp1[0], M[1] - Mp1[1])
        new_poly_x = list(poly.xpoints)
        new_poly_x.append(p2[0])
        new_poly_y = list(poly.ypoints)
        new_poly_y.append(p2[1])
        mask_imp.setRoi(PolygonRoi(new_poly_x, new_poly_y, PolygonRoi.POLYGON))
        IJ.run(mask_imp, "Fill", "slice")
        mask_imp.killRoi()

        # now dilate the masked image and identify the unmasked region closest to the midpoint anchor
        ip = mask_imp.getProcessor()
        dilations = 5
        for d in range(dilations):
            ip.dilate()
        ip.invert()
        mask_imp.setProcessor(ip)
        mxsz = mask_imp.getWidth() * mask_imp.getHeight()
        pa = ParticleAnalyzer(
            ParticleAnalyzer.ADD_TO_MANAGER | ParticleAnalyzer.SHOW_PROGRESS,
            ParticleAnalyzer.CENTROID, rt, 0, mxsz)
        pa.setRoiManager(roim)
        pa.analyze(mask_imp)
        ds_to_anchor = [
            math.sqrt((x - params.manual_anchor_midpoint[0][0])**2 +
                      (y - params.manual_anchor_midpoint[0][1])**2)
            for x, y in zip(
                rt.getColumn(rt.getColumnIndex("X")).tolist(),
                rt.getColumn(rt.getColumnIndex("Y")).tolist())
        ]
        if len(ds_to_anchor) > 0:
            roi = roim.getRoi(ds_to_anchor.index(min(ds_to_anchor)))
            rois.append(roi)
        else:
            rois.append(None)
        roim.reset()
        rt.reset()
    roim.close()
    mask_imp.close()
    return rois
Beispiel #17
0
def process(subFolder, outputDirectory, filename):
    #IJ.close()
    imp = IJ.openImage(inputDirectory + subFolder + '/' +
                       rreplace(filename, "_ch00.tif", ".tif"))
    imp.show()

    # Get the pixel values from the xml file
    for file in os.listdir(inputDirectory + subFolder):
        if file.endswith('.xml'):
            xml = os.path.join(inputDirectory + subFolder, file)
            xml = "C:/Users/Harris/Desktop/test_xml_for_parsing_pixel.xml"
            element_tree = ET.parse(xml)
            root = element_tree.getroot()
            for dimensions in root.iter('DimensionDescription'):
                num_pixels = int(dimensions.attrib['NumberOfElements'])
                if dimensions.attrib['Unit'] == "m":
                    length = float(dimensions.attrib['Length']) * 1000000
                else:
                    length = float(dimensions.attrib['Length'])
            pixel_length = length / num_pixels
        else:
            pixel_length = 0.8777017

    IJ.run(
        imp, "Properties...",
        "channels=1 slices=1 frames=1 unit=um pixel_width=" +
        str(pixel_length) + " pixel_height=" + str(pixel_length) +
        " voxel_depth=25400.0508001")
    ic = ImageConverter(imp)
    ic.convertToGray8()
    #IJ.setThreshold(imp, 2, 255)

    #Automatically selects the area of the organoid based on automated thresholding and creates a mask to be applied on
    #all other images

    IJ.setAutoThreshold(imp, "Mean dark no-reset")
    IJ.run(imp, "Convert to Mask", "")
    IJ.run(imp, "Analyze Particles...", "size=100000-Infinity add select")
    rm = RoiManager.getInstance()
    num_roi = rm.getCount()

    for i in num_roi:

        imp = getCurrentImage()
        rm.select(imp, i)
        IJ.setBackgroundColor(0, 0, 0)
        IJ.run(imp, "Clear Outside", "")

        IJ.run(imp, "Convert to Mask", "")
        IJ.run(imp, "Remove Outliers...",
               "radius=5" + " threshold=50" + " which=Dark")
        IJ.run(imp, "Remove Outliers...",
               "radius=5" + " threshold=50" + " which=Bright")

        # Save the mask and open it
        IJ.saveAs("tiff", inputDirectory + '/mask' + i)
        mask = IJ.openImage(inputDirectory + '/mask' + i + '.tif')

        if not displayImages:
            imp.changes = False
            imp.close()

        images = [None] * 5
        intensities = [None] * 5
        blobsarea = [None] * 5
        blobsnuclei = [None] * 5
        bigAreas = [None] * 5

        imp.close()

        # Loop to open all the channel images
        for chan in channels:
            v, x = chan
            images[x] = IJ.openImage(inputDirectory + subFolder + '/' +
                                     rreplace(filename, "_ch00.tif", "_ch0" +
                                              str(x) + ".tif"))

            # Apply Mask on all the images and save them into an array
            apply_mask = ImageCalculator()
            images[x] = apply_mask.run("Multiply create 32 bit", mask,
                                       images[x])
            ic = ImageConverter(images[x])
            ic.convertToGray8()
            imp = images[x]

            # Calculate the intensities for each channel as well as the organoid area
            for roi in rm.getRoisAsArray():
                imp.setRoi(roi)
                stats_i = imp.getStatistics(Measurements.MEAN
                                            | Measurements.AREA)
                intensities[x] = stats_i.mean
                bigAreas[x] = stats_i.area

        rm.close()

        # Opens the ch00 image and sets default properties

        #Get the pixel values from the xml file
        for file in os.listdir(subFolder):
            if file.endswith('.xml'):
                xml = os.path.join(inputDirectory + subFolder, file)
                xml = "C:/Users/Harris/Desktop/test_xml_for_parsing_pixel.xml"
                element_tree = ET.parse(xml)
                root = element_tree.getroot()
                for dimensions in root.iter('DimensionDescription'):
                    num_pixels = int(dimensions.attrib['NumberOfElements'])
                    if dimensions.attrib['Unit'] == "m":
                        length = float(dimensions.attrib['Length']) * 1000000
                    else:
                        length = float(dimensions.attrib['Length'])
                pixel_length = length / num_pixels
            else:
                pixel_length = 0.8777017

        imp = IJ.openImage(inputDirectory + subFolder + '/' + filename)
        imp = apply_mask.run("Multiply create 32 bit", mask, imp)
        IJ.run(
            imp, "Properties...",
            "channels=1 slices=1 frames=1 unit=um pixel_width=" +
            str(pixel_length) + "pixel_height=" + str(pixel_length) +
            "voxel_depth=25400.0508001")

        # Sets the threshold and watersheds. for more details on image processing, see https://imagej.nih.gov/ij/developer/api/ij/process/ImageProcessor.html

        ic = ImageConverter(imp)
        ic.convertToGray8()

        IJ.run(imp, "Remove Outliers...",
               "radius=2" + " threshold=50" + " which=Dark")

        IJ.run(imp, "Gaussian Blur...", "sigma=" + str(blur))

        IJ.setThreshold(imp, lowerBounds[0], 255)

        if displayImages:
            imp.show()
        IJ.run(imp, "Convert to Mask", "")
        IJ.run(imp, "Watershed", "")

        if not displayImages:
            imp.changes = False
            imp.close()

        # Counts and measures the area of particles and adds them to a table called areas. Also adds them to the ROI manager

        table = ResultsTable()
        roim = RoiManager(True)
        ParticleAnalyzer.setRoiManager(roim)
        pa = ParticleAnalyzer(ParticleAnalyzer.ADD_TO_MANAGER,
                              Measurements.AREA, table, 15, 9999999999999999,
                              0.2, 1.0)
        pa.setHideOutputImage(True)
        # imp = impM

        # imp.getProcessor().invert()
        pa.analyze(imp)

        areas = table.getColumn(0)

        # This loop goes through the remaining channels for the other markers, by replacing the ch00 at the end with its corresponding channel
        # It will save all the area fractions into a 2d array called areaFractionsArray

        areaFractionsArray = [None] * 5
        for chan in channels:
            v, x = chan
            # Opens each image and thresholds

            imp = images[x]
            IJ.run(
                imp, "Properties...",
                "channels=1 slices=1 frames=1 unit=um pixel_width=0.8777017 pixel_height=0.8777017 voxel_depth=25400.0508001"
            )

            ic = ImageConverter(imp)
            ic.convertToGray8()
            IJ.setThreshold(imp, lowerBounds[x], 255)

            if displayImages:
                imp.show()
                WaitForUserDialog("Title",
                                  "Adjust Threshold for Marker " + v).show()

            IJ.run(imp, "Convert to Mask", "")

            # Measures the area fraction of the new image for each ROI from the ROI manager.
            areaFractions = []
            for roi in roim.getRoisAsArray():
                imp.setRoi(roi)
                stats = imp.getStatistics(Measurements.AREA_FRACTION)
                areaFractions.append(stats.areaFraction)

            # Saves the results in areaFractionArray

            areaFractionsArray[x] = areaFractions

        roim.close()

        for chan in channels:
            v, x = chan

            imp = images[x]
            imp.deleteRoi()
            roim = RoiManager(True)
            ParticleAnalyzer.setRoiManager(roim)
            pa = ParticleAnalyzer(ParticleAnalyzer.ADD_TO_MANAGER,
                                  Measurements.AREA, table, 15,
                                  9999999999999999, 0.2, 1.0)
            pa.analyze(imp)

            blobs = []
            for roi in roim.getRoisAsArray():
                imp.setRoi(roi)
                stats = imp.getStatistics(Measurements.AREA)
                blobs.append(stats.area)

            blobsarea[x] = sum(
                blobs
            )  #take this out and use intial mask tissue area from the beginning
            blobsnuclei[x] = len(blobs)

            if not displayImages:
                imp.changes = False
                imp.close()
            roim.reset()
            roim.close()

            imp.close()

    # Creates the summary dictionary which will correspond to a single row in the output csv, with each key being a column

    summary = {}

    summary['Image'] = filename
    summary['Directory'] = subFolder

    # Adds usual columns

    summary['size-average'] = 0
    summary['#nuclei'] = 0
    summary['all-negative'] = 0

    summary['too-big-(>' + str(tooBigThreshold) + ')'] = 0
    summary['too-small-(<' + str(tooSmallThreshold) + ')'] = 0

    # Creates the fieldnames variable needed to create the csv file at the end.

    fieldnames = [
        'Name', 'Directory', 'Image', 'size-average',
        'too-big-(>' + str(tooBigThreshold) + ')',
        'too-small-(<' + str(tooSmallThreshold) + ')', '#nuclei',
        'all-negative'
    ]

    # Adds the columns for each individual marker (ignoring Dapi since it was used to count nuclei)

    summary["organoid-area"] = bigAreas[x]
    fieldnames.append("organoid-area")

    for chan in channels:
        v, x = chan
        summary[v + "-positive"] = 0
        fieldnames.append(v + "-positive")

        summary[v + "-intensity"] = intensities[x]
        fieldnames.append(v + "-intensity")

        summary[v + "-blobsarea"] = blobsarea[x]
        fieldnames.append(v + "-blobsarea")

        summary[v + "-blobsnuclei"] = blobsnuclei[x]
        fieldnames.append(v + "-blobsnuclei")

    # Adds the column for colocalization between first and second marker

    if len(channels) > 2:
        summary[channels[1][0] + '-' + channels[2][0] + '-positive'] = 0
        fieldnames.append(channels[1][0] + '-' + channels[2][0] + '-positive')

    # Adds the columns for colocalization between all three markers

    if len(channels) > 3:
        summary[channels[1][0] + '-' + channels[3][0] + '-positive'] = 0
        summary[channels[2][0] + '-' + channels[3][0] + '-positive'] = 0
        summary[channels[1][0] + '-' + channels[2][0] + '-' + channels[3][0] +
                '-positive'] = 0

        fieldnames.append(channels[1][0] + '-' + channels[3][0] + '-positive')
        fieldnames.append(channels[2][0] + '-' + channels[3][0] + '-positive')
        fieldnames.append(channels[1][0] + '-' + channels[2][0] + '-' +
                          channels[3][0] + '-positive')

    # Loops through each particle and adds it to each field that it is True for.

    areaCounter = 0
    for z, area in enumerate(areas):

        log.write(str(area))
        log.write("\n")

        if area > tooBigThreshold:
            summary['too-big-(>' + str(tooBigThreshold) + ')'] += 1
        elif area < tooSmallThreshold:
            summary['too-small-(<' + str(tooSmallThreshold) + ')'] += 1
        else:

            summary['#nuclei'] += 1
            areaCounter += area

            temp = 0
            for chan in channels:
                v, x = chan
                if areaFractionsArray[x][z] > areaFractionThreshold[
                        0]:  # theres an error here im not sure why. i remember fixing it before
                    summary[chan[0] + '-positive'] += 1
                    if x != 0:
                        temp += 1

            if temp == 0:
                summary['all-negative'] += 1

            if len(channels) > 2:
                if areaFractionsArray[1][z] > areaFractionThreshold[1]:
                    if areaFractionsArray[2][z] > areaFractionThreshold[2]:
                        summary[channels[1][0] + '-' + channels[2][0] +
                                '-positive'] += 1

            if len(channels) > 3:
                if areaFractionsArray[1][z] > areaFractionThreshold[1]:
                    if areaFractionsArray[3][z] > areaFractionThreshold[3]:
                        summary[channels[1][0] + '-' + channels[3][0] +
                                '-positive'] += 1
                if areaFractionsArray[2][z] > areaFractionThreshold[2]:
                    if areaFractionsArray[3][z] > areaFractionThreshold[3]:
                        summary[channels[2][0] + '-' + channels[3][0] +
                                '-positive'] += 1
                        if areaFractionsArray[1][z] > areaFractionThreshold[1]:
                            summary[channels[1][0] + '-' + channels[2][0] +
                                    '-' + channels[3][0] + '-positive'] += 1

    # Calculate the average of the particles sizes

    if float(summary['#nuclei']) > 0:
        summary['size-average'] = round(areaCounter / summary['#nuclei'], 2)

    # Opens and appends one line on the final csv file for the subfolder (remember that this is still inside the loop that goes through each image)

    with open(outputDirectory + "/" + outputName + ".csv", 'a') as csvfile:

        writer = csv.DictWriter(csvfile,
                                fieldnames=fieldnames,
                                extrasaction='ignore',
                                lineterminator='\n')
        if os.path.getsize(outputDirectory + "/" + outputName + ".csv") < 1:
            writer.writeheader()
        writer.writerow(summary)

    IJ.run(imp, "Close All", "")
def process(subFolder, outputDirectory, filename):

    imp = IJ.openImage(inputDirectory + subFolder + '/' +
                       rreplace(filename, "_ch00.tif", ".tif"))
    IJ.run(
        imp, "Properties...",
        "channels=1 slices=1 frames=1 unit=um pixel_width=0.8777017 pixel_height=0.8777017 voxel_depth=25400.0508001"
    )
    ic = ImageConverter(imp)
    ic.convertToGray8()
    IJ.setThreshold(imp, 2, 255)
    IJ.run(imp, "Convert to Mask", "")
    IJ.run(imp, "Remove Outliers...",
           "radius=5" + " threshold=50" + " which=Dark")
    IJ.run(imp, "Remove Outliers...",
           "radius=5" + " threshold=50" + " which=Bright")

    imp.getProcessor().invert()
    rm = RoiManager(True)
    imp.getProcessor().setThreshold(0, 0, ImageProcessor.NO_LUT_UPDATE)

    boundroi = ThresholdToSelection.run(imp)
    rm.addRoi(boundroi)

    if not displayImages:
        imp.changes = False
        imp.close()

    images = [None] * 5
    intensities = [None] * 5
    blobsarea = [None] * 5
    blobsnuclei = [None] * 5
    bigAreas = [None] * 5

    for chan in channels:
        v, x = chan
        images[x] = IJ.openImage(inputDirectory + subFolder + '/' +
                                 rreplace(filename, "_ch00.tif", "_ch0" +
                                          str(x) + ".tif"))
        imp = images[x]
        for roi in rm.getRoisAsArray():
            imp.setRoi(roi)
            stats = imp.getStatistics(Measurements.MEAN | Measurements.AREA)
            intensities[x] = stats.mean
            bigAreas[x] = stats.area

    rm.close()
    # Opens the ch00 image and sets default properties

    imp = IJ.openImage(inputDirectory + subFolder + '/' + filename)
    IJ.run(
        imp, "Properties...",
        "channels=1 slices=1 frames=1 unit=um pixel_width=0.8777017 pixel_height=0.8777017 voxel_depth=25400.0508001"
    )

    # Sets the threshold and watersheds. for more details on image processing, see https://imagej.nih.gov/ij/developer/api/ij/process/ImageProcessor.html

    ic = ImageConverter(imp)
    ic.convertToGray8()

    IJ.run(imp, "Remove Outliers...",
           "radius=2" + " threshold=50" + " which=Dark")

    IJ.run(imp, "Gaussian Blur...", "sigma=" + str(blur))

    IJ.setThreshold(imp, lowerBounds[0], 255)

    if displayImages:
        imp.show()
    IJ.run(imp, "Convert to Mask", "")
    IJ.run(imp, "Watershed", "")

    if not displayImages:
        imp.changes = False
        imp.close()

    # Counts and measures the area of particles and adds them to a table called areas. Also adds them to the ROI manager

    table = ResultsTable()
    roim = RoiManager(True)
    ParticleAnalyzer.setRoiManager(roim)
    pa = ParticleAnalyzer(ParticleAnalyzer.ADD_TO_MANAGER, Measurements.AREA,
                          table, 15, 9999999999999999, 0.2, 1.0)
    pa.setHideOutputImage(True)
    #imp = impM

    # imp.getProcessor().invert()
    pa.analyze(imp)

    areas = table.getColumn(0)

    # This loop goes through the remaining channels for the other markers, by replacing the ch00 at the end with its corresponding channel
    # It will save all the area fractions into a 2d array called areaFractionsArray

    areaFractionsArray = [None] * 5
    for chan in channels:
        v, x = chan
        # Opens each image and thresholds

        imp = images[x]
        IJ.run(
            imp, "Properties...",
            "channels=1 slices=1 frames=1 unit=um pixel_width=0.8777017 pixel_height=0.8777017 voxel_depth=25400.0508001"
        )

        ic = ImageConverter(imp)
        ic.convertToGray8()
        IJ.setThreshold(imp, lowerBounds[x], 255)

        if displayImages:
            imp.show()
            WaitForUserDialog("Title",
                              "Adjust Threshold for Marker " + v).show()

        IJ.run(imp, "Convert to Mask", "")

        # Measures the area fraction of the new image for each ROI from the ROI manager.
        areaFractions = []
        for roi in roim.getRoisAsArray():
            imp.setRoi(roi)
            stats = imp.getStatistics(Measurements.AREA_FRACTION)
            areaFractions.append(stats.areaFraction)

        # Saves the results in areaFractionArray

        areaFractionsArray[x] = areaFractions

    roim.close()

    for chan in channels:
        v, x = chan

        imp = images[x]
        imp.deleteRoi()
        roim = RoiManager(True)
        ParticleAnalyzer.setRoiManager(roim)
        pa = ParticleAnalyzer(ParticleAnalyzer.ADD_TO_MANAGER,
                              Measurements.AREA, table, 15, 9999999999999999,
                              0.2, 1.0)
        pa.analyze(imp)

        blobs = []
        for roi in roim.getRoisAsArray():
            imp.setRoi(roi)
            stats = imp.getStatistics(Measurements.AREA)
            blobs.append(stats.area)

        blobsarea[x] = sum(blobs)
        blobsnuclei[x] = len(blobs)

        if not displayImages:
            imp.changes = False
            imp.close()
        roim.reset()
        roim.close()

    # Creates the summary dictionary which will correspond to a single row in the output csv, with each key being a column

    summary = {}

    summary['Image'] = filename
    summary['Directory'] = subFolder

    # Adds usual columns

    summary['size-average'] = 0
    summary['#nuclei'] = 0
    summary['all-negative'] = 0

    summary['too-big-(>' + str(tooBigThreshold) + ')'] = 0
    summary['too-small-(<' + str(tooSmallThreshold) + ')'] = 0

    # Creates the fieldnames variable needed to create the csv file at the end.

    fieldnames = [
        'Name', 'Directory', 'Image', 'size-average',
        'too-big-(>' + str(tooBigThreshold) + ')',
        'too-small-(<' + str(tooSmallThreshold) + ')', '#nuclei',
        'all-negative'
    ]

    # Adds the columns for each individual marker (ignoring Dapi since it was used to count nuclei)

    summary["organoid-area"] = bigAreas[x]
    fieldnames.append("organoid-area")

    for chan in channels:
        v, x = chan
        summary[v + "-positive"] = 0
        fieldnames.append(v + "-positive")

        summary[v + "-intensity"] = intensities[x]
        fieldnames.append(v + "-intensity")

        summary[v + "-blobsarea"] = blobsarea[x]
        fieldnames.append(v + "-blobsarea")

        summary[v + "-blobsnuclei"] = blobsnuclei[x]
        fieldnames.append(v + "-blobsnuclei")

    # Adds the column for colocalization between first and second marker

    if len(channels) > 2:
        summary[channels[1][0] + '-' + channels[2][0] + '-positive'] = 0
        fieldnames.append(channels[1][0] + '-' + channels[2][0] + '-positive')

    # Adds the columns for colocalization between all three markers

    if len(channels) > 3:
        summary[channels[1][0] + '-' + channels[3][0] + '-positive'] = 0
        summary[channels[2][0] + '-' + channels[3][0] + '-positive'] = 0
        summary[channels[1][0] + '-' + channels[2][0] + '-' + channels[3][0] +
                '-positive'] = 0

        fieldnames.append(channels[1][0] + '-' + channels[3][0] + '-positive')
        fieldnames.append(channels[2][0] + '-' + channels[3][0] + '-positive')
        fieldnames.append(channels[1][0] + '-' + channels[2][0] + '-' +
                          channels[3][0] + '-positive')

    # Loops through each particle and adds it to each field that it is True for.

    areaCounter = 0
    for z, area in enumerate(areas):

        log.write(str(area))
        log.write("\n")

        if area > tooBigThreshold:
            summary['too-big-(>' + str(tooBigThreshold) + ')'] += 1
        elif area < tooSmallThreshold:
            summary['too-small-(<' + str(tooSmallThreshold) + ')'] += 1
        else:

            summary['#nuclei'] += 1
            areaCounter += area

            temp = 0
            for chan in channels:
                v, x = chan
                if areaFractionsArray[x][z] > areaFractionThreshold[
                        0]:  #theres an error here im not sure why. i remember fixing it before
                    summary[chan[0] + '-positive'] += 1
                    if x != 0:
                        temp += 1

            if temp == 0:
                summary['all-negative'] += 1

            if len(channels) > 2:
                if areaFractionsArray[1][z] > areaFractionThreshold[1]:
                    if areaFractionsArray[2][z] > areaFractionThreshold[2]:
                        summary[channels[1][0] + '-' + channels[2][0] +
                                '-positive'] += 1

            if len(channels) > 3:
                if areaFractionsArray[1][z] > areaFractionThreshold[1]:
                    if areaFractionsArray[3][z] > areaFractionThreshold[3]:
                        summary[channels[1][0] + '-' + channels[3][0] +
                                '-positive'] += 1
                if areaFractionsArray[2][z] > areaFractionThreshold[2]:
                    if areaFractionsArray[3][z] > areaFractionThreshold[3]:
                        summary[channels[2][0] + '-' + channels[3][0] +
                                '-positive'] += 1
                        if areaFractionsArray[1][z] > areaFractionThreshold[1]:
                            summary[channels[1][0] + '-' + channels[2][0] +
                                    '-' + channels[3][0] + '-positive'] += 1

    # Calculate the average of the particles sizes

    if float(summary['#nuclei']) > 0:
        summary['size-average'] = round(areaCounter / summary['#nuclei'], 2)

    # Opens and appends one line on the final csv file for the subfolder (remember that this is still inside the loop that goes through each image)

    with open(outputDirectory + "/" + outputName + ".csv", 'a') as csvfile:

        writer = csv.DictWriter(csvfile,
                                fieldnames=fieldnames,
                                extrasaction='ignore',
                                lineterminator='\n')
        if os.path.getsize(outputDirectory + "/" + outputName + ".csv") < 1:
            writer.writeheader()
        writer.writerow(summary)
Beispiel #19
0
					WaitForUserDialog("Title", "Adjust threshold for " + color[i]).show()



					#Get the threshold you've used

				summary[color[i] + "-threshold-used"] = ImageProcessor.getMinThreshold(channel.getProcessor())

					#Threshold and watershed

				IJ.run(channel, "Convert to Mask", "")
				IJ.run(channel, "Watershed", "")
				
				table = ResultsTable()
				roim = RoiManager(True)
				ParticleAnalyzer.setRoiManager(roim)

					#Analyses particles: finds all the objects that match criteria
				
				pa = ParticleAnalyzer(ParticleAnalyzer.ADD_TO_MANAGER | ParticleAnalyzer.EXCLUDE_EDGE_PARTICLES, Measurements.AREA, table, minimum_size, maximum_size, 0.1, 1.0)
				pa.setHideOutputImage(True)
				pa.analyze(channel)
				
				
				if thresholdMode:
					channel.show()
					WaitForUserDialog("Title", "Look at threshold for" + color[i]).show()
				
					#adds count to summary 
				
				if table.getColumnIndex("Area") != -1:
Beispiel #20
0
from ij.measure import Measurements
from ij.plugin.frame import RoiManager
from ij.plugin.filter import ParticleAnalyzer
from ij import IJ

# get active image
imp = IJ.getImage()

# set up first ROI manager
table1 = ResultsTable()
roim1 = RoiManager()
pa1 = ParticleAnalyzer(
    ParticleAnalyzer.ADD_TO_MANAGER,
    Measurements.AREA | Measurements.MEAN | Measurements.ELLIPSE, table1, 0,
    100, 0, 1)
pa1.setRoiManager(roim1)
pa1.analyze(imp)

# set up second ROI manager
table2 = ResultsTable()
# Pass true to second ROI manager so it will not be seen
roim2 = RoiManager(True)
pa2 = ParticleAnalyzer(
    ParticleAnalyzer.ADD_TO_MANAGER,
    Measurements.AREA | Measurements.MEAN | Measurements.ELLIPSE, table2, 100,
    500, 0, 1)
pa2.setRoiManager(roim2)
pa2.analyze(imp)

print "rois from first manager:"
for roi in roim1.getRoisAsArray():