Example #1
0
def analyzeImage(passImage, passModel, passChannel, passProbability,
                 passPixels, passOutput):
    retResults = list()
    windows = set()

    # Register current window
    registerWindow(passImage.title, windows)

    # Extract the requested channel
    IJ.run("Z Project...", "projection=[Max Intensity]")
    registerWindow("MAX_" + passImage.title, windows)
    IJ.run("Duplicate...", "title=temp")
    registerWindow("temp", windows)

    # Apply WEKA training model to image
    wekaSeg = WekaSegmentation(WindowManager.getCurrentImage())
    wekaSeg.loadClassifier(passModel)
    wekaSeg.applyClassifier(True)

    # Extract first slice of probability map
    wekaImg = wekaSeg.getClassifiedImage()
    wekaImg.show()
    registerWindow("Probability maps", windows)
    IJ.setSlice(1)
    IJ.run("Duplicate...", "title=temp2")
    registerWindow("temp2", windows)

    # Apply threshold and save
    IJ.setThreshold(passProbability, 1, "Black & White")
    fileParts = passImage.getTitle().split(".")
    IJ.save(
        os.path.join(
            passOutput, "{0}-probmap.png".format(fileParts[0],
                                                 '.'.join(fileParts[1:]))))

    # Perform particle analysis and save
    IJ.run("Analyze Particles...",
           "size={0}-Infinity show=Outlines pixel clear".format(passPixels))
    registerWindow("Drawing of temp2", windows)
    IJ.save(
        os.path.join(
            passOutput, "{0}-particles.png".format(fileParts[0],
                                                   '.'.join(fileParts[1:]))))

    # Get measurements
    tableResults = ResultsTable.getResultsTable()
    for rowIdx in range(tableResults.size()):
        retResults.append(tableResults.getRowAsString(rowIdx).split())
        retResults[-1].insert(
            0,
            WindowManager.getCurrentImage().getCalibration().unit)
        retResults[-1].append(
            float(retResults[-1][4]) / float(retResults[-1][3]))

    # Close windows
    closeWindows(windows)

    return retResults
def analyzeImage(passImage, passModel, passProbability, passPixels,
                 passOutput):
    retResults = list()

    # Apply WEKA training model to image
    wekaSeg = WekaSegmentation(passImage)
    wekaSeg.loadClassifier(passModel)
    wekaSeg.applyClassifier(True)

    # Extract first slice of probability map
    wekaImg = wekaSeg.getClassifiedImage()
    wekaImg.show()

    IJ.selectWindow("Probability maps")
    IJ.setSlice(1)
    IJ.run("Duplicate...", "title=temp")

    # Apply threshold and save
    IJ.setThreshold(passProbability, 1, "Black & White")
    fileParts = passImage.getTitle().split(".")
    IJ.save(
        os.path.join(
            passOutput, "{0}-probmap.png".format(fileParts[0],
                                                 '.'.join(fileParts[1:]))))

    # Perform particle analysis and save
    IJ.run("Analyze Particles...",
           "size={0}-Infinity show=Outlines pixel clear".format(passPixels))
    IJ.selectWindow("Drawing of temp")
    IJ.save(
        os.path.join(
            passOutput, "{0}-particles.png".format(fileParts[0],
                                                   '.'.join(fileParts[1:]))))

    # Get measurements (skip final row, this will correspond to legend)
    tableResults = ResultsTable.getResultsTable()
    for rowIdx in range(tableResults.size() - 1):
        retResults.append(tableResults.getRowAsString(rowIdx).split())

    # Close interim windows
    IJ.run("Close")
    IJ.selectWindow("temp")
    IJ.run("Close")
    IJ.selectWindow("Probability maps")
    IJ.run("Close")

    return retResults
