Batch process images with specific ROis

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Batch process images with specific ROis

Francomazzei
Dear all,

I don't have much experience coding but after reading your guides and
tutorials I was able to make a code for measuring the area of the
thresholded pixels in my images. the code looks like this :


#@File(label = "Input directory", style = "directory") input
#@File(label = "Output directory", style = "directory") output
#@String(label = "File suffix", value = ".tif") suffix

processFolder(input);
       
// function to scan folders/subfolders/files to find files with correct
suffix
function processFolder(input) {
        list = getFileList(input);
        for (i = 0; i < list.length; i++) {
                if(File.isDirectory(input + File.separator + list[i]))
                        processFolder("" + input + File.separator + list[i]);
                if(endsWith(list[i], suffix))
                        processFile(input, output, list[i]);
        }
function processFile(input, output, file) {
        setBatchMode(true);
run("Bio-Formats", "open=[" + input + "/" + file +"] autoscale
color_mode=Default rois_import=[ROI manager] view=Hyperstack
stack_order=XYCZT");
  id = getImageID(); // get original image id
run("Duplicate...", " ");
run("8-bit");
run("Subtract Background...", "rolling=50");
run("Median...", "radius=1.5");
save(output + "/Processed_filter_" + file);

setAutoThreshold("Otsu dark");
//run("Threshold...");

run("Create Mask");
run("Fill Holes");
run("Analyze Particles...", "add");
selectImage(id);
roiManager("Show All with labels"); // overlay ROIs
        roiManager("Deselect");
        roiManager("Measure"); // measure on original image
       
        // save ROIs for current image
        roiManager("Deselect");
        roiManager("Save", output+ "/" + file + "_Amyloid_ROI.zip"); // saves Rois
zip file
        roiManager("Deselect");
        roiManager("Delete");

saveAs("Results", output+ "/" + file + "_Amyloid_Results.csv") ;
run("Clear Results");
}

Now I would like to apply this same analysis but to specific previously made
ROIs in the same images.
To be more clear what I would like to do is:

1. Open image file from a specified image directory using bioformats.
2. Load matching ROIs previously saved as zip into the image.  
3. Perform the analysis described in the macro I posted above to each ROI
and save the results separately.
5. close all windows and start over with another image and a different set
of ROIs ( each image has a specific and different set of ROIs).

Is it possible to do so? I am very lost at the moment, any help would be
highly appreciated.

Thank you so much beforehand.

Best,

Franco  
 



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

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Batch process images with specific ROis

gnelson
HiFranco,

I think what you want is a loop to got through each ROI individually once
you have added them all to the ROI manager.  Is that correct?  I have done
something similar where I counted local maxima inside each ROI using the
code below:

for(j=0; j<roiManager("count"); j++) {
                        roiManager("select", j);
                        run("Find Maxima...", "noise="+tolerance+" output=[Count]");
// run("Find Maxima...", "noise="+tolerance+" output=[Point Selection]");
                        run("Add Selection...");
                        }
                saveAs("Results",
dir2+File.separator+originalImage+"_nuc_coloc_and_h2ax.xls"); //This is
savign as it goes along in case it crashes

                selectWindow(originalImage);

I then saved an image with the overlays burned on so I had a history of what
I did.  Plus, a separate csv file is saved for each image.
Obviously, you can alter to only measure whatever you are interested in.
Does that help?

Glyn.



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

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Batch process images with specific ROis

Francomazzei
Hi Glyn, thank you for your reply.

Yes, I would like to loop through each Roi individually.

I tried adding the fraction of the macro you posted but unfortunately, it
didn't work.

I believe that the main problem I have is that the analysis I want to
perform exports the results of "analyze particles" to the ROI manager, and
if I add  "roiManager("reset") " to delete my previously loaded ROIs  the
function "for(j=0; j<roiManager("count"); j++) { roiManager("select", j); "
does not work properly.

I will keep trying and post if I any solution.

Thank you again!
 




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

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Batch process images with specific ROis

gnelson
Hi Franco,

Apologies, I was rushing yesterday and literally just hacked the code from
my own macro, so variables were set wrongly etc.  I've adjusted your macro
below.  It uses the binary mask to find the objects, then analyses those
objects on the original image then saves the ROI list, the analysis and the
binary image.  I have set the measurements at the beginning to include a few
stats and the image label- this means you could potentially keep saving all
the output to one csv (since you can identify which image each measurement
comes from), which I find easier for analysis later.  To do this, just
change the csv name to a fixed name rather than the filename, and don't
clear the results after each loop.  Obviously you need to change the stats
to the ones you want or don't want too.  

You don't say if the images are a single channel or not.  If they are
multichannel and there is a specific channel you wish to analyse with the
ROIs you have made, then you need to add a line to switch to that channel.
Since your duplicate command is empty, I assume they are single channel.
I've tried to keep the rest of your code as you had it, so I hope you can
see how it works.  

Glyn

#@File(label = "Input directory", style = "directory") input
#@File(label = "Output directory", style = "directory") output
#@String(label = "File suffix", value = ".tif") suffix

processFolder(input);
//specify the measurements you want and the image label (plus ROI
co-ordinates)
run("Set Measurements...", "area mean standard integrated limit display
redirect=None decimal=3");        
// function to scan folders/subfolders/files to find files with correct
suffix
function processFolder(input) {
        list = getFileList(input);
        for (i = 0; i < list.length; i++) {
                if(File.isDirectory(input + File.separator + list[i]))
                        processFolder("" + input + File.separator +
list[i]);
                if(endsWith(list[i], suffix))
                        processFile(input, output, list[i]);
        }
function processFile(input, output, file) {
        setBatchMode(true);
run("Bio-Formats", "open=[" + input + "/" + file +"] autoscale
color_mode=Default rois_import=[ROI manager] view=Hyperstack
stack_order=XYCZT");
  id = getImageID(); // get original image id
run("Duplicate...", " ");
run("8-bit");
run("Subtract Background...", "rolling=50");
run("Median...", "radius=1.5");
//save(output + "/Processed_filter_" + file);

setAutoThreshold("Otsu dark");
//run("Threshold...");

run("Create Mask");
run("Fill Holes");
run("Analyze Particles...", "add");
rename("mask");
selectImage(id);
roiManager("Measure");
 
        // save ROIs for current image
        roiManager("Deselect");
        roiManager("Save", output+ "/" + file + "_Amyloid_ROI.zip"); //
saves Rois zip file
        roiManager("Deselect");
        roiManager("Delete");

saveAs("Results", output+ "/" + file + "_Amyloid_Results.csv") ;
run("Clear Results");
selectImage("mask");
save(output + "/Processed_filter_" + file);
}



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

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