Alternatives to "Draw" for drawing an ROI on an image from a macro?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Alternatives to "Draw" for drawing an ROI on an image from a macro?

cnarciso
Hello All,

To make a long story short, we have been collecting tiled image data of some
specimens which have two distinct regions labeled with a red and blue dye
respectively. One of the dyes is fluorescent, so I have been collecting a
series of images which capture the specimen as a whole and then another that
just capture the fluorescent dye region. I wrote a macro which iterates
through all of the directories of images and separates the red and blue
regions for quantification. As a validation step, I have the macro creating
thumbnail images of each specimen wherein the segmented region is stamped
into the thumbnail image. That way, I can quickly go back and skim the
images to make sure that the segmentation is working well and also check any
cases that seem strange from just the numbers. The main problem I seem to be
having is with the "Draw" command for stamping the ROI onto the thumbnail
image. This seems to work very inconsistently when called from a macro
(which is frustrating). The macro, overall, runs fine otherwise. For
instance, last week, this is the output that the macro below was giving when
run from my desktop computer:

<http://imagej.1557.x6.nabble.com/file/t379027/3434_0009_S1_1.jpg>

When I went to re-run the analysis earlier, this is the output it was giving
for the same thumbnail:

<http://imagej.1557.x6.nabble.com/file/t379027/3434_0009_S1.jpg>

No changes or updates have been made to ImageJ as far as I know. Further,
the "Draw" command works just fine when done manually (i.e. - when I draw a
line on the image and then manually go to "Edit>Draw"). Seems to just be
finicky when called from a macro. The same code works fine from my laptop
(which has the same version installed). Are there any alternatives to Draw
that I might use in my macro instead? If not, anybody have any ideas of what
I might be able to do to get "Draw" to work a little more consistently?

The full macro (quite long) is below...

//Pick the main directory where all the data is stored
main = getDirectory("Source Directory ");
outputDir = getDirectory("Output Directory "); //temp comment out

//Turn on batch mode
setBatchMode(true);

//Get files
mouseList = getFileList(main);
//sectionList = getFileList(main);

setBackgroundColor(255, 255, 0);
setForegroundColor(255, 255, 0);

//Loop through mouse directories. Note - "Tiff Virtual Stack..." and
"Bio-Formats" give memory errors. "Open()" works fine though.
print("\\Clear");
print("MOUSE #" + ";" + "SLIDE #" + ";" + "SECTION ID" + ";" + "ROIs" + ";"
+ "MAX AREA");
for(k=0; k<mouseList.length; k++){
        mousePath = outputDir + substring(mouseList[k], 0,4) + File.separator;
        //Create output directory for thumbnails if does not exist
        if (!File.exists(mousePath)) File.makeDirectory(mousePath);
        sectionDir = main + substring(mouseList[k], 0,4);
        sectionList = getFileList(main + mouseList[k]);
        //Iterate through the sections for each mouse
        for(i=0; i&lt;sectionList.length; i++){
                //run(&quot;TIFF Virtual Stack...&quot;, &quot;open=[&quot;+main +
slideList[i]+&quot;]&quot;);
                //run(&quot;Bio-Formats (Windowless)&quot;, &quot;open=[&quot;+main +
slideList[i]+&quot;]&quot;);
                open(sectionDir + File.separator + sectionList[i]);
                run(&quot;Set Scale...&quot;, &quot;distance=0 global&quot;);
                //Get image dimensions
                height = getHeight();
                width = getWidth();
                size = height * width;
                //Scale background subtraction, blur, and line width based on image size
                sigma = round(size * 0.0000008);
                if (round(size * 0.00009) &lt; 1800){
                        radius = round(size * 0.00009);
                } else {
                        radius = 1800;
                }
                line = size * 0.0000003;
                setLineWidth(line);
                //Analyze the channels to create binary masks of red and blue regions
                run(&quot;Gaussian Blur...&quot;,
&quot;sigma=[&quot;+sigma+&quot;]&quot;);
                fluorChan = &quot;C1-&quot; + sectionList[i];
                transChan = &quot;C2-&quot; + sectionList[i];
                nameLength = lengthOf(sectionList[i]) - 4;
                duplFluor = &quot;C1-&quot; + substring(sectionList[i], 0, nameLength) +
&quot;-1.tif&quot;;
                duplTrans = &quot;C2-&quot; + substring(sectionList[i], 0, nameLength) +
