Login  Register

Problem with ROI Manager in BatchMode

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options Options
Embed post
Permalink
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Problem with ROI Manager in BatchMode

Winfried Wurm
9 posts
Dear ImageJ community,

I´ve encountered some Problems by using the Batchmode together with the ROI Manager in my Macro. I wrote a Macro which counts Foci in a Binary Image, selects the Outline of the Focus in every Slice and adds it to the ROI Manager. The ROIs are than measured in a second Channel.
The Macro works fine if setBatchMode(false). But when I turn it true, the Measurements are completely nonsense.
I think that I´ve tracked the error down to the creation of the Selection in the second Channel. When I let the Macro fill the ROIs in the second Channel the ROI in every Slice has the shape of the ROI of the last added ROI, but are good when I manually select them in the ROI Manager afterwards or when BatchMode is turned off.

Since the Macro is very long I will only add the part of the Code which I think makes the trouble, the measuring in the second Channel.
At this point, the ROI Manager has a least two ROIs added.


                focus++;
                hc=false;
                selectWindow("cell.tif");
                run("Select None");
                m=(roiManager("count")-1);
                for(r=0;r<=m;r++)
                {
                   roiManager("deselect");
                   roiManager("select",r);
                   arg="enlarge="+px;
                   run("Enlarge...", arg);
                   run("Measure")
                   if(getResult("Max")==255)
                    hc=true;
                  run("Fill", "slice");
                }
                selectWindow("foci.tif");
                roiManager("Reset");
                if(hc==true)
                        HC++;
                else
                        EC++;



Can anyone help me fix this Problem?
Thx for Helping in advance
Winfried Wurm
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Problem with ROI Manager in BatchMode

Peter Haub
125 posts
Hi Winfried,

indeed there seems to be a problem.
I have reduce your code and found that opening a set of rois's with
'open' show the behaviour you described : Nothing happens! The
roiManager seems to be not available.
If the set of roi's is opened with roiManager("Open", "C:\\RoiSet.zip")
everything work as expected.

Pretty sure your problem relies on the same IJ internals.
Maybe somebody will have a look for it.

Regards,
Peter

ps: in core a ; is missing after  run("Measure") .


**************** Here is my code **********************
setBatchMode(true);
open("C:\\test.tif");

// not working version
//open("C:\\RoiSet.zip");

// working version
roiManager("Open", "C:\\RoiSet.zip");

selectWindow("test.tif");
run("Select None");
m=(roiManager("count")-1);
for(r=0;r<=m;r++)
{
    roiManager("deselect");
    roiManager("select",r);
    arg="enlarge="+2;
    run("Fill", "slice");
}
setBatchMode("exit and display");
*****************************************************



On 31.10.2011 20:39, Winfried Wurm wrote:

> Dear ImageJ community,
>
> I´ve encountered some Problems by using the Batchmode together with the ROI Manager in my Macro. I wrote a Macro which counts Foci in a Binary Image, selects the Outline of the Focus in every Slice and adds it to the ROI Manager. The ROIs are than measured in a second Channel.
> The Macro works fine if setBatchMode(false). But when I turn it true, the Measurements are completely nonsense.
> I think that I´ve tracked the error down to the creation of the Selection in the second Channel. When I let the Macro fill the ROIs in the second Channel the ROI in every Slice has the shape of the ROI of the last added ROI, but are good when I manually select them in the ROI Manager afterwards or when BatchMode is turned off.
>
> Since the Macro is very long I will only add the part of the Code which I think makes the trouble, the measuring in the second Channel.
> At this point, the ROI Manager has a least two ROIs added.
>
>
> focus++;
> hc=false;
> selectWindow("cell.tif");
> run("Select None");
> m=(roiManager("count")-1);
> for(r=0;r<=m;r++)
> {
>   roiManager("deselect");
>   roiManager("select",r);
>   arg="enlarge="+px;
>   run("Enlarge...", arg);
>   run("Measure")
>   if(getResult("Max")==255)
>   hc=true;
>  run("Fill", "slice");
> }
> selectWindow("foci.tif");
> roiManager("Reset");
> if(hc==true)
> HC++;
> else
> EC++;
>
>
>
> Can anyone help me fix this Problem?
> Thx for Helping in advance
> Winfried Wurm
>
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Problem with ROI Manager in BatchMode

