bug in rm.add(roi) , or in Jython running rm.add(roi)?

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

bug in rm.add(roi) , or in Jython running rm.add(roi)?

Aryeh Weiss
I have a code snippet that creates a convex hull selection and tries to
add it to the roimanager.

     IJ.run(segmentedImp, "Convex Hull", "")
     convexHullRoi = segmentedImp.getRoi()
     print convexHullRoi
     rm.add(segmentedImp.getRoi())

the print statement verifies that convexHullRoi  is in fact an Roi:

Roi[Polygon, x=772, y=955, width=1617, height=3299]

yet, the rm.add(segmentedImp.getRoi()) statement throws an error

     rm.add(segmentedImp.getRoi())
TypeError: add(): 1st arg can't be coerced to java.awt.Component,
java.awt.PopupMenu

When I record the same sequence of operation as a javascript, I see the
same command

imp = IJ.getImage();
rm = RoiManager.getInstance();
if (rm==null) rm = new RoiManager();
rm.addRoi(imp.getRoi());

The above js script works properly..

Why do I get that typeError?

--aryeh

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

Ph:  972-3-5317638
FAX: 972-3-7384051

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

Re: bug in rm.add(roi) , or in Jython running rm.add(roi)?

Marcel.
Dear Aryeh,

you added the wrong command for Jython, 'addRoi' versus 'add'. So Jython
works fine but you have to use the method 'addRoi' in Jython. I must admit
that I was confused in the beginning, too not seeing the differences in both
code snippets. But then I made this reproducible example:

from ij import IJ;
from ij.plugin.frame import RoiManager;
from ij.gui import PolygonRoi;
from ij.gui import Roi;

segmentedImp = IJ.openImage("http://wsr.imagej.net/images/blobs.gif");
xpoints = [85,184,157,207,119,177,43,89,11,77,19,81];
ypoints = [96,38,112,134,156,203,225,158,157,131,50,93];
segmentedImp.setRoi(PolygonRoi(xpoints,ypoints,Roi.POLYGON));
IJ.run(segmentedImp, "Convex Hull", "")
convexHullRoi = segmentedImp.getRoi()
print convexHullRoi
rm = RoiManager()
rm.addRoi(convexHullRoi)


See the differences in the ImageJ API. This explains your error:

https://imagej.nih.gov/ij/developer/api/ij/plugin/frame/RoiManager.html



--
Sent from: http://imagej.1557.x6.nabble.com/

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

Re: bug in rm.add(roi) , or in Jython running rm.add(roi)?

Aryeh Weiss
Thank you, Marcel.

Amazing how this was staring me in the face, and I missed it.

Best regards
--aryeh

On 31/07/2018 4:32, Marcel. wrote:

> Dear Aryeh,
>
> you added the wrong command for Jython, 'addRoi' versus 'add'. So Jython
> works fine but you have to use the method 'addRoi' in Jython. I must admit
> that I was confused in the beginning, too not seeing the differences in both
> code snippets. But then I made this reproducible example:
>
> from ij import IJ;
> from ij.plugin.frame import RoiManager;
> from ij.gui import PolygonRoi;
> from ij.gui import Roi;
>
> segmentedImp = IJ.openImage("http://wsr.imagej.net/images/blobs.gif");
> xpoints = [85,184,157,207,119,177,43,89,11,77,19,81];
> ypoints = [96,38,112,134,156,203,225,158,157,131,50,93];
> segmentedImp.setRoi(PolygonRoi(xpoints,ypoints,Roi.POLYGON));
> IJ.run(segmentedImp, "Convex Hull", "")
> convexHullRoi = segmentedImp.getRoi()
> print convexHullRoi
> rm = RoiManager()
> rm.addRoi(convexHullRoi)
>
>
> See the differences in the ImageJ API. This explains your error:
>
> https://imagej.nih.gov/ij/developer/api/ij/plugin/frame/RoiManager.html
>
>
>
> --
> Sent from: http://imagej.1557.x6.nabble.com/
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

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

