Fill an ROI with transparent color (alpha value)

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

Fill an ROI with transparent color (alpha value)

Jan Eglinger-3
Dear all,

I'd like to fill an ROI (e.g. rectangle or arrow) with a
half-transparent color (i.e. alpha = 0.5).

How can I do this with Javascript?

I tried with
  ip.setColor(new Color(r/255,g/255,b/255,alpha));
  roi.drawPixels(ip);
which works for TextRois, but when I try similar for other ROIs:
  ip.setColor(new Color(r/255,g/255,b/255,alpha));
  ip.fill(roi);
seems to ignore the alpha value.

Thanks for any hints,
Jan
Reply | Threaded
Open this post in threaded view
|

Re: Fill an ROI with transparent color (alpha value)

dscho
Hi Jan,

On Sat, 21 May 2011, Jan Eglinger wrote:

> I'd like to fill an ROI (e.g. rectangle or arrow) with a
> half-transparent color (i.e. alpha = 0.5).
>
> How can I do this with Javascript?
>
> I tried with
>   ip.setColor(new Color(r/255,g/255,b/255,alpha));
>   roi.drawPixels(ip);
> which works for TextRois, but when I try similar for other ROIs:
>   ip.setColor(new Color(r/255,g/255,b/255,alpha));
>   ip.fill(roi);
> seems to ignore the alpha value.

I fear that you have to get an AWT image via ImageProcessor's getImage()
method, get the corresponding Graphics object via getGraphics(),
then convert the ROI into an AWT Shape via new ShapeRoi(roi).getShape()
and then use the Graphics' methods to fill or draw the Shape.

Ciao,
Dscho
Reply | Threaded
Open this post in threaded view
|

Re: Fill an ROI with transparent color (alpha value)

Rasband, Wayne (NIH/NIMH) [E]
In reply to this post by Jan Eglinger-3
On May 21, 2011, at 11:25 AM, Jan Eglinger wrote:

> Dear all,
>
> I'd like to fill an ROI (e.g. rectangle or arrow) with a
> half-transparent color (i.e. alpha = 0.5).
>
> How can I do this with Javascript?
>
> I tried with
>  ip.setColor(new Color(r/255,g/255,b/255,alpha));
>  roi.drawPixels(ip);
> which works for TextRois, but when I try similar for other ROIs:
>  ip.setColor(new Color(r/255,g/255,b/255,alpha));
>  ip.fill(roi);
> seems to ignore the alpha value.

You can do this by using the ImagePlus flatten() method. Here is an example:

  imp = IJ.openImage("http://imagej.nih.gov/ij/images/FluorescentCells.zip");
  roi = new OvalRoi(130, 118, 254, 239);
  r=150; g=0; b=150; alpha=0.5;
  roi.setFillColor(new Color(r/255,g/255,b/255,alpha));
  imp.setOverlay(new Overlay(roi));
  imp = imp.flatten();
  imp.show();

-wayne
Reply | Threaded
Open this post in threaded view
|

Re: Fill an ROI with transparent color (alpha value)

Jan Eglinger-3
Dear Wayne,

thanks a lot for the helpful example!

On 21.05.2011 7:17 PM, Rasband, Wayne (NIH/NIMH) [E] wrote:
> You can do this by using the ImagePlus flatten() method. Here is an example:
>
>   imp = IJ.openImage("http://imagej.nih.gov/ij/images/FluorescentCells.zip");
>   roi = new OvalRoi(130, 118, 254, 239);
>   r=150; g=0; b=150; alpha=0.5;
>   roi.setFillColor(new Color(r/255,g/255,b/255,alpha));
>   imp.setOverlay(new Overlay(roi));
>   imp = imp.flatten();
>   imp.show();

As far as I understand, this wouldn't work on a single slice of a stack,
since imp.flatten() will flatten the whole stack, right?

Best regards,
Jan
Reply | Threaded
Open this post in threaded view
|

Re: Fill an ROI with transparent color (alpha value)

dscho
Hi Jan,

On Sun, 22 May 2011, Jan Eglinger wrote:

> thanks a lot for the helpful example!
>
> On 21.05.2011 7:17 PM, Rasband, Wayne (NIH/NIMH) [E] wrote:
> > You can do this by using the ImagePlus flatten() method. Here is an example:
> >
> >   imp = IJ.openImage("http://imagej.nih.gov/ij/images/FluorescentCells.zip");
> >   roi = new OvalRoi(130, 118, 254, 239);
> >   r=150; g=0; b=150; alpha=0.5;
> >   roi.setFillColor(new Color(r/255,g/255,b/255,alpha));
> >   imp.setOverlay(new Overlay(roi));
> >   imp = imp.flatten();
> >   imp.show();
>
> As far as I understand, this wouldn't work on a single slice of a stack,
> since imp.flatten() will flatten the whole stack, right?

The common workaround is to create a dummy ImagePlus with the single
slice:

        ImageProcessor ip = imp.getStack().getProcessor(15);
        ImagePlus dummy = new ImagePlus("dummy", ip);

        /* ... work on dummy, the pixel data are shared ... */

        // now the original image's current slice might be changed
        imp.updateAndDraw();

Ciao,
Dscho