Login  Register

Re: Help with ImageJ macro code

Posted by crieken on Feb 01, 2008; 12:16pm
URL: http://imagej.273.s1.nabble.com/Help-with-ImageJ-macro-code-tp3697312p3697316.html

Hi Michael,
Thanks for the advice.  I have rewritten the code as you have suggested, but I'm still getting errors.  The good news is, I think I know why, but the bad is that I have no idea how to solve it.  When I run the macro and open my images, I notice that the active image window (the one on top) will not remain consistent from run to run.  I therefore believe that ImageJ is closing some of the channels I want to retain (A, C, D, etc...) and keeping some of the junk channels I want to discard, thus causing errors in the back end because the imageID that I direct ImageJ to no longer exists.  I will try rewriting the code so that no channels are closed, but how can I be sure that I'm getting the correct imageID when I don't know how to control which window is active?  I could try to identify the windows based on their original window names using a string search (ie 402nm ch:1), but then I run back into the problem that these window names are being reset to "1".  Aaargh!  Sorry to be such a bother, but does anyone have a good fix for this?  Many thanks!
Chris


Michael Schmid-3 wrote
Hi Chris,

you are right that ImageIDs are negative numbers. They stay
valid until an image is closed. A new image that you open gets
a new ID that is the last ID used minus one.
(strictly speaking, "image" means the ImagePlus class of ImageJ)

See the selectImage(id) and getImageID entries on
   http://rsb.info.nih.gov/ij/developer/macro/functions.html

It might be that your problems are caused by
   run("Put Behind [tab]");
since some operations on Windows happen in a different thread
that may be slower than the thread executing the macro.

Therefore, to access the images created I would simply try:
   Z = getImageID();
   W = getImageID()+1; //the image created before Z


Michael
________________________________________________________________

On 1 Feb 2008, at 01:42, crieken wrote:

