Re: Can't automate copying of polygon ROIs

Posted by Wayne Rasband on
URL: http://imagej.273.s1.nabble.com/Can-t-automate-copying-of-polygon-ROIs-tp3695935p3695938.html

> Sorry to sound ignorant, I'm having trouble with this (I think it has  
> to do
> with the setLocation commands or something) but ImageJ has a nasty  
> habit of
> swallowing errors silently.  Is there any way to disable this? or is  
> there a
> verbose logfile somewhere that would tell me where exceptions are being
> thrown/caught?

Exceptions thrown on the event dispatch thread are displayed in the  
console window. To get a console window on Windows you need to change  
"javaw.exe" in the second line of ImageJ/ImageJ.cfg to "java.exe" and  
restart ImageJ. You can view console error messages on Mac OS X by  
running the /Applications/Utilities/Console program.

There was a bug in the JavaScript example that caused the ROI in the  
source Image to move to the upper left corner. You can fix this by  
changing  the line

     roi = img.getRoi();

to

    roi = img.getRoi().clone();

In Java, you need to caste the object returned by the clone() method to  
an Roi:

     roi = (Roi)img.getRoi().clone();

The corrected script is at

     http://rsb.info.nih.gov/ij/macros/js/ShowClipboard.js

With the 1.41f daily build, you can open this script in the macro  
editor by pasting the URL into the File>Import>URL dialog box, and run  
it by pressing ctrl-r (Macros>Run Macro).

-wayne


>
> Thanks!
> --Andrew
>
> On Wed, Jun 11, 2008 at 10:28 PM, Rasband Wayne <[hidden email]> wrote:
>
>> Here is code that duplicates what the Edit>Copy and File>New>Internal
>> Clipboard commands do except that it fills  with zero instead of the
>> background color. You can test it using the Macros>Evaluate JavaScript
>> command that was added to the macro editor in v1.41e.
>>
>>   img = IJ.getImage();
>>   ip = img.getProcessor();
>>   ip = ip.crop();
>>   roi = img.getRoi();
>>   roi.setLocation(0,0);
>>   ip.setColor(0);
>>   ip.snapshot()
>>   ip.fill();
>>   s1 = new ShapeRoi(roi);
>>   s2 = new ShapeRoi(new Roi(0,0, ip.getWidth(), ip.getHeight()));
>>   s3 = s1.xor(s2);
>>   ip.reset(s3.getMask());
>>   new ImagePlus("img", ip).show();
>>
>> -wayne
>>
>>
>>
>>  Thanks Wayne,
>>> That has the intended effect (and fixes the problem), however, I am
>>> performing this copy/paste hundreds of times on the same image.  So,  
>>> every
>>> time I call Internal Clipboard, a new image pops up on the screen, I  
>>> have
>>> to
>>> save it, and then hide the image so I can work on the original image
>>> again.
>>> It's just messy and I'm wondering why my method of copying using
>>> getClipboard() produces unintended effects.  Thanks!
>>>
>>> On Mon, Jun 9, 2008 at 5:15 PM, Wayne Rasband <[hidden email]> wrote:
>>>
>>>> I am having trouble copying ROIs that are based on polygons
>>>>> (non-rectangular).  If I create a polygon ROI by hand, and do
>>> Edit->Copy,
>>>>> then File->New->Internal Clipboard, I get a new image the same  
>>>>> size as
>>> the
>>>>> bounding box of the ROI, but anything NOT inside the ROI is white  
>>>>> (or
>>>>> whatever the background color is). This is the effect I want, but  
>>>>> if I
>>> do
>>>>> it
>>>>> in code:
>>>>>
>>>>> img.setRoi(p); //Where p is a polygon and img is an ImagePlus
>>>>> img.copy(false);
>>>>> ImagePlus newImg = new
>>>>> ImagePlus("img",ImagePlus.getClipboard().getProcessor().duplicate()
>>>>> );
>>>>>
>>>>> When I do that, I get a new image that contains EVERYTHING inside  
>>>>> the
>>>>> bounding box of the polygon. this includes a LOT of data that I do  
>>>>> not
>>>>> want
>>>>> in the image. How can I obtain the same effect as edit->copy in my
>>> code?
>>>>>
>>>>
>>>> You can do this sort of thing easily in a macro:
>>>>
>>>>   makeOval(50, 50, 100, 100);
>>>>   run("Copy");
>>>>   run("Internal Clipboard");
>>>>
>>>> In a plugin, the code would look something like this:
>>>>
>>>>   img = IJ.getImage();
>>>>   img.setRoi(p)
>>>>   IJ.run("Copy");
>>>>   IJ.run("Internal Clipboard");
>>>>
>>>> -wayne
>>>>
>>>
>>
>