generalize macro for different images

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

generalize macro for different images

Finn Peters
Hi,


I want to write a macro that cuts out a selection of one image and
pastes it into a second image. Meanwhile some contrast adjustments are made.

For a set of two defined images it works with the code beneath. My
problem is to generalize this to any set of images with different names.
If I want to run the macro e.g. with the images "xyz_green" and
"xyz_red" it doesn't work.

Optimally the macro should automatically load two consecutive images
from a folder, process and close them and then load the next two images
in the folder and so on.

Has anybody an idea how to do this?

selectWindow("Image 1_red.TIF");

run("8-bit");

run("Red");

run("Brightness/Contrast..."); setMinAndMax(3, 7);

setAutoThreshold("Default");

//run("Threshold...");

setThreshold(3, 7);

run("Create Selection");

run("RGB Color");

run("Copy");

selectWindow("Image 1_green.TIF");

run("Brightness/Contrast..."); setMinAndMax(16, 44);

run("Paste");

run("Select None");

saveAs("Tiff", "C:\\Users\\Admins\\Desktop\\test\\test source\\Image
1_merged.tif");

close();

close();

cheers,

Finn
Reply | Threaded
Open this post in threaded view
|

Re: generalize macro for different images

dscho
Hi Finn,

On Sat, 20 Aug 2011, Finn Peters wrote:

> My problem is to generalize this to any set of images with different
> names. If I want to run the macro e.g. with the images "xyz_green" and
> "xyz_red" it doesn't work.

Probably the best way would be to work with selectImage(id) rather than
selectWindow(name).

> Optimally the macro should automatically load two consecutive images
> from a folder, process and close them and then load the next two images
> in the folder and so on.

inputFolder = getDirectory("Input folder");
files = getFileList(inputFolder);
count = 0;
id = 0;
for (i = 0; i < files.length; i++)
        if (endsWith(files[i]), ".TIF")) {
                open(inputFolder + files[i]);
                id2 = id;
                id = getImageID();
                counter++;
                if ((counter % 2) == 0) {
                        selectImage(id2);
                        ... do stuff on the first image ...
                        selectImage(id);
                        ... do stuff on the second image ...
                        ... save and do not forget to close ...
                }

Ciao,
Johannes
Reply | Threaded
Open this post in threaded view
|

Re: generalize macro for different images

Finn Peters
Hi Johannes,

thanks a lot! It nearly works now. The only problem I still have is how
to save the merged image.

Is there a command to create a new folder with the name "merged" in the
input folder directory?

If yes I would like to save the modified image in that folder with the
name of the active image plus the ending "_merged". How can I implement
this with the saveAs command?

Something like this?
saveAs("Tiff", input folder+image.name+"_merged");


cheers,

Finn

> Hi Finn,
>
> On Sat, 20 Aug 2011, Finn Peters wrote:
>
>> My problem is to generalize this to any set of images with different
>> names. If I want to run the macro e.g. with the images "xyz_green" and
>> "xyz_red" it doesn't work.
> Probably the best way would be to work with selectImage(id) rather than
> selectWindow(name).
>
>> Optimally the macro should automatically load two consecutive images
>> from a folder, process and close them and then load the next two images
>> in the folder and so on.
> inputFolder = getDirectory("Input folder");
> files = getFileList(inputFolder);
> count = 0;
> id = 0;
> for (i = 0; i<  files.length; i++)
> if (endsWith(files[i]), ".TIF")) {
> open(inputFolder + files[i]);
> id2 = id;
> id = getImageID();
> counter++;
> if ((counter % 2) == 0) {
> selectImage(id2);
> ... do stuff on the first image ...
> selectImage(id);
> ... do stuff on the second image ...
> ... save and do not forget to close ...
> }
>
> Ciao,
> Johannes
>