Morphological segmentation script issues in my macro

Dear @iarganda and @dlegland,

I have imaged samples with several z-slices on a confocal microscope and am using Weka segmentation 3D and your MorpholibJ Plugins to segment my cells which are tightly packed human induced pluripotent cells that have been grown in a confined geometry known as ‘micropatterns’. The nuclei have been stained with DAPI, which is how I’m segmenting the cells. I’ve also stained the nuclei with other fluorescent antibodies, which I need to measure the intensity of.

I’m running into a problem when writing into my Macro the Morphological Segmentation 3D part of the script. I used the FIJI/ImageJ recorder to do this (see below):
//Use Gradient filtering and further segment in MorpholibJ
selectWindow(“Probability maps-Contour”);
run(“Morphological Segmentation”);
selectWindow(“Morphological Segmentation”);
//setTool(“multipoint”);
call(“inra.ijpb.plugins.MorphologicalSegmentation.setInputImageType”, “object”);
wait(100);
call(“inra.ijpb.plugins.MorphologicalSegmentation.setShowGradient”, “true”);
call(“inra.ijpb.plugins.MorphologicalSegmentation.setGradientRadius”, “3”);
call(“inra.ijpb.plugins.MorphologicalSegmentation.setGradientType”, “Morphological”);
call(“inra.ijpb.plugins.MorphologicalSegmentation.segment”, “tolerance=250.0”, “calculateDams=true”, “connectivity=26”);
wait(10000);
call(“inra.ijpb.plugins.MorphologicalSegmentation.setDisplayFormat”, “Catchment basins”);
call(“inra.ijpb.plugins.MorphologicalSegmentation.createResultImage”);
run(“Remove Border Labels”, “left right top bottom”);

My image opens into the Morphological Segmentation window and the gradient radius is changed. However, none of the other settings that I need are adjusted (the script will not select ‘Object image’, ‘show gradient’ is not ticked either and the tolerence value doesn’t change and the advanced options doesn’t get selected). See screenshot:

I am able to do all these functions manually successfully. I now would like to automate the process by batch processing my images, but am unable to at the moment because of this problem. Is there something I’m missing?

Kind regards,
@nsantucci

Hello @nsantucci and welcome to the forum!

Sorry for the late answer. These days is difficult to stay up to date with the e-mail and forum posts!

Try adding a wait as well after calling the Morphological Segmentation plugin, it takes some time for the GUI to open:

run("Morphological Segmentation");
wait(2000);
selectWindow("Morphological Segmentation");

Also, you have selected the “object” image type but you have contour maps, wouldn’t it make more sense to use the border option?

Cheers!

1 Like

Hi @iarganda! Thank you so much for responding!

Your wait time suggestion has worked. However, I now have a problem with creating a Catchment basins image and have tried to introduce wait times between the script lines to fix this issue (example below), without success. Do you have any suggestions for this?

    call("inra.ijpb.plugins.MorphologicalSegmentation.segment", "tolerance=250.0", 
        "calculateDams=true", "connectivity=26");
wait(100000);
selectWindow("Morphological Segmentation");
call("inra.ijpb.plugins.MorphologicalSegmentation.setDisplayFormat", "Catchment basins");
wait(2000);
selectWindow("Probability maps-Contour-catchment-basins");
wait(2000);
call("inra.ijpb.plugins.MorphologicalSegmentation.createResultImage");
run("Remove Border Labels", "left right top bottom");

Regarding your question as to ‘object’ versus ‘border’:
When I select ‘border’ instead of ‘object’ for my contour map I get a speckled result which doesn’t represent the nuclei and therefore doesn’t find the cells adequately. This doesn’t improve no mater what tolerance is selected.

In Weka I had 3 categories in which I trained the plugin - nucleus, contour and background. Using the Nucleus probability map in MorpholibJ’s ‘Morphological Segmentation 2D/3D’ allowed the nuclei to be segmented but the entire diameter of the nuclei weren’t able to be captured, which will cause problems later on when I’m measuring nuclei fluorescent intensity as the entire nuclei will not be represented in the data.

I’m currently not entirely happy with how the contour map aligns with the catchment basins created in ‘Morphological Segmentation 2D/3D’ and am now trying to find a new way to threshold the cells (instead of using Weka). I’m contemplating trying to integrate TrackMate’s spot detector algorithm (but this is in python, which creates a whole set of issues which I’m not sure I’ll be able to tackle! I’m a cell biologist, so script writing is not easy for me!!). Any suggestions would be greatly appreciated!!

FYI

image This is a screenshot of the original DAPI image (the middle z-slice)

This is a the result when ‘border’ is selected instead of ‘object’

What is the problem you have? Can you post the input image and the entire macro so I can reproduce the error, please?

That depends of course on how well the borders of your cells are detected by TWS. I’m interested on having a look at that probability image, can you post it here, please?

That looks like a perfect image to be segmented by StarDist! Why don’t you give it a try?

1 Like

Thanks for the suggestion! It seems that the limitation with Stardist is that it is for 2D images. I’m wandering if I can segment the z-slices individually on Stardist, then stack them back together when needing to analyze them on MorpholibJ. Also, Stardist is in Jython, which I’m not familiar with.

My images are czi files and are very large. I’ve even separated the channels and tried uploading just the DAPI stain as a tiff, but this is too large too. I am, however, able to upload as a jpeg:

.

This is my entire script so far:
//Ask the user to select the source directory
dir1 = getDirectory(“Select the source directory”);

