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()
Ejemplo n.º 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
Ejemplo n.º 3
0
def save_qcd_edges2(edges, output_folder, filename="user_defined_edges.zip"):
    """save edges as rois to a *.zip file"""
    roim = RoiManager(False)
    for edge in edges:
        if edge is not None:
            roim.addRoi(edge)
    roim.runCommand("Save", os.path.join(output_folder, filename))
    roim.close()
Ejemplo n.º 4
0
def save_cell_rois(rois, output_folder, filename):
    """save cell rois to a *.zip file"""
    roim = RoiManager(False)
    for roi in rois:
        if roi is not None:
            roim.addRoi(roi)
    roim.runCommand(
        "Save", os.path.join(output_folder,
                             "{} cell rois.zip".format(filename)))
    roim.close()
Ejemplo n.º 5
0
def load_qcd_edges2(input_file_path):
    """load edges from roi *.zip file"""
    if os.path.isfile(input_file_path):
        roim = RoiManager(False)
        roim.runCommand("Open", input_file_path)
        edges = roim.getRoisAsArray()
        roim.close()
    else:
        edges = load_qcd_edges(os.path.splitext(input_file_path)[0] + '.json')
    return edges
Ejemplo n.º 6
0
def save_qc_image(imp, rois, output_path):
    """save rois overlaid on imp to output_path"""
    imp.killRoi()
    roim = RoiManager(False)
    for roi in rois:
        roim.addRoi(roi)
    roim.runCommand("Show All with labels")
    RGBStackConverter.convertToRGB(imp)
    roim.moveRoisToOverlay(imp)
    FileSaver(imp).saveAsTiff(output_path)
    roim.runCommand("Show None")
    roim.close()
Ejemplo n.º 7
0
def perform_manual_qc(imp, rois, important_channel=1):
    """given cell rois generated by automatic methods, allow user to delete/add/redraw as appropriate"""
    for ch in range(imp.getNChannels()):
        imp.setC(ch + 1)
        sat_frac = 0.99 if (ch + 1) == important_channel else 0.01
        IJ.run(imp, "Enhance Contrast", "saturated={}".format(sat_frac))

    imp.setC(important_channel)
    IJ.setTool("freehand")
    proceed = False
    roim = RoiManager()
    roim.runCommand("Show all with labels")
    for roi in rois:
        roim.addRoi(roi)
    auto_rois_only = rois
    while not proceed:
        dialog = NonBlockingGenericDialog("Perform manual segmentation")
        dialog.setOKLabel("Proceed to next image...")
        dialog.addMessage("Perform manual correction of segmentation: ")
        dialog.addMessage(
            "Draw around cells and add to the region of interest manager (Ctrl+T). "
        )
        dialog.addMessage("Delete and redraw cells as appropriate. ")
        dialog.addMessage(
            "Then press \"proceed to next image\" when all cells have been added. "
        )
        dialog.showDialog()
        if dialog.wasCanceled():
            print("Manual segmentation canceled")
            return auto_rois_only
        elif dialog.wasOKed():
            if roim.getCount() == 0:
                rois = []
                confirm_dialog = GenericDialog("Continue?")
                confirm_dialog.addMessage(
                    "No rois selected in this FOV. Are you sure you want to proceed?"
                )
                confirm_dialog.setOKLabel("Yes, proceed")
                confirm_dialog.setCancelLabel("No, not yet")
                confirm_dialog.showDialog()
                if confirm_dialog.wasOKed():
                    proceed = True
            else:
                rois = roim.getRoisAsArray()
                proceed = True
    roim.reset()
    roim.close()
    for ch in range(imp.getNChannels()):
        imp.setC(ch + 1)
        IJ.run(imp, "Enhance Contrast", "saturated={}".format(0.35))
    imp.setC(important_channel)
    return rois
