Scripting Marker-Based Watershed

Hi everyone,

I’m having a problem scripting with MorphoLibJ. I’d appreciate any help if anyone else has had this problem. Or maybe @iarganda, you might be the person to know the answer?

I am trying to script a segmentation workflow using marker-controlled watershed segmentation. Whenever I run the marker controlled watershed command in my script, it throws an error:

	"avgip_astropdl_001_316-2.tif" is not a valid choice for "mask" in line 55:
	run ( "Marker-controlled Watershed" , "input=&grad marker=&marker mask=&mask binary calculate use" <)> ; 

However, when I run the plugin outside of the script, it works great. Even using the same exact images I am calling in the script.

run("Marker-controlled Watershed", "input=avgip_astropdl_001_316-2-Gradient marker=markers mask=avgip_astropdl_001_316-2 binary calculate use");

Can’t seem to pin down what the problem is. I’ve tried setting all the image dimensions to be equal, binarizing images different ways, changing the bit depth of all the images. I’m out of ideas.

For what it’s worth, I’ve attached a sample image that I’m working with. The script is pasted below. Anyone have any suggestions on getting the command to work in the script?

Best,
Wilson

avgip_astropdl_001_316.tif (8.0 MB)

// Open AvgIP file, get image stats
avgip = getTitle();
Stack.getDimensions(width, height, channels, slices, frames);
getPixelSize(unit, pixelWidth, pixelHeight);

// Sub Background (50px kernel)
run("Duplicate...", " ");
wkimg = getTitle();
run("Subtract Background...", "rolling=50");
run("Enhance Contrast", "saturated=5.00");

// Gaussian Blur (7px kernel)
run("Gaussian Blur...", "sigma=7");

// Additional Flatfield Correction if needed for cell activation
run("Enhance Local Contrast (CLAHE)", "blocksize=199 histogram=256 maximum=7.00 mask=*None* fast_(less_accurate)");
run("Duplicate...", " ");

// Threshold image to generate Mask (Huang). Erode a bit. Fill holes.
setAutoThreshold("Otsu dark");
//run("Threshold...");
run("Convert to Mask");
mask = getTitle();

// Make Gradient mask image
run("Morphological Filters", "operation=Gradient element=Disk radius=2");
grad = getTitle();

// Find Local Maxima (Prominence 20), add to ROI manager
selectWindow(wkimg);
run("Find Maxima...", "prominence=20 output=[Point Selection]");
roiManager("add");

// Make blank marker image. Apply points to img with 'Fill'
// You cant use ROImanager point selections. you have to have an image with points identifies as maximum seed points.
newImage("markers", "8-bit black", width, height, 1);
marker = getTitle();
roiManager("Select", 0);
roiManager("Fill");
run("Make Binary");
run("Properties...", "channels=1 slices=1 frames=1 unit=&unit pixel_width=&pixelWidth pixel_height=&pixelHeight voxel_depth=1.0000");

// Marker based watershed (input = gradient, binary markers, mask)
run("Marker-controlled Watershed", "input=&grad marker=&marker mask=&mask binary calculate use");
wtsh = getTitle(); 
run("Fire");
roiManager("select", 0);
roiManager("delete");

// Threshold watershed map 1:inf for Analyze Particles
run("Duplicate...", " ");
setThreshold(0.1000, 1000000000000000000000000000000.0000);
run("Convert to Mask");
wsmsk = getTitle(); 

// Analyze particles --> min area = 200 into ROI. Save
run("Analyze Particles...", "size=200-Infinity pixel circularity=0.10-1.00 add");

// Check and Update ROIs (if needed, probably needed...). Save to new subdir

selectWindow(avgip); 
roiManager("show all with labels");

// Multimeasure on the orig stack. 

// Save output into a new subdirectory

// Close everything

close(wkimg);
close(mask);
close(grad);
close(mark);
close(wtsh);
close(wsmsk);

So this won’t be input that can explain why you see this problem but I have got it to work at least.

Due to the problem seemingly being the image titles, I tested if instead of capturing the titles of images in variables, I name the images those variables, i.e.

//avgip = getTitle();
rename("avgip");

And once I did this the marker watershed worked fine (plus removing the & symbols in the command of course).

I did it again renaming only the problematic mask image in this way and that was also fine so that’s a way to get it working with minimal changing.

While playing around I noticed that you get that not a valid choice error if the window can’t be found so not just if it’s having trouble using it as a mask.

2 Likes

Hey @lmurphy,

Thanks so much for having a look. Looks like your fix works in my machine as well. Thanks for the suggestion. Much appreciated.

I’ve found that I can get away with running the script on a binary image and it seems to work fine. but when i run it on a 16bit experimental image it throws that error. Interesting that it cannot find the window. Not sure if that has anything to do with it? Otherwise, this works for the time being.

Best,
Wilson

Dear @Wilson_Adams and @lmurphy,

That was actually a “bug”. In the plugin I was taking the short titles of the windows instead of the full titles as returned by “getTitle()”. I will correct it and add it to the next release of MorphoLibJ. Thanks for reporting!

@Wilson_Adams Regarding your macro, once you have the maxima located, you can skip the creation of markers calling the Interactive Marker-controlled plugin instead :wink:

Cheers!

2 Likes

Ah interesting.

I thought it was maybe that at some point because I tried capturing the image variables with “File.nameWithoutExtension” to see if that would make a difference but I still didn’t get it to work. Doesn’t matter now though, thanks for being a vigliant developer.

1 Like

Good to know! Thanks for being on top of that.

Regarding the interactive marker-controled watershed, i had no idea! I’m going to try that out right now. Thanks for the suggestion

Best,
Wilson