&quot;-1.tif&quot;;
                run(&quot;Split Channels&quot;);
                run(&quot;Grays&quot;);
                run(&quot;Invert&quot;);
                run(&quot;Subtract Background...&quot;,
&quot;rolling=[&quot;+radius+&quot;]&quot;);
                selectWindow(fluorChan);
                run(&quot;Grays&quot;);
                run(&quot;Duplicate...&quot;, &quot; &quot;);
                selectWindow(transChan);
                run(&quot;Duplicate...&quot;, &quot; &quot;);
                selectWindow(duplFluor);
                run(&quot;Auto Threshold&quot;, &quot;method=Otsu ignore_black
white&quot;);
                run(&quot;Dilate&quot;);
                run(&quot;Dilate&quot;);
                run(&quot;Dilate&quot;);
                run(&quot;Dilate&quot;);
                run(&quot;Dilate&quot;);
                run(&quot;Dilate&quot;);
                run(&quot;Dilate&quot;);
                run(&quot;Dilate&quot;);
                selectWindow(duplTrans);
                run(&quot;Auto Threshold&quot;, &quot;method=Otsu ignore_black
white&quot;);
                run(&quot;Erode&quot;);
                run(&quot;Erode&quot;);
                run(&quot;Erode&quot;);
                run(&quot;Erode&quot;);
                run(&quot;Erode&quot;);
                run(&quot;Erode&quot;);
                run(&quot;Erode&quot;);
                run(&quot;Erode&quot;);
                run(&quot;Erode&quot;);
                run(&quot;Erode&quot;);
                run(&quot;Erode&quot;);
                run(&quot;Erode&quot;);
                imageCalculator(&quot;Subtract create&quot;, duplTrans, duplFluor);
                binScar = &quot;Result of &quot; + duplTrans;
                selectWindow(binScar);
                run(&quot;Analyze Particles...&quot;, &quot;size=100-Infinity show=Nothing
clear add&quot;);
               
                //Find the biggest ROI
                numROIs = roiManager(&quot;count&quot;);
                if (numROIs>1) {
        area=newArray(numROIs);
        for (j=0; j<numROIs;j++){
               roiManager(&quot;select&quot;, j);
                getStatistics(area[j], mean, min, max, std, histogram);
        }
        areaLarge = 0;
        for (j=0; j&lt;numROIs; j++){
                if (area[j]>areaLarge) {
                        areaLarge=area[j];
                        large = j;
                }
        }
                } else {
                        roiManager("select", 0);
        getStatistics(area, mean, min, max, std, histogram);
        areaLarge = area;
        large = 0;
                }
                //Print stats
                print(substring(sectionList[i], 0, 4) + ";" + substring(sectionList[i], 5,
9) + ";" + substring(sectionList[i], 10, 12) + ";" + numROIs + ";" +
areaLarge); //+ ";" + substring(sectionList[i], 5, 9) + ";" +
substring(sectionList[i], 10, 12) + ";" numROIs + ";" + areaLarge);
               
                //Create thumbnails for validation
                imageCalculator("Difference create 32-bit", transChan, fluorChan);
                imgScar = "Result of " + transChan;
                selectWindow(imgScar);
                run("16-bit");
                run("Enhance Contrast", "saturated=0.35");
                selectWindow(fluorChan);
                run("Enhance Contrast", "saturated=0.35");
                run("Merge Channels...", "c1=["+fluorChan+"] c3=["+imgScar+"] create");
                run("RGB Color");
                roiManager("Select", large);
                run("Draw", "slice");
                run("Select None");
                newWidth = 256;
                newHeight = getHeight() * round(256 / getHeight());
                run("Size...", "width=["+newWidth+"] height=["+newHeight+"] depth=1
constrain average interpolation=Bicubic");
                run("8-bit Color", "number=256");
                thumbName = sectionList[i];
                imagePath = mousePath + thumbName;
                saveAs("Tiff", imagePath);
                close("*");
                call("java.lang.System.gc");
        }
       
        //Save the log with all the measurements
        logPath = mousePath + substring(mouseList[k], 0, 4) + "_results.txt";
        selectWindow("Log");
        saveAs("Text", logPath);
       
        //Close everything and run garbage collection to free up memory
        run("Close All");
        call("java.lang.System.gc");
       
        //Make sure the log is clear before starting the next mouse
        print("\\Clear");
        print("MOUSE #" + ";" + "SLIDE #" + ";" + "SECTION ID" + ";" + "ROIs" + ";"
+ "MAX AREA");
}

Thanks in advance!



--
Sent from: http://imagej.1557.x6.nabble.com/

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html