def Weka_Segm(dirs):
	""" Loads trained classifier and segments cells """ 
	"""	in aligned images according to training.    """
	
	# Define reference image for segmentation (default is timepoint000).
	w_train = os.path.join(dirs["Composites_Aligned"], "Timepoint000.tif")
	trainer = IJ.openImage(w_train)
	weka = WekaSegmentation()
	weka.setTrainingImage(trainer)
	
	# Select classifier model.
	weka.loadClassifier(str(classifier))
     
	weka.applyClassifier(False)
	segmentation = weka.getClassifiedImage()
	segmentation.show()

	# Convert image to 8bit
	ImageConverter(segmentation).convertToRGB()
	ImageConverter(segmentation).convertToGray8()
		
	# Threshold segmentation to soma only.
	hist = segmentation.getProcessor().getHistogram()
	lowth = Auto_Threshold.IJDefault(hist)
	segmentation.getProcessor().threshold(lowth)
	segmentation.getProcessor().setThreshold(0, 0, ImageProcessor.NO_LUT_UPDATE)
	segmentation.getProcessor().invert()
	segmentation.show()
	
	# Run Watershed Irregular Features plugin, with parameters.
	IJ.run(segmentation, "Watershed Irregular Features",
	      "erosion=20 convexity_treshold=0 separator_size=0-Infinity")

	# Make selection and add to RoiManager.	
	RoiManager()
	rm = RoiManager.getInstance()
	rm.runCommand("reset")
	roi = ThresholdToSelection.run(segmentation)
	segmentation.setRoi(roi)
	rm.addRoi(roi)
	rm.runCommand("Split")
def analyzeImage(passImage, passModel, passProbability, passOutput):
    retResults = list()

    # Apply weka training model to image
    wekaSeg = WekaSegmentation(passImage)
    wekaSeg.loadClassifier(passModel)
    wekaSeg.applyClassifier(True)

    # Extract probability map
    wekaImg = wekaSeg.getClassifiedImage()
    wekaImg.show()
    IJ.run("Clear Results")

    # Process each slice
    for sliceIdx in range(ROW_BACKGROUND + 1):
        # Select slice and duplicate
        IJ.selectWindow("Probability maps")
        IJ.setSlice(sliceIdx + 1)
        IJ.run("Duplicate...", "title=temp")

        # Apply threshold to probability
        IJ.setThreshold(passProbability, 1, "Black & White")

        # For background, take inverse
        if sliceIdx == ROW_BACKGROUND: IJ.run("Invert")

        # Change background to NaN for area, then measure
        IJ.run("NaN Background", ".")
        IJ.run("Measure")

        # Save image to output directory
        fileParts = passImage.getTitle().split(".")
        IJ.save(
            os.path.join(
                passOutput,
                "{0}-{1}.{2}".format(fileParts[0], FILE_TYPE[sliceIdx],
                                     '.'.join(fileParts[1:]))))

        IJ.selectWindow("temp")
        IJ.run("Close")

    # Close probability maps
    IJ.selectWindow("Probability maps")
    IJ.run("Close")

    # Obtain results
    tempResults = list()
    tableResults = ResultsTable.getResultsTable()
    for rowIdx in range(tableResults.size()):
        tempResults.append(
            [float(x) for x in tableResults.getRowAsString(rowIdx).split()])

    # Compile image statistics as M/(M+F), F/(M+F), M/total, F/total, U/total, E/total, M+F, total
    mfTotal = tempResults[ROW_MALE][FIELD_AREA] + tempResults[ROW_FEMALE][
        FIELD_AREA]
    total = tempResults[ROW_BACKGROUND][FIELD_AREA]

    retResults.append(tempResults[ROW_MALE][FIELD_AREA] / mfTotal)
    retResults.append(tempResults[ROW_FEMALE][FIELD_AREA] / mfTotal)
    retResults.append(tempResults[ROW_MALE][FIELD_AREA] / total)
    retResults.append(tempResults[ROW_FEMALE][FIELD_AREA] / total)
    retResults.append(tempResults[ROW_UNDIF][FIELD_AREA] / total)
    retResults.append(tempResults[ROW_EXTRA][FIELD_AREA] / total)
    retResults.append(mfTotal)
    retResults.append(total)

    return retResults