Ejemplo n.º 8
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
Ejemplo n.º 9
0
def manual_analysis(imp, file_name, output_folder):
    """perform analysis based on manually drawn cells"""
    cal = imp.getCalibration()
    channel_imps = ChannelSplitter.split(imp)
    gfp_imp = channel_imps[0]
    IJ.setTool("freehand")
    proceed = False
    roim = RoiManager()
    roim.runCommand("Show all with labels")
    dialog = NonBlockingGenericDialog("Perform manual segmentation")
    dialog.setOKLabel("Proceed to next image...")
    dialog.addMessage("Perform manual segmentation: ")
    dialog.addMessage(
        "Draw around cells and add to the region of interest manager (Ctrl+T)")
    dialog.addMessage(
        "You can see what you've added so far if you check \"show all\" on the ROI manager"
    )
    dialog.addMessage(
        "Then press \"proceed to next image\" when all cells have been added")
    dialog.showDialog()
    if dialog.wasCanceled():
        raise KeyboardInterrupt("Run canceled")
    elif dialog.wasOKed():
        rois = roim.getRoisAsArray()
        roim.reset()
        roim.close()
        out_stats = generate_cell_shape_results(rois, gfp_imp, cal, file_name)
        print("Number of cells identified = {}".format(len(out_stats)))
        # save output
        save_qc_image(
            imp, rois, "{}_plus_overlay.tiff".format(
                os.path.join(output_folder,
                             os.path.splitext(file_name)[0])))
        save_cell_rois(rois, output_folder, os.path.splitext(file_name)[0])
        imp.changes = False
        imp.close()
        save_output_csv(out_stats, output_folder)
        return out_stats
    return None
Ejemplo n.º 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()
Ejemplo n.º 11
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", "")
Ejemplo n.º 12
0
csv_name = make_path(out_dir, image_name, ".csv")
roi_name = make_path(out_dir, image_name, "_rois.zip")
old_name = make_path(target_dir, image_name, ".tif")
new_name = make_path(out_dir, image_name, ".tif")

current = []
for roi in all_rois:
    res = get_stats(2, roi, imp)
    current.append(res)

# write csv
write_csv(current, csv_name)
# save all rois
save_rois(roi_manager, roi_name)
roi_manager.close()
# move image open next
move_file(old_name, new_name)
imp.changes = False  # prevent the "are you sure? dialogue"
imp.close()

th_files = sorted([
    os.path.join(target_dir, i) for i in os.listdir(target_dir)
    if i.endswith(".tif")
])
if th_files:
    print("{} files left to analyze!".format(len(th_files)))
    print("opening {}".format(th_files[0]))
    next_image = IJ.openImage(th_files[0])
    next_image.show()
else:
Ejemplo n.º 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
Ejemplo n.º 14
0
if reffn is None:
    print "User canceled the dialog!"
else:
    refdir = odref.getDirectory()
    refpath = os.path.join(refdir, reffn)

refImp = IJ.openImage(refpath)
width = refImp.width  
height = refImp.height  

roim = RoiManager()
roim.runCommand("open", roipath)

roiArray = roim.getRoisAsArray()
nRoi = len(roiArray)
roim.close()

bwStack = ImageStack(width, height, nRoi)
for i in xrange(1, nRoi+1):
    bwStack.setProcessor(FloatProcessor(width, height, zeros('f', width * height), None), i)

for i in xrange(1, nRoi+1):
    roi = roiArray[i-1]
    fp = bwStack.getProcessor(i)
    fp.setValue(1.0)
    fp.fill(roi)

roiImp = ImagePlus("roi", bwStack)

