How to open 2 differents images in the same macro in batch processing

Hello,
In a folder i’ve got 2 populations of images, one in .tif (for segmentation) and the other in .jpg for a control. I wrote a macro for batch processing a directory. First the macro do the analysis of the .tif images and put the results in the ROI manager. And now i want in a second time open my image in JPG and do a overlay between hte JPG and the results in the ROI. And same it in a directory.
this is my macro:

path = getDirectory(""); 
outputFolder = getDirectory(""); 
filelist = getFileList(path); // charge toute les images
roiManager("reset");
for (i=0; i< filelist.length; i++) {
     // Ne marche que sur des .tif
     if (endsWith(filelist[i], ".tif")) {
         // Ouverture des fichiers via Bio-format
//run("Bio-Formats Importer", "open=[" + path + filelist[i] + "] autoscale color_mode=Grayscale rois_import=[ROI manager] view=Hyperstack stack_order=XYCZT");
open( path + filelist[i], "tif" );
roiManager("reset");
run("Invert");
setOption("BlackBackground", true);
run("Make Binary");
run("Invert");
run("Median...", "radius=2");
run("Dilate");
run("Dilate");
run("Invert");
run("Set Measurements...", "area display redirect=None decimal=2");
run("Analyze Particles...", "size=30000-Infinity show=Outlines display exclude include add");
        for (i=0; i< filelist.length; i++) {
	if (endsWith(filelist[i], ".jpg")) {
     	open( path + filelist[i], "jpg" );
     	imageTitle = getTitle();
    	selectWindow(imageTitle);
if (roiManager("Count") >0){
	run("From ROI Manager");
	saveAs("Jpeg", outputFolder+getTitle);
	run("Close All");
}
}
}
}
}

My macro do a loop in the second part. And i don’t know correct this.

I hope someone could help me!

Regards
Romain

Hi Romain,

do you need a second loop?
If you always have a jpg file for each .tif file, then you can avoid the loop.
You can determine the name of the jpg file from the tif file you open with something like
jpgfile = substring(filelist[i],0,lengthOf(filelist[i])-4)+".jpg";
At this point, after the particle analysis instruction, you just open the file and transfer the ROIs on it. No need for an internal loop.

I hope it helps.
Best,
Giovanni

Hi Giovanni,
I’ve got my tif files with with name X_SimpleSegmentation.tif and my jpg files with this name x.jpg.
I want to:
- 1-open the X_SimpleSegmentation.tif file -> do the analyse and put the result in ROI manager
| 2- open the X.jpg file
| 3- Overlay the ROI on the X.jpg
| 4-Save the overlay in the Result folder
- 5 Close all the images
6- Start a new analyse
7- Etc…
The steps between 1 to 5 it’s my loop to analyse a folder
I don’t now how to open the JPG file associate to the tif file in this loop.

Romain

Hi @RomainM,

First, in your first message, there is a big problem. You have one loop within anoather, but with the same variable i. It can’t work.

For your problem, you should invert your search.
First look for you .jpg image and get the root of the name, just as @gcardone said :

Name = substring(filelist[i],0,lengthOf(filelist[i])-4);

Then you can construct the name of your tif file :

TifName = Name + "_SimpleSegmentation.tif";

Nico

Hi Romain,

this doesn’t change the approach I proposed. Only difference is that when you determine the name of the corresponding jpg file, you need to remove from the file name not just the extension (the last 4 characters) but also the string _SimpleSegmentation (an additional 19 characters), that is the last 23 characters. Using the same substring command, this time the name of the jpg file would obtained with
jpgfile = substring(filelist[i],0,lengthOf(filelist[i])-23)+".jpg"
This would be at step 2 of your workflow.
Look at the documentation if it is not clear how the parameters for this command are set.

Giovanni

PS: and @VirtualSlide is right: never user the same iterator variable in two nested loops.

Thanks a lot!! I will try this today!

Have a nice day

Ok, i’ve tried a little bit but i don’t know what to do with this command line. I’ve to specify that my 2 images jpg and tif are differents. My jpg file is a phase contrast image and my tif file is a segmentation of my jpg so it’s in black and white. I want to put my ROI done with my tif file on my phase contrast image whitch is in jpg.
I am a beginner in imagej macro so i don’t know how to mix this command line in my macro.
THX in advance
Romain

Ok non my bad. It’s works…
This my macro corrected:

path = getDirectory("");
outputFolder = getDirectory("");
filelist = getFileList(path); // charge toute les images
roiManager(“reset”);
for (i=0; i< filelist.length; i++) {
// Ne marche que sur des .tif
if (endsWith(filelist[i], “.tif”)) {
// Ouverture des fichiers via Bio-format
//run(“Bio-Formats Importer”, “open=[” + path + filelist[i] + “] autoscale color_mode=Grayscale rois_import=[ROI manager] view=Hyperstack stack_order=XYCZT”);
open( path + filelist[i], “tif” );
roiManager(“reset”);
run(“Invert”);
setOption(“BlackBackground”, true);
run(“Make Binary”);
run(“Invert”);
run(“Median…”, “radius=2”);
run(“Dilate”);
run(“Dilate”);
run(“Invert”);
run(“Set Measurements…”, “area display redirect=None decimal=2”);
run(“Analyze Particles…”, “size=30000-Infinity show=Outlines display exclude include add”);
jpgfile = substring(filelist[i],0,lengthOf(filelist[i])-24)+".jpg";
open( path + jpgfile, “tif” );
imageTitle = getTitle();
selectWindow(imageTitle);
if (roiManager(“Count”) >0){
run(“From ROI Manager”);
saveAs(“Jpeg”, outputFolder+getTitle);
run(“Close All”);
}
}
}

Thx a lot!

Romain