Winfried Wurm
9 posts
In reply to this post by Winfried Wurm
Thank you Peter for trying to solve my Problem.
But my Problems seems to have another reason. Actually I solved my Problem today, but I have to admit that I don´t know why the Macro is working now....

It seems to me now that the Problem was another Function that I run before the Foci counting part that I posted yesterday. That function is measuring the Area of Chromocenters. When I turned it off or behind the Foci measuring today everything worked fine. But I don’t know why! The function measures in a completely different Image and shares no variable with the Foci measuring function.  And when I call that function AFTER the Foci Funtion there is no Problem.

So why is that working?
CountMeasureFoci("cell.tif","foci.tif",px[con],mpx[con],msl[con]);    
cellarea=Cellar("cellar.tif");
chromarea=Cellar("chrom.tif");

And this not?
cellarea=Cellar("cellar.tif");
chromarea=Cellar("chrom.tif");
CountMeasureFoci("cell.tif","foci.tif",px[con],mpx[con],msl[con]);

And why did the Problem only occur in BatchMode? I have no Idea what has happend. Maybe someone can tell what went wrong? Because even if it works now. I would like to understand why.

Here is the function that seems to make the Problem:

function Cellar(window)
{
    selectWindow(window);
    area=0;
    for(z=1;z<=nSlices;z++)
    {    
        setSlice(z);
        run("Select None");
        run("Measure");
        if (getResult("Max")==255)    
        {
            for(x=1;x<getWidth;x++)
            {                    
                makeRectangle(x, 0, 1, getHeight);
                run("Measure");
                if (getResult("Max")==255)
                {
                    for(y=1;y<getHeight;y++)
                    {
                        if(getPixel(x,y)==255)
                        {
                            doWand(x-1,y);
                            run("Measure");
                            m=getResult("Area");
                            area+=m;
                            run("Fill", "slice");
                            makeRectangle(x, 0, (getWidth-x), getHeight);
                            run("Measure");
                            if (getResult("Max")==255)
                            {
                                makeRectangle(x, 0, 1, getHeight);
                                run("Measure");
                                if (getResult("Max")<255)
                                    y=getHeight;
                            }
                            else
                            {
                                x=getWidth;
                                y=getHeight;
                            }
                        }
                    }
                   
               
                }                
            }
        }
    }
   return area;
}
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

can I divide a circular ROI into equal slices?

Aryeh Weiss
468 posts
Is there a plugin or option to divide a circular ROI in a set of slices
(like pie slices). I foudn something in the archives for dividing
circular ROIs into hemispheres and quadrants. I was wondering if someone
has a more general version of that?

--aryeh
--
Aryeh Weiss
School of Engineering
Bar Ilan University
Ramat Gan 52900 Israel

Ph:  972-3-5317638
FAX: 972-3-7384051
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Problem with ROI Manager in BatchMode

BenTupper
122 posts
In reply to this post by Winfried Wurm
Hi,

On Nov 1, 2011, at 12:44 PM, Winfried Wurm wrote:

> Thank you Peter for trying to solve my Problem.
> But my Problems seems to have another reason. Actually I solved my Problem today, but I have to admit that I don´t know why the Macro is working now....
>
> It seems to me now that the Problem was another Function that I run before the Foci counting part that I posted yesterday. That function is measuring the Area of Chromocenters. When I turned it off or behind the Foci measuring today everything worked fine. But I don’t know why! The function measures in a completely different Image and shares no variable with the Foci measuring function.  And when I call that function AFTER the Foci Funtion there is no Problem.
>
> So why is that working?
> CountMeasureFoci("cell.tif","foci.tif",px[con],mpx[con],msl[con]);    
> cellarea=Cellar("cellar.tif");
> chromarea=Cellar("chrom.tif");
>
> And this not?
> cellarea=Cellar("cellar.tif");
> chromarea=Cellar("chrom.tif");
> CountMeasureFoci("cell.tif","foci.tif",px[con],mpx[con],msl[con]);