outfn = "roi_" + os.path.splitext(roifn)[0] + ".tif"
outpath = os.path.join(roidir, outfn)
Ejemplo n.º 15
0
def batch_open_images(pathImage,
                      pathRoi,
                      pathMask,
                      file_typeImage=None,
                      name_filterImage=None,
                      recursive=False):
    '''Open all files in the given folder.
    :param path: The path from were to open the images. String and java.io.File are allowed.
    :param file_type: Only accept files with the given extension (default: None).
    :param name_filter: Reject files that contain the given string (default: wild characters).
    :param recursive: Process directories recursively (default: False).
    '''
    # Converting a File object to a string.
    if isinstance(pathImage, File):
        pathImage = pathImage.getAbsolutePath()

    def check_type(string):
        '''This function is used to check the file type.
        It is possible to use a single string or a list/tuple of strings as filter.
        This function can access the variables of the surrounding function.
        :param string: The filename to perform the check on.
        '''
        if file_typeImage:
            # The first branch is used if file_type is a list or a tuple.
            if isinstance(file_typeImage, (list, tuple)):
                for file_type_ in file_typeImage:
                    if string.endswith(file_type_):
                        # Exit the function with True.
                        return True
                    else:
                        # Next iteration of the for loop.
                        continue
            # The second branch is used if file_type is a string.
            elif isinstance(file_typeImage, string):
                if string.endswith(file_typeImage):
                    return True
                else:
                    return False
            return False
        # Accept all files if file_type is None.
        else:
            return True

    def check_filter(string):
        '''This function is used to check for a given filter.
        It is possible to use a single string or a list/tuple of strings as filter.
        This function can access the variables of the surrounding function.
        :param string: The filename to perform the filtering on.
        '''
        if name_filterImage:
            # The first branch is used if name_filter is a list or a tuple.
            if isinstance(name_filterImage, (list, tuple)):
                for name_filter_ in name_filterImage:
                    if name_filter_ in string:
                        # Exit the function with True.

                        return True
                    else:
                        # Next iteration of the for loop.
                        continue
            # The second branch is used if name_filter is a string.
            elif isinstance(name_filterImage, string):
                if name_filterImage in string:
                    return True
                else:
                    return False
            return False
        else:
            # Accept all files if name_filter is None.
            return True

    # We collect all files to open in a list.
    path_to_Image = []
    # Replacing some abbreviations (e.g. $HOME on Linux).
    path = os.path.expanduser(pathImage)
    path = os.path.expandvars(pathImage)
    # If we don't want a recursive search, we can use os.listdir().
    if not recursive:
        for file_name in os.listdir(pathImage):
            full_path = os.path.join(pathImage, file_name)
            if os.path.isfile(full_path):
                if check_type(file_name):
                    if check_filter(file_name):
                        path_to_Image.append(full_path)
    # For a recursive search os.walk() is used.
    else:
        # os.walk() is iterable.
        # Each iteration of the for loop processes a different directory.
        # the first return value represents the current directory.
        # The second return value is a list of included directories.
        # The third return value is a list of included files.
        for directory, dir_names, file_names in os.walk(pathImage):
            # We are only interested in files.
            for file_name in file_names:
                # The list contains only the file names.
                # The full path needs to be reconstructed.
                full_path = os.path.join(directory, file_name)
                # Both checks are performed to filter the files.
                if check_type(file_name):
                    if check_filter(file_name) is False:
                        # Add the file to the list of images to open.
                        path_to_Image.append([
                            full_path,
                            os.path.basename(os.path.splitext(full_path)[0])
                        ])
    # Create the list that will be returned by this function.
    Images = []
    Rois = []
    for img_path, file_name in path_to_Image:
        # IJ.openImage() returns an ImagePlus object or None.
        imp = IJ.openImage(img_path)
        print(img_path)
        if check_filter(file_name):
            continue
        else:
            print(file_name, pathRoi)
            RoiName = str(pathRoi) + '/' + file_name + '.roi'
            Roi = IJ.open(RoiName)
            # An object equals True and None equals False.
            rm = RoiManager.getInstance()
            if (rm == None):
                rm = RoiManager()
            rm.addRoi(Roi)

            impMask = IJ.createImage("Mask", "8-bit grayscale-mode",
                                     imp.getWidth(), imp.getHeight(),
                                     imp.getNChannels(), imp.getNSlices(),
                                     imp.getNFrames())
            IJ.setForegroundColor(255, 255, 255)
            rm.runCommand(impMask, "Deselect")
            rm.runCommand(impMask, "Fill")
            rm.runCommand('Delete')
            IJ.saveAs(impMask, '.tif',
                      str(pathMask) + "/" + file_name)
            imp.close()
            rm.close()

            #print(img_path, RoiName)
            Images.append(imp)
            Rois.append(Roi)
    return Images, Rois
Ejemplo n.º 16
0
manders = MandersColocalization()
results = ResultsTable()
for imageFile in os.listdir(inputDir):
	print "Opening " + imageFile
	try:
		images = BF.openImagePlus(inputDir + imageFile)
		image = images[0]
	except UnknownFormatException:
		continue
	preview = getPreview(image)
	preview.show()
	rm = RoiManager()
	dialog = WaitForUserDialog("Action required", "Please select regions of interest in this image. Click OK when done.")
	dialog.show()
	rm.close()
	splitter = ChannelSplitter()
	imp1 = ImagePlus("CH1", splitter.getChannel(image, imageA))
	imp2 = ImagePlus("CH2", splitter.getChannel(image, imageB))
	title = image.getTitle()
	title = title[:title.rfind('.')]
	image.close()
	preview.close()
	ch1 = ImagePlusAdapter.wrap(imp1)
	ch2 = ImagePlusAdapter.wrap(imp2)

	for roi in rm.getRoisAsArray():
		container = createContainer(roi, ch1, ch2)
		img1 = container.getSourceImage1()
		img2 = container.getSourceImage2()
		mask = container.getMask()
