Create pre-defined ROI on mouse click and add to ROI manager

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

Create pre-defined ROI on mouse click and add to ROI manager

JBImageJ
Hi,

I'm trying to create a simple macro that allows me to create a pre-defined ROI (e.g. a circle with defined radius) wherever I click on a hyperstack image.  After clicking, I would like the ROI to be added to the ROI manager so that I can record the position of every ROI I create and perform other analyses.  I modified some code I found on the web, which successfully creates a circle ROI wherever I left-click, and terminates whenever I right-click.  However, when I tell it to add the circle to the ROI manager, it adds thousands of ROIs instead of just the one I created by left-clicking.  I'm guessing this is an issue with the roiManager("Add") function being called within an if statement that is based on an instantaneous command (i.e. left-click), but I can't think of another way to do it.  Could someone please help me?  I'm pasting the code below.  

macro CircleOn_MouseClick{
        getPixelSize(unit, pixelWidth, pixelHeight);
        setTool("rectangle");
        leftButton=16;
        rightButton=4;
        radius = 100;
        Dialog.create("Settings");
        Dialog.addNumber("Set radius of circle", radius);
        Dialog.show();
        radius = Dialog.getNumber();
        height = 2*pixelHeight*radius;
        width = 2*pixelWidth*radius;
        x2=-1; y2=-1; z2=-1; flags2=-1;
        getCursorLoc(x, y, z, flags);
        while (flags&rightButton==0){
                getCursorLoc(x, y, z, flags);
                if (flags&leftButton!=0) {
                                if (x!=x2 || y!=y2 || z!=z2 || flags!=flags2) {
                                x = x - width/2;
                                y = y - height/2;
                                makeOval(x, y, width, height);
                                roiManager("Add");
                                 
                        }

               }  
      }
}


Thanks!
Reply | Threaded
Open this post in threaded view
|

Re: Create pre-defined ROI on mouse click and add to ROI manager

Straub, Volko A. (Dr.)
I encountered similar problems in the past when I tried to do something
like this. The only way I could get around the problem was by inserting
a wait() command after the getCursorLoc() command (try wait(100), but
depends on your mouse clicking habit).
Hope this helps,
Volko


On 18/06/2014 17:31, JBImageJ wrote:

> macro CircleOn_MouseClick{
>          getPixelSize(unit, pixelWidth, pixelHeight);
>          setTool("rectangle");
>          leftButton=16;
>          rightButton=4;
>          radius = 100;
>          Dialog.create("Settings");
>          Dialog.addNumber("Set radius of circle", radius);
>          Dialog.show();
>          radius = Dialog.getNumber();
>          height = 2*pixelHeight*radius;
>          width = 2*pixelWidth*radius;
>          x2=-1; y2=-1; z2=-1; flags2=-1;
>          getCursorLoc(x, y, z, flags);
>          while (flags&rightButton==0){
>                  getCursorLoc(x, y, z, flags);
>                  if (flags&leftButton!=0) {
>                                  if (x!=x2 || y!=y2 || z!=z2 ||
> flags!=flags2) {
>                                  x = x - width/2;
>                                  y = y - height/2;
>                                  makeOval(x, y, width, height);
>                                  roiManager("Add");
>                                  
>                          }
>
>                 }
>        }
> }

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

Re: Create pre-defined ROI on mouse click and add to ROI manager

Olivier Burri
Hi all,

Like Voklo I had used this approach before, but like he mentions, this can still lead to problems.

I'm using this strategy now:
When you press the left button. Register that the button has been pressed but draw the ROI only when it is released.

Here is the new code. I also added the option "DisablePopupMenu" so that right clicking while in your macro does not cause the contextual menu to popup.

Best

Oli

macro CircleOn_MouseClick{
        setOption("DisablePopupMenu", true);
        getPixelSize(unit, pixelWidth, pixelHeight);
        setTool("rectangle");
        leftButton=16;
        rightButton=4;
        radius = 100;
        Dialog.create("Settings");
        Dialog.addNumber("Set radius of circle", radius);
        Dialog.show();
        radius = Dialog.getNumber();
        height = 2*pixelHeight*radius;
        width = 2*pixelWidth*radius;
        x2=-1; y2=-1; z2=-1; flags2=-1;
        getCursorLoc(x, y, z, flags);
        wasLeftPressed = false;
        while (flags&rightButton==0){
                getCursorLoc(x, y, z, flags);
               
                if (flags&leftButton!=0) {
                // Wait for it to be released
                wasLeftPressed = true;
                } else if (wasLeftPressed) {
                wasLeftPressed = false;
                if (x!=x2 || y!=y2 || z!=z2 || flags!=flags2) {
                                x = x - width/2;
                                y = y - height/2;
                                makeOval(x, y, width, height);
                                roiManager("Add");
                                 
                        }
                }
      }
      setOption("DisablePopupMenu", false);
}

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

Re: Create pre-defined ROI on mouse click and add to ROI manager

JBImageJ
Great, thank you both so much!  Both of these solutions worked.