//Ask the user to select the destination directory
dir2 = getDirectory(“Choose the destination directory”);

//Generate a list of the files in the selected directory
list = getFileList(dir1);
Array.sort(list);

for(i=0;i<list.length;i++){	
fileName = dir1 + list[i];
if(endsWith(fileName,"czi")){
//use the Bio-FOrmats importer to open the file
run("Bio-Formats Importer", "open=["+fileName+"] autoscale color_mode=Colorized rois_import=[ROI manager] view=Hyperstack stack_order=XYCZT");


//Store the initial image name into the nameStore variable
nameStore = getTitle();

//Split the channels into red and blue
run("Split Channels");

//Rename the channels according to their stain
selectWindow("C1-"+nameStore);
rename("DAPI");

selectWindow("C2-"+nameStore);
rename("594");

selectWindow("C3-"+nameStore);
rename("488");

//use the DAPI stain to segment the micropattern using Weka Segmentation 3D plugin
selectWindow("DAPI");
run("Trainable Weka Segmentation 3D");
selectWindow("Trainable Weka Segmentation v3.2.34");
call("trainableSegmentation.Weka_Segmentation.changeClassName", "0", "NUCLEUS");
call("trainableSegmentation.Weka_Segmentation.changeClassName", "1", "CONTOUR");
call("trainableSegmentation.Weka_Segmentation.createNewClass", "BG");
call("trainableSegmentation.Weka_Segmentation.loadData", "G:\\Lab 5 Image Data\\Project and data\\hIPSC\\Differentiation\\Micropattern\\data and classifiers for Weka 3D\\data_fa3_2048x2048_edges and diff gaussian blur settings_48h_15-09-20.arff");
call("trainableSegmentation.Weka_Segmentation.trainClassifier");
wait(100000);
call("trainableSegmentation.Weka_Segmentation.getProbability");
wait(10000);

//separate contour probability map
selectWindow("Probability maps");
run("Duplicate...", "title=[Probability maps-Contour] duplicate channels=2");

//threshold probability maps and create a binary image
selectWindow("Probability maps-Contour");
run("Convert to Mask", "method=MaxEntropy background=Dark calculate black");

//Use Gradient filtering and further segment in MorpholibJ
selectWindow("Probability maps-Contour");
run("Morphological Segmentation");
wait(2000);
selectWindow("Morphological Segmentation");
//setTool("multipoint");
call("inra.ijpb.plugins.MorphologicalSegmentation.setInputImageType", "object");
wait(2000);
call("inra.ijpb.plugins.MorphologicalSegmentation.setShowGradient", "true");
wait(2000);
call("inra.ijpb.plugins.MorphologicalSegmentation.setGradientRadius", "3");
wait(2000);
call("inra.ijpb.plugins.MorphologicalSegmentation.setGradientType", "Morphological");
wait(2000);
call("inra.ijpb.plugins.MorphologicalSegmentation.segment", "tolerance=250.0", "calculateDams=true", "connectivity=26");
wait(100000);
selectWindow("Morphological Segmentation");
call("inra.ijpb.plugins.MorphologicalSegmentation.setDisplayFormat", "Catchment basins");
wait(2000);
selectWindow("Probability maps-Contour-catchment-basins");
wait(2000);
call("inra.ijpb.plugins.MorphologicalSegmentation.createResultImage");
run("Remove Border Labels", "left right top bottom");

//tabulate the coordinates and characteristic of each nuclei in the micropattern and save the data
run("Analyze Regions 3D", "volume surface_area mean_breadth sphericity euler_number bounding_box centroid equivalent_ellipsoid ellipsoid_elongations max._inscribed surface_area_method=[Crofton (13 dirs.)] euler_connectivity=C26");
saveAs("Results", dir2+nameStore+"-Analyzed regions.csv");

//tabulate the intensity measurement of each nuclei for each stain in the micropattern and save the data
run("Intensity Measurements 2D/3D", "input=DAPI labels=Probability-killBorders mean stddev numberofvoxels volume");
saveAs("Results", dir2+nameStore+"-DAPI Intensity Measurement.csv");
run("Intensity Measurements 2D/3D", "input=594 labels=Probability-killBorders mean stddev numberofvoxels volume");
saveAs("Results", dir2+nameStore+"-594 Intensity Measurement.csv");
run("Intensity Measurements 2D/3D", "input=488 labels=Probability-killBorders mean stddev numberofvoxels volume");
saveAs("Results", dir2+nameStore+"-488 Intensity Measurement.csv");

//save the images
selectWindow("Probability maps");
rename(nameStore+"-Probability maps");
saveAs("Tiff", dir2+nameStore+"-Probability maps");

selectWindow("Probability maps-Contour");
rename(nameStore+"-Contour");
saveAs("Tiff", dir2+nameStore+"-Probability maps");

selectWindow("Probability-killBorders");
rename(nameStore+"-Probability-killBorders");
saveAs("Tiff", dir2+nameStore+"-Probability-killBorders");

//close all windows
run("Close All");
close(nameStore+"-Analyzed regions.csv");
close(nameStore+"-DAPI Intensity Measurement.csv");
close(nameStore+"-594 Intensity Measurement.csv");
close(nameStore+"-488 Intensity Measurement.csv");

}

}

//clean up memory
wait(1000);
call(“java.lang.System.gc”);
wait(1000);

This example shows the 11 z-slices taken and the nucleus, contour and background probability maps produced by Weka segmentation 3D.