Ejemplo n.º 17
0
def tethered_cell(image_path, frame_number=100, frame_rate=100.0, CCW=1):
    """
    parameter setting; frame rate (frame/sec)

    CCW = 1 : the motor rotation direction and the cell rotation direction on the image are same
    CCW = -1: the motor rotation direction and the cell rotation direction on the image are different
    """
    opener = Opener()
    imp = opener.openImage(image_path)
    image_slice_number = imp.getNSlices()
    rm = RoiManager().getInstance()

    if image_slice_number < frame_number: # too short movie
        IJ.log('Number of frame of the movie is fewer than the number of frame that you selected')
        return False
    # create result directory
    result_path = image_path + '_tethered_cell_result'
    if os.path.lexists(result_path) is False:
        os.mkdir(result_path)

    #z projection; standard deviation, tethered cell shorws circle
    IJ.run(imp, 'Subtract Background...', 'rolling=5 light stack')
    IJ.run(imp, 'Median...', 'radius=2 stack')
    IJ.run(imp, 'Z Project...', 'stop=500 projection=[Standard Deviation]')
    zimp = IJ.getImage()
    IJ.saveAs(zimp, 'bmp', os.path.join(result_path,'STD_DEV.bmp'))
    # pick up tethered cell
    IJ.setAutoThreshold(zimp, 'MaxEntropy dark')
    IJ.run(zimp, 'Convert to Mask', '')
    IJ.run('Set Measurements...', "area centroid bounding shape feret's limit redirect=None decimal=3")
    IJ.run(zimp, 'Analyze Particles...', 'size=30-Infinity circularity=0.88-1.00 show=Nothing display exclude clear include')
    zrt = ResultsTable.getResultsTable()
    IJ.saveAs('Results', os.path.join(result_path,'RoiInfo.csv'))

    #tcX and tcY are xy coordinates of tethered cell, tcdia is outer diameter of rotating tethered cell
    #add ROI into stack image
    for i in range(zrt.getCounter()):
        tcX = zrt.getValue('X', i)
        tcY = zrt.getValue('Y', i)
        tcdia = zrt.getValue('Feret', i)
        rm.add(imp, OvalRoi(tcX - tcdia/2.0, tcY - tcdia/2.0, tcdia + 1, tcdia + 1), i)

    #calculate rotation speed by ellipse fitting
    IJ.setAutoThreshold(imp, 'Li')
    for roi_number in range(rm.getCount()):
        t = []
        XM = []
        YM = []
        theta = []
        rotation_speed = []
        area = []
        imp.setRoi(rm.getRoi(roi_number))
        cropped_imp = Duplicator().run(imp)
        IJ.run('Set Measurements...', 'area mean center fit limit redirect=None decimal=3')
        rm.select(roi_number)
        rt = rm.multiMeasure(imp)

        # check cell is present while analysis. Don't a cell gose anywhare?
        for i in range(frame_number):
            area.append(rt.getValue('Area1', i))
        if 0 in area:
            continue

        for i in range(frame_number):
            t.append((1/frame_rate)*i)
            XM.append(rt.getValue('XM1', i))
            YM.append(rt.getValue('YM1', i))
            theta.append(rt.getValue('Angle1', i)/180.0*math.pi)  # convert to radian
            if i == 0:
                rotation_speed.append(0)
            else:
                # phase treatment, theta should be -pi ~ pi
                temp_rotation_speed = [theta[i] - theta[i-1],
                          theta[i] - theta[i-1] + math.pi,
                          theta[i] - theta[i-1] - math.pi,
                          theta[i] - theta[i-1] + 2*math.pi,
                          theta[i] - theta[i-1] - 2*math.pi]
                temp_rotation_speed = sorted(temp_rotation_speed, key = lambda x :abs(x) )[0]
                rotation_speed.append(CCW*temp_rotation_speed/(2.0*math.pi)*frame_rate)

        # write csv
        # earch columns indicate 1:index, 2:time(sec), 3:X-coordinate of center of mass(pixel), 4:Y-coordinate of center of mass (pixel), 5:Angle(Radian), 6:Rotation Speed(Hz)
        with open(os.path.join(result_path,'Roi' + str(roi_number) + '.csv'), 'w') as f:
            writer = csv.writer(f)
            writer.writerow(['Index', 'time(s)', 'X', 'Y', 'Angle(rad)', 'Rotation Speed(Hz)'])
            for i in range(len(t)):
                writer.writerow([i, t[i], XM[i], YM[i], theta[i], rotation_speed[i]])
        # plot x-y, t-x, t-y, t-rotation speed, save plot as bmp
        plotRotation(roi_number, result_path, t, XM, YM, rotation_speed)
        IJ.saveAs(cropped_imp, 'tiff', os.path.join(result_path,'Roi' + str(roi_number) + '.tiff'))
        rt.reset()

    # get analysis date and time
    dt = datetime.datetime.today()
    dtstr = dt.strftime('%Y-%m-%d %H:%M:%S')

    # wtite analysis setting
    with open(os.path.join(result_path,'analysis_setting.csv'), 'w') as f:
        writer = csv.writer(f)
        writer.writerow(['Analysis Date','frame number','frame rate','CCW direction', 'Method','Auto threshold', 'Subtruct Background', 'Median filter'])
        writer.writerow([dtstr, frame_number, frame_rate, CCW, 'Ellipse', 'Li', '5.0', '2'])

    # save roi
    if rm.getCount() != 0:
        rm.runCommand('Save', os.path.join(result_path, 'Roi.zip'))

    zimp.close()
    imp.close()
    rm.close()
    zrt.reset()
