Login  Register

Re: Can't automate copying of polygon ROIs

Posted by Wayne Rasband on Jun 12, 2008; 2:28am
URL: http://imagej.273.s1.nabble.com/Can-t-automate-copying-of-polygon-ROIs-tp3695935p3695936.html

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
> >