Substracting a color from an Image / setting one color to transparent

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

Substracting a color from an Image / setting one color to transparent

Johannes Hermen
Hi there,

i have an imagePlus (16bit bw) which i want to draw onto another image as
an overlay. Therefore i want to set the black background of the image to
transparent to not cover the background in this region.

Any hints how to do this, i a a bit stuck here.

Greets
        Johannes


-----------------------------------------------------------------
Johannes Hermen  -  Ingenieur de Recherche
[hidden email]
-----------------------------------------------------------------
CRP Henri Tudor  http://www.santec.tudor.lu
29, Avenue John F. Kennedy
L-1855 Luxembourg
-----------------------------------------------------------------
Reply | Threaded
Open this post in threaded view
|

Re: Substracting a color from an Image / setting one color to transparent

dscho
Hi Johannes,

On Thu, 29 Mar 2012, Johannes Hermen wrote:

> i have an imagePlus (16bit bw) which i want to draw onto another image
> as an overlay. Therefore i want to set the black background of the image
> to transparent to not cover the background in this region.

In the Image Expression Parser, IIRC you could do

        (B == 0) * A + B

You can also achieve the affect by using a threshold on the partially
black image, turning that into a selection, copying that selection,
switching to the other image and pasting.

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

Re: Substracting a color from an Image / setting one color to transparent

Rasband, Wayne (NIH/NIMH) [E]
In reply to this post by Johannes Hermen
On Mar 29, 2012, at 3:58 AM, Johannes Hermen wrote:

> Hi there,
>
> i have an imagePlus (16bit bw) which i want to draw onto another image as
> an overlay. Therefore i want to set the black background of the image to
> transparent to not cover the background in this region.
>
> Any hints how to do this, i a a bit stuck here.

This example plugin draws one image (the "CT" sample image) onto another ("Boats") as an overlay,
with the black background transparent.

Replace

    boats.setOverlay(overlay);

with

    boats.setRoi(imageOverlay);

and you will be able to move the CT image by dragging it.

-wayne

[cid:[hidden email]]


import ij.*;
import ij.process.*;
import ij.gui.*;
import java.awt.*;
import ij.plugin.*;
import java.awt.image.*;

public class Image_Overlay_Demo implements PlugIn {

    public void run(String arg) {
        ImagePlus ct = IJ.openImage("http://imagej.nih.gov/ij/images/ct.dcm.zip");
        ImageProcessor ip = ct.getProcessor();
        int width = ip.getWidth()/2;
        int height = ip.getHeight()/2;
        ip = ip.convertToByte(true);
        ip = ip.resize(width, height, true);
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics g = bi.getGraphics();
        for (int x=0; x<width; x++) {
           for (int y=0; y<height; y++) {
                int v = ip.get(x, y);
                if (v>1) {
                    g.setColor(new Color(v,v,v));
                    g.drawRect(x, y, 1, 1);
                }
            }
        }
        Roi imageOverlay = new ImageRoi(100, 25, bi);
        ImagePlus boats = IJ.openImage("http://imagej.nih.gov/ij/images/boats.gif");
        Overlay overlay = new Overlay(imageOverlay);
        boats.setOverlay(overlay);
        boats.show();

    }

}



ct-overlayed-on-bloats.jpg (62K) Download Attachment