eval("script", "roi.contains(x, y);");

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

eval("script", "roi.contains(x, y);");

Douglas Benn-2
Another query please.

I wrote the following code to test the use of roi.contains(x,y) from within a macro. I have code before it which loads an image, thresholds, masks, outlines and opens roimanager. I also checked to see that the roi list is populated and the image displays the rois and a selection.

When I run the code

 index          =getNumber("Enter number of ROI", 0);
   index        = index--;
   roiManager("select", index);
   x            =getNumber("Cursor X", 0);
   y            =getNumber("Cursor Y", 0);
   roi_flag     =eval("script", "roi.contains(x, y);");
   if (roi_flag==true) print("roi flag=Inside\n");
   else print("roi flag=Outside\n");

I get this output in my Log.

ReferenceError: "roi" is not defined. (<Unknown source>#1) in <Unknown source> at line number 1

Although it references line number 1 it is not line number 1 of my program.What am I doing wrong?

Thanks.

Douglas
Dr Douglas K Benn, BDS, M.Phil., Ph.D., Dipl. Dental Radiology (Royal College of Radiologists, England).
Professor
Dept of General Dentistry
Creighton University Dental School
2500 California Plaza
Omaha
Nebraska 68178

Tel: (402)280 5025
Fax: (402)280 5094
Reply | Threaded
Open this post in threaded view
|

Re: eval("script", "roi.contains(x, y);");

David Webster
Douglas,

Line 1 probably refers to line 1 of the Javascripts code in the
eval("script", "roi.contains(x, y);");, i.e roi.contains(x, y);.

For what it's worth, the macro function documentation says that for
roiManager("select", index);"*index* must be greater than or equal zero."
So, index-- is probably wrong. This has nothing to do with your problem, but
will show up eventually.

I haven't tried running Javascript but gave it a quick look anyway. I tried
using Roi.contains(x,y) instead of roi.contains(x,y)  but get the error
below:
"Error: Java class "ij.gui.Roi" has no public instance field or method named
"contains". (<Unknown source>#1) in <Unknown source> at line number 1.

I am interested to see what's going on, so I hope someone swho knows what
doing answers you.

David




On Mon, Apr 5, 2010 at 11:36 AM, Benn, Douglas K. <[hidden email]
> wrote:

> Another query please.
>
> I wrote the following code to test the use of roi.contains(x,y) from within
> a macro. I have code before it which loads an image, thresholds, masks,
> outlines and opens roimanager. I also checked to see that the roi list is
> populated and the image displays the rois and a selection.
>
> When I run the code
>
>  index          =getNumber("Enter number of ROI", 0);
>   index        = index--;
>   roiManager("select", index);
>   x            =getNumber("Cursor X", 0);
>   y            =getNumber("Cursor Y", 0);
>   roi_flag     =eval("script", "roi.contains(x, y);");
>   if (roi_flag==true) print("roi flag=Inside\n");
>   else print("roi flag=Outside\n");
>
> I get this output in my Log.
>
> ReferenceError: "roi" is not defined. (<Unknown source>#1) in <Unknown
> source> at line number 1
>
> Although it references line number 1 it is not line number 1 of my
> program.What am I doing wrong?
>
> Thanks.
>
> Douglas
> Dr Douglas K Benn, BDS, M.Phil., Ph.D., Dipl. Dental Radiology (Royal
> College of Radiologists, England).
> Professor
> Dept of General Dentistry
> Creighton University Dental School
> 2500 California Plaza
> Omaha
> Nebraska 68178
>
> Tel: (402)280 5025
> Fax: (402)280 5094
>
Reply | Threaded
Open this post in threaded view
|

Re: eval("script", "roi.contains(x, y);");

jmutterer
In reply to this post by Douglas Benn-2
On Mon, Apr 5, 2010 at 8:36 PM, Benn, Douglas K.
<[hidden email]>wrote:

>   roi_flag     =eval("script", "roi.contains(x, y);");
>

Benn, there are several problems in your code.
First, you can not mix macro variables and javascript. Instead, you should
feed the variable's value in the js code:
eval("script", "roi.contains("+x+","+y+");");
Regarding the error you're getting, if you write roi.contains(..), roi is
supposed to be defined first from the active ROI in the active image.
roi = IJ.getImage().getRoi();
Finally, the eval('script',script) macro function does not return anything,
just executes the script, so you can not get the roi.contains() value back
in your macro.

In addition to what Gabriel already suggested to check if a point at (x,y)
is in a given ROI, you could maybe use one of the following approach:
interactively : the getCursorLoc(x,y,z,flags); macro function. In the
returned flags variable, a bit is set if the mouse was clicked inside a ROI.
see http://rsb.info.nih.gov/ij/macros//GetCursorLocDemo.txt
Non intervactively, get the active roi's mask using run("Create Mask") , and
check the mask value at position (x,y) using getPixel(x,y).

Jerome




  if (roi_flag==true) print("roi flag=Inside\n");

>   else print("roi flag=Outside\n");
>
> I get this output in my Log.
>
> ReferenceError: "roi" is not defined. (<Unknown source>#1) in <Unknown
> source> at line number 1
>
> Although it references line number 1 it is not line number 1 of my
> program.What am I doing wrong?
>
> Thanks.
>
> Douglas
> Dr Douglas K Benn, BDS, M.Phil., Ph.D., Dipl. Dental Radiology (Royal
> College of Radiologists, England).
> Professor
> Dept of General Dentistry
> Creighton University Dental School
> 2500 California Plaza
> Omaha
> Nebraska 68178
>
> Tel: (402)280 5025
> Fax: (402)280 5094
>
Reply | Threaded
Open this post in threaded view
|

Re: eval("script", "roi.contains(x, y);");

Douglas Benn-2
Thanks to everybody who replied. I will have a go!

Dr Douglas K Benn, BDS, M.Phil., Ph.D., Dipl. Dental Radiology (Royal College of Radiologists, England).
Professor
Dept of General Dentistry
Creighton University Dental School
2500 California Plaza
Omaha
Nebraska 68178

Tel: (402)280 5025
Fax: (402)280 5094



-----Original Message-----
From: ImageJ Interest Group on behalf of Jerome Mutterer
Sent: Mon 4/5/2010 4:28 PM
To: [hidden email]
Subject: Re: eval("script", "roi.contains(x, y);");
 
On Mon, Apr 5, 2010 at 8:36 PM, Benn, Douglas K.
<[hidden email]>wrote:

>   roi_flag     =eval("script", "roi.contains(x, y);");
>

Benn, there are several problems in your code.
First, you can not mix macro variables and javascript. Instead, you should
feed the variable's value in the js code:
eval("script", "roi.contains("+x+","+y+");");
Regarding the error you're getting, if you write roi.contains(..), roi is
supposed to be defined first from the active ROI in the active image.
roi = IJ.getImage().getRoi();
Finally, the eval('script',script) macro function does not return anything,
just executes the script, so you can not get the roi.contains() value back
in your macro.

In addition to what Gabriel already suggested to check if a point at (x,y)
is in a given ROI, you could maybe use one of the following approach:
interactively : the getCursorLoc(x,y,z,flags); macro function. In the
returned flags variable, a bit is set if the mouse was clicked inside a ROI.
see http://rsb.info.nih.gov/ij/macros//GetCursorLocDemo.txt
Non intervactively, get the active roi's mask using run("Create Mask") , and
check the mask value at position (x,y) using getPixel(x,y).

Jerome




  if (roi_flag==true) print("roi flag=Inside\n");

>   else print("roi flag=Outside\n");
>
> I get this output in my Log.
>
> ReferenceError: "roi" is not defined. (<Unknown source>#1) in <Unknown
> source> at line number 1
>
> Although it references line number 1 it is not line number 1 of my
> program.What am I doing wrong?
>
> Thanks.
>
> Douglas
> Dr Douglas K Benn, BDS, M.Phil., Ph.D., Dipl. Dental Radiology (Royal
> College of Radiologists, England).
> Professor
> Dept of General Dentistry
> Creighton University Dental School
> 2500 California Plaza
> Omaha
> Nebraska 68178
>
> Tel: (402)280 5025
> Fax: (402)280 5094
>