In my experience, selecting windows/images by name can be problematic under batchMode.  It must be that looking up the object by name creates race conditions or something.  To avoid this issue I would use the image ID instead of the image name.  Like this...

id = getImageID("cellar.tif");
selectImage(id);

I suggest that you change the argument to your Cellar function from a string to an image ID.  Really, you need only change ...

> selectWindow(window);

to

selectImage(window);

... in the function.  I can't know this will correct the problem for sure, but I would be interested to know if the following works for you after you modify the function.

//start

cellarID = getImageID("cellar.tif");
chromID = getImageID("chrom.tif");

cellarea=Cellar(cellarID);
chromarea=Cellar(chromID);

CountMeasureFoci("cell.tif","foci.tif",px[con],mpx[con],msl[con]);

// end

Cheers,
Ben


>
> And why did the Problem only occur in BatchMode? I have no Idea what has happend. Maybe someone can tell what went wrong? Because even if it works now. I would like to understand why.
>
> Here is the function that seems to make the Problem:
>
> function Cellar(window)
> {
>    selectWindow(window);
>    area=0;
>    for(z=1;z<=nSlices;z++)
>    {    
>        setSlice(z);
>        run("Select None");
>        run("Measure");
>        if (getResult("Max")==255)    
>        {
>            for(x=1;x<getWidth;x++)
>            {                    
>                makeRectangle(x, 0, 1, getHeight);
>                run("Measure");
>                if (getResult("Max")==255)
>                {
>                    for(y=1;y<getHeight;y++)
>                    {
>                        if(getPixel(x,y)==255)
>                        {
>                            doWand(x-1,y);
>                            run("Measure");
>                            m=getResult("Area");
>                            area+=m;
>                            run("Fill", "slice");
>                            makeRectangle(x, 0, (getWidth-x), getHeight);
>                            run("Measure");
>                            if (getResult("Max")==255)
>                            {
>                                makeRectangle(x, 0, 1, getHeight);
>                                run("Measure");
>                                if (getResult("Max")<255)
>                                    y=getHeight;
>                            }
>                            else
>                            {
>                                x=getWidth;
>                                y=getHeight;
>                            }
>                        }
>                    }
>
>
>                }                
>            }
>        }
>    }
>   return area;
> }
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: can I divide a circular ROI into equal slices?

dscho
1631 posts
In reply to this post by Aryeh Weiss
Hi Aryeh,

On Tue, 1 Nov 2011, Aryeh Weiss wrote:

> Is there a plugin or option to divide a circular ROI in a set of slices
> (like pie slices). I foudn something in the archives for dividing
> circular ROIs into hemispheres and quadrants. I was wondering if someone
> has a more general version of that?

I just had a little macro fun and came up with this one (beware: it
expects the current selection to be an oval ROI, and it has no error
handling whatsoever):

-- snipsnap --
// get the center and radii
getSelectionBounds(x, y, width, height);
radiusX = width / 2;
radiusY = height / 2;
centerX = x + radiusX;
centerY = y + radiusY;

// ask the user how many slices she wants
pieSliceCount = getNumber("How many slices?", 4);
angleSteps = 2 * PI * (radiusX + radiusY) / 4 / pieSliceCount;

// make the slices and add them to the ROI Manager
for (i = 0; i < pieSliceCount; i++) {
        startAngle = 2 * PI * i / pieSliceCount;
        endAngle = 2 * PI * (i + 1) / pieSliceCount;
        x = newArray(angleSteps + 2);
        y = newArray(angleSteps + 2);
        for (step = 0; step <= angleSteps; step++) {
                angle = startAngle + (endAngle - startAngle)
                        * step / angleSteps;
                x[step] = centerX + radiusX * cos(angle);
                y[step] = centerY + radiusY * sin(angle);
        }
        x[angleSteps + 1] = centerX;
        y[angleSteps + 1] = centerY;
        makeSelection("polygon", x, y);
        roiManager("Add");
}
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Problem with ROI Manager in BatchMode