> Hi Justin,
> Unfortunately, it doesn't appear that each image is getting a  
> unique ID...
> unless I'm not using the code correctly.  In the end I need to have  
> the
> windows named "A, C, D, E, F, gamma, W, Z" in order to do particle  
> analysis.
> I keep getting errors, so I printed the image IDs and there is  
> always a
> repeat, but it seems to vary.  Are the image IDs usually expressed
> like"-10", "-71", etc.?  Thanks for your advice.
> Chris
>
>
> //Selects directory to save files to
> dir = getDirectory("Select save directory");
>
> //Prompt for 402nm image
> var violet = "Open a 402nm image";
> requires("1.34m");
>   title = "Untitled";
>   width=512; height=512;
>   Dialog.create("402nm");
>   Dialog.addMessage(violet);
>   Dialog.show();
>
> //Run ICS opener
> run("ICS/IDS... ");
>
> //Closes 402 unassigned, 402 background
> close();
> close();
> Z = getImageID();
> run("Put Behind [tab]");
> W = getImageID();
>
> //Prompt for 488nm image
> var blue = "Open a 488nm image";
> requires("1.34m");
>   title = "Untitled";
>   width=512; height=512;
>   Dialog.create("488nm");
>   Dialog.addMessage(blue);
>   Dialog.show();
>
> //Run ICS opener
> run("ICS/IDS... ");
>
> //Converts stack to images
> run("Convert Stack to Images");
>
> //Closes 488 unassigned, 488 background, E_488
> close();
> close();
> close();
> D = getImageID();
> run("Put Behind [tab]");
> C = getImageID();
> run("Put Behind [tab]");
> A = getImageID();
>
>
> //Prompt for 561nm image
> var green = "Open a 561nm image";
> requires("1.34m");
>   title = "Untitled";
>   width=512; height=512;
>   Dialog.create("561nm");
>   Dialog.addMessage(green);
>   Dialog.show();
>
> //Run ICS opener
> run("ICS/IDS... ");
>
> //Converts stack to images
> run("Convert Stack to Images");
>
> //Closes 561 unassigned, 561 background
> close();
> close();
> gamma = getImageID();
> run("Put Behind [tab]");
> F = getImageID();
> run("Put Behind [tab]");
> E = getImageID();
>
> //Prints image IDs
> print(A);
> print(C);
> print(D);
> print(E);
> print(F);
> print(gamma);
> print(W);
> print(Z);
>
>
> //Renames windows
> selectWindow(A);
> rename("A");
> selectWindow(C);
> rename("C");
> selectWindow(D);
> rename("D");
> selectWindow(E);
> rename("E");
> selectWindow(F);
> rename("F");
> selectWindow(gamma);
> rename("gamma");
> selectWindow(W);
> rename("W");
> selectWindow(Z);
> rename("Z");
>
>
> Justin McGrath wrote:
>>
>> I do not know why your images get renamed, but I think the easiest  
>> way
>> to solve this is to use the getImageID() function, which returns a
>> unique ID for the active image, and then use the selectImage(id)
>> function to activate the image later.  It has a few benefits: it's
>> faster, the IDs are unique, and the IDs don't change throughout macro
>> execution.
>>
>> I think you can just replace the rename() and selectWindow()
>> statements you have now, and it should work.  However, I believe
>> imageCalculator("or", img1, img2) does the same thing as all of those
>> select-copy-select-pastes, so you might use that in order to make the
>> code more succinct.
>>
>> Justin
>>
>>
>>
>> On 1/30/08, crieken <chris.rieken@gmail.com> wrote:
>>> Hi all, I hope this is an appropriate place for this question.  I'm
>>> writing
>>> a
>>> macro to put together a mask from three images with multiple  
>>> channels.  I
>>> am
>>> making progress on the macro, but as I test it, I'm running into a
>>> problem.
>>> When the 402nm image is opened, the windows "Z" and "W" are named
>>> properly.
>>> When the 488nm image is opened, the windows "A", "C", and "D" are  
>>> named
>>> fine, but the "Z" and "W" windows are renamed to "1".  This  
>>> messes up the
>>> creation of the mask and will mess up the analysis that will come  
>>> further
>>> down the line.  I have even tried saving each image individually,  
>>> but the
>>> names still get reset.  This only occurs occasionally, though.  Does
>>> anyone
>>> have any ideas?  I can email the original images if it would help.
>>> Thanks
>>> in advance!  BTW, this may not be the most elegant code, but go  
>>> easy, I'm
>>> a
>>> beginner :confused:.
>>> Chris
>>>
>>> //Selects directory to save files to
>>> dir = getDirectory("Select save directory");
>>>
>>> //Prompt for 402nm image
>>> var violet = "Open a 402nm image";
>>> requires("1.34m");
>>>   title = "Untitled";
>>>   width=512; height=512;
>>>   Dialog.create("402nm");
>>>   Dialog.addMessage(violet);
>>>   Dialog.show();
>>>
>>> //Run ICS opener
>>> run("ICS/IDS... ");
>>>
>>> //Closes 402 unassigned, 402 background, and renames channel 2 as  
>>> "Z" and
>>> channel 1 as "W"
>>> close();
>>> close();
>>> rename("Z");
>>> run("Put Behind [tab]");
>>> rename("W");
>>>
>>> //Prompt for 488nm image
>>> var blue = "Open a 488nm image";
>>> requires("1.34m");
>>>   title = "Untitled";
>>>   width=512; height=512;
>>>   Dialog.create("488nm");
>>>   Dialog.addMessage(blue);
>>>   Dialog.show();
>>>
>>> //Run ICS opener
>>> run("ICS/IDS... ");
>>>
>>> //Converts stack to images
>>> run("Convert Stack to Images");
>>>
>>> //Closes 488 unassigned, 488 background, E_488 and renames  
>>> channel 3 as
>>> "D",
>>> channel 2 as "C", and channel 1 as "A"
>>> close();
>>> close();
>>> close();
>>> rename("D");
>>> run("Put Behind [tab]");
>>> rename("C");
>>> run("Put Behind [tab]");
>>> rename("A");
>>>
>>> //Prompt for 561nm image
>>> var green = "Open a 561nm image";
>>> requires("1.34m");
>>>   title = "Untitled";
>>>   width=512; height=512;
>>>   Dialog.create("561nm");
>>>   Dialog.addMessage(green);
>>>   Dialog.show();
>>>
>>> //Run ICS opener
>>> run("ICS/IDS... ");
>>>
>>> //Converts stack to images
>>> run("Convert Stack to Images");
>>>
>>> //Closes 561 unassigned, 561 background, and renames channel 3 as
>>> "gamma",
>>> channel 2 as "F", and channel 1 as "E"
>>> close();
>>> close();
>>> rename("gamma");
>>> run("Put Behind [tab]");
>>> rename("F");
>>> run("Put Behind [tab]");
>>> rename("E");
>>>
>>> //Create a mask
>>> run("Duplicate...", "title=Mask");
>>> run("Paste Control...");
>>> setPasteMode("OR");
>>> selectWindow("F");
>>> run("Copy");
>>> selectWindow("Mask");
>>> run("Paste");
>>> selectWindow("gamma");
>>> run("Copy");
>>> selectWindow("Mask");
>>> run("Paste");
>>> selectWindow("D");
>>> run("Copy");
>>> selectWindow("Mask");
>>> run("Paste");
>>> selectWindow("C");
>>> run("Copy");
>>> selectWindow("Mask");
>>> run("Paste");
>>> selectWindow("A");
>>> run("Copy");
>>> selectWindow("Mask");
>>> run("Paste");
>>> selectWindow("Z");
>>> run("Copy");
>>> selectWindow("Mask");
>>> run("Paste");
>>> selectWindow("W");
>>> run("Copy");
>>> selectWindow("Mask");
>>> run("Paste");
>>>
>>> --
>>> View this message in context:
>>> http://www.nabble.com/Help-with-ImageJ-macro-code- 
>>> tp15195190p15195190.html
>>> Sent from the ImageJ mailing list archive at Nabble.com.
>>>
>>
>>
>
> --
> View this message in context: http://www.nabble.com/Help-with- 
> ImageJ-macro-code-tp15195190p15218299.html
> Sent from the ImageJ mailing list archive at Nabble.com.