Ejemplo n.º 18
0
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)
Ejemplo n.º 19
0
def process(subFolder, outputDirectory, filename):

    roim = RoiManager()
    roim.close()

    imp = IJ.openImage(inputDirectory + subFolder + '/' +
                       filename.replace("_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()
    imp.updateAndDraw()
    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()

    imp.changes = False
    imp.close()

    x_amount = 10
    y_amount = 10

    l = 0
    j = 0
    while l < x_amount:
        k = 0
        while k < y_amount:
            copy = IJ.openImage(inputDirectory + subFolder + '/' +
                                filename.replace("_ch00.tif", ".tif"))
            Xposition = (int)(round((imp.width / x_amount) * l))
            Yposition = (int)(round((imp.width / y_amount) * k))
            Width = (int)(round(imp.width / x_amount))
            Height = (int)(round(imp.height / y_amount))
            roi = Roi(Xposition, Yposition, Width, Height)
            copy.setRoi(roi)
            IJ.run(copy, "Crop", "")
            FileSaver(copy).saveAsTiff(outputDirectory + '/' + filename +
                                       "_crop_" + str(j) + ".tif")
            copy.changes = False
            copy.close()

            for chan in channels:
                v, x = chan
                image = IJ.openImage(inputDirectory + subFolder + '/' +
                                     filename.replace("ch00.tif", "ch0" +
                                                      str(x) + ".tif"))
                roi = Roi(Xposition, Yposition, Width, Height)
                image.setRoi(roi)
                IJ.run(image, "Crop", "")
                FileSaver(image).saveAsTiff(outputDirectory + '/' + filename +
                                            "_crop_" + str(j) + "_ch0" +
                                            str(x) + ".tif")
                image.changes = False
                image.close()

            roim.close()

            k = k + 1
            j = j + 1

        l = l + 1

    imp.getProcessor().setThreshold(0, 0, ImageProcessor.NO_LUT_UPDATE)
    boundroi = ThresholdToSelection.run(imp)

    rm = RoiManager()
    rm.addRoi(boundroi)

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

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

    rm.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

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

    fieldnames = ['Name', 'Directory', 'Image']

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

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

    for chan in channels:
        v, x = chan

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

    # 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)
Ejemplo n.º 20
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)
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
Ejemplo n.º 22
0
                        1.0)

	pa.analyze(imp)
	# time.sleep(2)
	print 'Size of results: ',rt.size()
	# rm.runCommand("select","all")
	# rm.runCommand("Fill","3")
	save_path=saving_dir+"\\mask_%s" % (image[image.rindex('\\')+1:])
	# print(save_path)
	impMask = IJ.createImage("Mask", "8-bit grayscale-mode", imp.getWidth(), imp.getHeight(), imp.getNChannels(), imp.getNSlices(), imp.getNFrames())
	impMask.show()
	IJ.setForegroundColor(255, 255, 255)
	
	rm.runCommand(impMask,"Deselect")
	rm.runCommand(impMask,"Draw")
	

	if doFileSave and FileSaver(impMask).saveAsTiff(save_path):
		print 'Saved Image ',image
	else:
		print '!!! Not saved Image',image
	
	if not leaveMasks and img_ind<len(proc_images)-1:
		impMask.changes=False
		impMask.close()	
		imp.changes=False
		imp.close()
	
	# rm.runCommand("save selected", save_path)