Peter Haub
125 posts
In reply to this post by Winfried Wurm
Hi Winfried,
Hi Wayne,

sorry, my description was not precise enough.

The problem has - as far as I can see in the IJ code - to do with the IJ
internal management of the RoiManager.
In the BatchMode there is a special handling of the RoiManager.
So the behaviour of your macro depends on the sequence of activation the
BatchMode and first time calling the RoiManager.
You solved the problem by changing that sequence.

The idea of my code was to attract someones attention who can solve this.

The problem can be visualized more clearly by changing my code and to
open the RoiManager before activating the BatchMode.
This code works fine:

**************** Here is my 'changed' code **********************
open("C:\\RoiSet.zip");

setBatchMode(true);

open("C:\\test.tif");

// It's working if this call is previous to the batch mode activation
!!!!!!!!
//open("C:\\RoiSet.zip");

selectWindow("test.tif");
run("Select None");
m=(roiManager("count")-1);
for(r=0;r<=m;r++)
{
    roiManager("deselect");
    roiManager("select",r);
    arg="enlarge="+2;
    run("Fill", "slice");
}
setBatchMode("exit and display");
*****************************************************

Peter

On 01.11.2011 17:44, Winfried Wurm wrote:

> Thank you Peter for trying to solve my Problem.
> But my Problems seems to have another reason. Actually I solved my Problem today, but I have to admit that I don´t know why the Macro is working now....
>
> It seems to me now that the Problem was another Function that I run before the Foci counting part that I posted yesterday. That function is measuring the Area of Chromocenters. When I turned it off or behind the Foci measuring today everything worked fine. But I don’t know why! The function measures in a completely different Image and shares no variable with the Foci measuring function.  And when I call that function AFTER the Foci Funtion there is no Problem.
>
> So why is that working?
> CountMeasureFoci("cell.tif","foci.tif",px[con],mpx[con],msl[con]);
> cellarea=Cellar("cellar.tif");
> chromarea=Cellar("chrom.tif");
>
> And this not?
> cellarea=Cellar("cellar.tif");
> chromarea=Cellar("chrom.tif");
> CountMeasureFoci("cell.tif","foci.tif",px[con],mpx[con],msl[con]);
>
> And why did the Problem only occur in BatchMode? I have no Idea what has happend. Maybe someone can tell what went wrong? Because even if it works now. I would like to understand why.
>
> Here is the function that seems to make the Problem:
>
> function Cellar(window)
> {
>      selectWindow(window);
>      area=0;
>      for(z=1;z<=nSlices;z++)
>      {
>          setSlice(z);
>          run("Select None");
>          run("Measure");
>          if (getResult("Max")==255)
>          {
>              for(x=1;x<getWidth;x++)
>              {
>                  makeRectangle(x, 0, 1, getHeight);
>                  run("Measure");
>                  if (getResult("Max")==255)
>                  {
>                      for(y=1;y<getHeight;y++)
>                      {
>                          if(getPixel(x,y)==255)
>                          {
>                              doWand(x-1,y);
>                              run("Measure");
>                              m=getResult("Area");
>                              area+=m;
>                              run("Fill", "slice");
>                              makeRectangle(x, 0, (getWidth-x), getHeight);
>                              run("Measure");
>                              if (getResult("Max")==255)
>                              {
>                                  makeRectangle(x, 0, 1, getHeight);
>                                  run("Measure");
>                                  if (getResult("Max")<255)
>                                      y=getHeight;
>                              }
>                              else
>                              {
>                                  x=getWidth;
>                                  y=getHeight;
>                              }
>                          }
>                      }
>
>
>                  }
>              }
>          }
>      }
>     return area;
> }
>
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Problem with ROI Manager in BatchMode

Rasband, Wayne (NIH/NIMH) [E]
1064 posts
On Nov 1, 2011, at 3:28 PM, Peter Haub wrote:

> Hi Winfried,
> Hi Wayne,
>
> sorry, my description was not precise enough.
>
> The problem has - as far as I can see in the IJ code - to do with the IJ
> internal management of the RoiManager.
> In the BatchMode there is a special handling of the RoiManager.
> So the behaviour of your macro depends on the sequence of activation the
> BatchMode and first time calling the RoiManager.
> You solved the problem by changing that sequence.
>
> The idea of my code was to attract someones attention who can solve this.

The open() macro function is unable to open ROI sets if the macro is running in batch mode and the ROI Manager is not open. I did not see an easy fix for this problem so the ImageJ 1.46a4 daily build displays an error message in this situation. You can easily work around this problem by using

   roiManager("Open", path_to_ROI_set);

to open ROI sets.

-wayne

> The problem can be visualized more clearly by changing my code and to
> open the RoiManager before activating the BatchMode.
> This code works fine:
>
> **************** Here is my 'changed' code **********************
> open("C:\\RoiSet.zip");
>
> setBatchMode(true);
>
> open("C:\\test.tif");
>
> // It's working if this call is previous to the batch mode activation
> !!!!!!!!
> //open("C:\\RoiSet.zip");
>
> selectWindow("test.tif");
> run("Select None");
> m=(roiManager("count")-1);
> for(r=0;r<=m;r++)
> {
>    roiManager("deselect");
>    roiManager("select",r);
>    arg="enlarge="+2;
>    run("Fill", "slice");
> }
> setBatchMode("exit and display");
> *****************************************************
>
> Peter
>
> On 01.11.2011 17:44, Winfried Wurm wrote:
>> Thank you Peter for trying to solve my Problem.
>> But my Problems seems to have another reason. Actually I solved my Problem today, but I have to admit that I don´t know why the Macro is working now....
>>
>> It seems to me now that the Problem was another Function that I run before the Foci counting part that I posted yesterday. That function is measuring the Area of Chromocenters. When I turned it off or behind the Foci measuring today everything worked fine. But I don’t know why! The function measures in a completely different Image and shares no variable with the Foci measuring function.  And when I call that function AFTER the Foci Funtion there is no Problem.
>>
>> So why is that working?
>> CountMeasureFoci("cell.tif","foci.tif",px[con],mpx[con],msl[con]);
>> cellarea=Cellar("cellar.tif");
>> chromarea=Cellar("chrom.tif");
>>
>> And this not?
>> cellarea=Cellar("cellar.tif");
>> chromarea=Cellar("chrom.tif");
>> CountMeasureFoci("cell.tif","foci.tif",px[con],mpx[con],msl[con]);
>>
>> And why did the Problem only occur in BatchMode? I have no Idea what has happend. Maybe someone can tell what went wrong? Because even if it works now. I would like to understand why.
>>
>> Here is the function that seems to make the Problem:
>>
>> function Cellar(window)
>> {
>>     selectWindow(window);
>>     area=0;
>>     for(z=1;z<=nSlices;z++)
>>     {
>>         setSlice(z);
>>         run("Select None");
>>         run("Measure");
>>         if (getResult("Max")==255)
>>         {
>>             for(x=1;x<getWidth;x++)
>>             {
>>                 makeRectangle(x, 0, 1, getHeight);
>>                 run("Measure");
>>                 if (getResult("Max")==255)
>>                 {
>>                     for(y=1;y<getHeight;y++)
>>                     {
>>                         if(getPixel(x,y)==255)
>>                         {
>>                             doWand(x-1,y);
>>                             run("Measure");
>>                             m=getResult("Area");
>>                             area+=m;
>>                             run("Fill", "slice");
>>                             makeRectangle(x, 0, (getWidth-x), getHeight);
>>                             run("Measure");
>>                             if (getResult("Max")==255)
>>                             {
>>                                 makeRectangle(x, 0, 1, getHeight);
>>                                 run("Measure");
>>                                 if (getResult("Max")<255)
>>                                     y=getHeight;
>>                             }
>>                             else
>>                             {
>>                                 x=getWidth;
>>                                 y=getHeight;
>>                             }
>>                         }
>>                     }
>>
>>
>>                 }
>>             }
>>         }
>>     }
>>    return area;
>> }
>>