Debugging & Understanding Macro

batch-processing
segmentation
macro
Tags: #<Tag:0x00007fb87d5bb908> #<Tag:0x00007fb87d5bb7a0> #<Tag:0x00007fb87d5bb610>

#1

Hi all, I am new to image J. I have a large quantity of images that I would like to process in the same fashion. This includes a pre-processing technique followed by running a plug in, all which I would like to complete with set values I have chosen. I was hoping that a macro would seamless record the steps I took and apply it to the image I am testing it on, but of course it isn’t so :slight_smile: It seems to want me to manually enter values in pop-up windows and then the image needed to continue to the next processing step isn’t made yet and the macro fails. Also, how might I easily save the data output for each image to be analyzed? I have three images of each category which I would like to analyze. All help is greatly appreciated.

Here is an explanation of the manual steps I would take:
File > Open > Image
Image > Type > 8-bit
Image > Adjust > Brightness/Contrast
Process > Filters > Median > Radius = 1 pixels, Preview
Process > Subtract Background > Rolling ball radius = 20 pixels, Preview
Plugins > Morpholibj > Fill Holes (Binary/Gray)
Plugins > Morpholibj > Segmentation > Morphological Segmentation
Object Image
Tolerance = 50
Review results on Display Overlaid dams
Display Watershed Lines
Create Image
Analyze > Set Scale 1 Pixel/_ Known Distance (unit of length) > OK
Analyze > Set Measurements > Area, Area Fraction, Centroid, Feret’s diameter, Stack position, Display Label > Okay
Analyze > Analyze Particles
Adjust circularity (.25-1) and size constraints (20-inf) to include desired particles
Show: Masks
Include holes

Here is a copy of the text for the macro (note that the set scale step is to be updated):

run("8-bit");
//run("Brightness/Contrast...");
run("Enhance Contrast", "saturated=0.35");
run("Enhance Contrast", "saturated=0.35");
run("Apply LUT");
run("Close");
run("Median...", "radius=1");
run("Remove Outliers...");
run("Subtract Background...", "rolling=20");
run("Fill Holes (Binary/Gray)");
run("Morphological Segmentation");
selectWindow("Morphological Segmentation");
//setTool("multipoint");
call("inra.ijpb.plugins.MorphologicalSegmentation.setInputImageType", "object");
call("inra.ijpb.plugins.MorphologicalSegmentation.setGradientRadius", "1");
call("inra.ijpb.plugins.MorphologicalSegmentation.segment", "tolerance=50", "calculateDams=true", "connectivity=6");
call("inra.ijpb.plugins.MorphologicalSegmentation.setDisplayFormat", "Watershed lines");
call("inra.ijpb.plugins.MorphologicalSegmentation.setDisplayFormat", "Overlaid dams");
call("inra.ijpb.plugins.MorphologicalSegmentation.setDisplayFormat", "Watershed lines");
selectWindow("Courtney_3_10_KinserCtrlWell1-watershed-lines.1_ch00-fillHoles");
call("inra.ijpb.plugins.MorphologicalSegmentation.createResultImage");
selectWindow("Morphological Segmentation");
selectWindow("Courtney_3_10_KinserCtrlWell1-watershed-lines.1_ch00-fillHoles");
run("Set Scale...", "distance=1 known=2 unit=unit global");
run("Set Measurements...", "area feret's area_fraction display redirect=None decimal=5");
run("Analyze Particles...", "size=20-Infinity circularity=0.25-1.00 show=Masks display include summarize");
makePoint(483, 0);        // **(What does this mean?)**

#2

Hello @Undergrad,

you are closing the currently active image in line 6 with run("Close"). Get rid of that first before continuing any work.

Furthermore, it seems that Remove Outliers has not properly recorded the values you had set in the user interface. It looks like this for me, when recording:

run("Remove Outliers...", "radius=2 threshold=50 which=Bright");

Also, you have some selectWindow() statements in your macro that contain a fixed name and are unnecessary. You will either have to remove those (if you have a linear of images) or store the title of the active image at the very beginning of the macro execution (title = getTitle(); selectWindow(title);).

And as a last step. The output image of the Morphological Segmentation is not automatically selected as active image, so you will have to do that manually:

// Open sample image
run("Blobs (25K)");

run("8-bit");
run("Enhance Contrast", "saturated=0.35");
run("Apply LUT");
run("Median...", "radius=1");
run("Remove Outliers...", "radius=2 threshold=50 which=Bright");
run("Subtract Background...", "rolling=20");
run("Fill Holes (Binary/Gray)");
titleAfterProcessing = getTitle();

run("Morphological Segmentation");
wait(1000);
//setTool("multipoint");
call("inra.ijpb.plugins.MorphologicalSegmentation.setInputImageType", "object");
call("inra.ijpb.plugins.MorphologicalSegmentation.setGradientRadius", "1");
call("inra.ijpb.plugins.MorphologicalSegmentation.segment", "tolerance=50", "calculateDams=true", "connectivity=6");
call("inra.ijpb.plugins.MorphologicalSegmentation.setDisplayFormat", "Watershed lines");
call("inra.ijpb.plugins.MorphologicalSegmentation.createResultImage");

// Close Morphological Segmentation
selectWindow("Morphological Segmentation");
close();

// Select output of morphological segmentation
selectWindow(titleAfterProcessing+"-watershed-lines");

// Continue processing
run("Set Scale...", "distance=1 known=2 unit=unit global");
run("Set Measurements...", "area feret's area_fraction display redirect=None decimal=5");
run("Analyze Particles...", "size=20-Infinity circularity=0.25-1.00 show=Masks display include summarize");

Best,
Stefan