Ph:  972-3-5317638
FAX: 972-3-7384051

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

Re: bug in rm.add(roi) , or in Jython running rm.add(roi)?

Albert Cardona-2
In reply to this post by Aryeh Weiss
Possibly because jython does not distinguish well equally named methods with the same number if arguments. No method dispatch by argument type. In this case, it is picking up the one you don't want.

There are workarounds but they aren't pretty.

Albert

> On Jul 31, 2018, at 2:25 AM, Aryeh Weiss <[hidden email]> wrote:
>
> I have a code snippet that creates a convex hull selection and tries to add it to the roimanager.
>
>    IJ.run(segmentedImp, "Convex Hull", "")
>    convexHullRoi = segmentedImp.getRoi()
>    print convexHullRoi
>    rm.add(segmentedImp.getRoi())
>
> the print statement verifies that convexHullRoi  is in fact an Roi:
>
> Roi[Polygon, x=772, y=955, width=1617, height=3299]
>
> yet, the rm.add(segmentedImp.getRoi()) statement throws an error
>
>    rm.add(segmentedImp.getRoi())
> TypeError: add(): 1st arg can't be coerced to java.awt.Component, java.awt.PopupMenu
>
> When I record the same sequence of operation as a javascript, I see the same command
>
> imp = IJ.getImage();
> rm = RoiManager.getInstance();
> if (rm==null) rm = new RoiManager();
> rm.addRoi(imp.getRoi());
>
> The above js script works properly..
>
> Why do I get that typeError?
>
> --aryeh
>
> --
> Aryeh Weiss
> Faculty of Engineering
> Bar Ilan University
> Ramat Gan 52900 Israel
>
> Ph:  972-3-5317638
> FAX: 972-3-7384051
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

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

Re: bug in rm.add(roi) , or in Jython running rm.add(roi)?

Aryeh Weiss
Hi Albert and thank you for your reply.

Turned out that the method I needed was addRoi() , not add().

My bad -- I stared at the API docs and saw the wrong thing. And the js I
recorded did it right but again I missed it.

Best regards
--aryeh


On 31/07/2018 17:57, Albert Cardona wrote:

> Possibly because jython does not distinguish well equally named methods with the same number if arguments. No method dispatch by argument type. In this case, it is picking up the one you don't want.
>
> There are workarounds but they aren't pretty.
>
> Albert
>
>> On Jul 31, 2018, at 2:25 AM, Aryeh Weiss <[hidden email]> wrote:
>>
>> I have a code snippet that creates a convex hull selection and tries to add it to the roimanager.
>>
>>     IJ.run(segmentedImp, "Convex Hull", "")
>>     convexHullRoi = segmentedImp.getRoi()
>>     print convexHullRoi
>>     rm.add(segmentedImp.getRoi())
>>
>> the print statement verifies that convexHullRoi  is in fact an Roi:
>>
>> Roi[Polygon, x=772, y=955, width=1617, height=3299]
>>
>> yet, the rm.add(segmentedImp.getRoi()) statement throws an error
>>
>>     rm.add(segmentedImp.getRoi())
>> TypeError: add(): 1st arg can't be coerced to java.awt.Component, java.awt.PopupMenu
>>
>> When I record the same sequence of operation as a javascript, I see the same command
>>
>> imp = IJ.getImage();
>> rm = RoiManager.getInstance();
>> if (rm==null) rm = new RoiManager();
>> rm.addRoi(imp.getRoi());
>>
>> The above js script works properly..
>>
>> Why do I get that typeError?
>>
>> --aryeh
>>
>> --
>> Aryeh Weiss
>> Faculty of Engineering
>> Bar Ilan University
>> Ramat Gan 52900 Israel
>>
>> Ph:  972-3-5317638
>> FAX: 972-3-7384051
>>
>> --
>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html


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

Ph:  972-3-5317638
FAX: 972-3-7384051

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html