if doRmClose: rm.close()
Ejemplo n.º 23
0
def process(subFolder, outputDirectory, filename):

    imp = IJ.openImage(inputDirectory + subFolder + '/' + filename)
    imp.show()
    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)
    dup = imp.duplicate()
    dup_title = dup.getTitle()
    ic.convertToGray8()
    imp.updateAndDraw()
    IJ.run("Threshold...")

    IJ.setThreshold(218, 245)

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

    rm = RoiManager()
    imp.getProcessor().setThreshold(0, 0, ImageProcessor.NO_LUT_UPDATE)
    boundroi = ThresholdToSelection.run(imp)
    rm.addRoi(boundroi)

    imp.changes = False
    imp.close()

    images = [None] * 5
    intensities = [None] * 5
    blobsarea = [None] * 5
    blobsnuclei = [None] * 5
    cells = [None] * 5
    bigareas = [None] * 5

    IJ.run(dup, "Colour Deconvolution", "vectors=[H DAB]")

    images[0] = getImage(dup_title + "-(Colour_1)")
    images[1] = getImage(dup_title + "-(Colour_2)")
    images[2] = getImage(dup_title + "-(Colour_3)")

    images[2].close()

    for chan in channels:
        v, x = chan
        imp = images[x]
        imp.show()
        for roi in rm.getRoiManager().getRoisAsArray():
            imp.setRoi(roi)
            stats = imp.getStatistics(Measurements.MEAN | Measurements.AREA)
            intensities[x] = stats.mean
            bigareas[x] = stats.area

        rm.runCommand(imp, "Show None")

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

    imp = images[0].duplicate()
    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

    imp.show()
    setTempCurrentImage(imp)
    ic = ImageConverter(imp)
    imp.updateAndDraw()
    IJ.run(imp, "Gaussian Blur...", "sigma=" + str(blur))
    imp.updateAndDraw()

    imp.show()
    IJ.run("Threshold...")
    IJ.setThreshold(30, lowerBounds[0])
    if displayImages:
        imp.show()
        WaitForUserDialog(
            "Title", "Adjust threshold for nuclei. Current region is: " +
            region).show()
    IJ.run(imp, "Convert to Mask", "")

    # 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()
    pa = ParticleAnalyzer(ParticleAnalyzer.ADD_TO_MANAGER, Measurements.AREA,
                          table, 5, 9999999999999999, 0.05, 1.0)

    pa.setHideOutputImage(True)
    imp = IJ.getImage()
    # imp.getProcessor().invert()
    pa.analyze(imp)

    imp.changes = False
    imp.close()

    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
    maxThresholds = []
    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"
        )

        imp.show()

        setTempCurrentImage(imp)

        ic = ImageConverter(imp)
        ic.convertToGray8()
        imp.updateAndDraw()

        rm.runCommand(imp, "Show None")
        rm.runCommand(imp, "Show All")
        rm.runCommand(imp, "Show None")

        imp.show()
        IJ.selectWindow(imp.getTitle())

        IJ.run("Threshold...")
        IJ.setThreshold(20, lowerBounds[x])

        if displayImages:

            WaitForUserDialog(
                "Title", "Adjust threshold for " + v +
                ". Current region is: " + region).show()
            ip = imp.getProcessor()
            maxThresholds.append(ip.getMaxThreshold())

        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.getRoiManager().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()
        imp.updateAndDraw()
        setTempCurrentImage(imp)
        roim = RoiManager()
        pa = ParticleAnalyzer(ParticleAnalyzer.ADD_TO_MANAGER,
                              Measurements.AREA, table, 15, 9999999999999999,
                              0.2, 1.0)
        pa.analyze(imp)

        blobs = []
        cell = []
        for roi in roim.getRoiManager().getRoisAsArray():
            imp.setRoi(roi)
            stats = imp.getStatistics(Measurements.AREA)
            blobs.append(stats.area)
            if stats.area > tooSmallThresholdDAB and stats.area < tooBigThresholdDAB:
                cell.append(stats.area)

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

        cells[x] = len(cell)
        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 = [
        'Directory', 'Image', 'size-average',
        'too-big-(>' + str(tooBigThreshold) + ')',
        'too-small-(<' + str(tooSmallThreshold) + ')', '#nuclei',
        'all-negative'
    ]

    for row in info:
        if row['Animal ID'] == filename.replace('s', '-').replace(
                'p', '-').split('-')[0]:
            for key, value in row.items():
                fieldnames.insert(0, key)
                summary[key] = value

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

    summary["tissue-area"] = bigareas[0]
    fieldnames.append("tissue-area")

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

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

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

        summary[v + "-area/tissue-area"] = blobsarea[x] / bigareas[0]
        fieldnames.append(v + "-area/tissue-area")

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

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

        summary[v + "-particles/tissue-area"] = blobsnuclei[x] / bigareas[0]
        fieldnames.append(v + "-particles/tissue-area")

        fieldnames.append(v + "-HEMO-Cells/tissue-area")

    # 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):

        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]:
                    summary[chan[0] + '-HEMO-cells'] += 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

    for chan in channels:
        v, x = chan
        summary[v + "-cells/tissue-area"] = summary[v + "-cells"] / bigareas[0]

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

    if displayImages:

        fieldnames = ["Directory", "Image"]

        for chan in channels:
            v, x = chan
            summary[v + "-threshold"] = maxThresholds[x]
            fieldnames.append(v + "-threshold")
            allMaxThresholds[v + "-" + region].append(maxThresholds[x])

    # 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(outputName, 'a') as csvfile:

        writer = csv.DictWriter(csvfile,
                                fieldnames=fieldnames,
                                extrasaction='ignore',
                                lineterminator='\n')
        if os.path.getsize(outputName) < 1:
            writer.writeheader()
        writer.writerow(summary)
Ejemplo n.º 24
0
				
				if thresholdMode:
					channel.show()
					WaitForUserDialog("Title", "Look at threshold for" + color[i]).show()
				
					#adds count to summary 
				
				if table.getColumnIndex("Area") != -1:
					summary[color[i] + "-ROI-count"] = len(table.getColumn(table.getColumnIndex("Area")))


				channel.changes = False
				channel.close()

				roim.reset()
				roim.close()

			# Writes everything in the output file

			fieldnames = ["Directory", "Filename", "Red-intensity", "Red-threshold-used", "Red-ROI-count", "Green-intensity", "Green-threshold-used", "Green-ROI-count", "Blue-intensity", "Blue-threshold-used", "Blue-ROI-count"]
			with open(output_name, 'a') as csvfile:		

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

			
			
# End of macro
Ejemplo n.º 25
0
    pa.analyze(imp)
    # time.sleep(2)
    print 'Size of results: ', rt.size()
    # rm.runCommand("select","all")
    # rm.runCommand("Fill","3")
    save_path = saving_dir + "/mask_%s" % (image[image.rindex('/') + 1:])
    # print(save_path)
    impMask = IJ.createImage("Mask", "8-bit grayscale-mode", imp.getWidth(),
                             imp.getHeight(), imp.getNChannels(),
                             imp.getNSlices(), imp.getNFrames())
    impMask.show()
    IJ.setForegroundColor(255, 255, 255)

    rm.runCommand(impMask, "Deselect")
    rm.runCommand(impMask, "Draw")

    if doFileSave and FileSaver(impMask).saveAsTiff(save_path):
        print 'Saved Image ', image
    else:
        print '!!! Not saved Image', image

    if not leaveMasks and img_ind < len(proc_images) - 1:
        impMask.changes = False
        impMask.close()
        imp.changes = False
        imp.close()

    # rm.runCommand("save selected", save_path)
if doRmClose:
    rm.close()