0
votes

So I have searched this question and got many hits and therefore I was able to come up with the code shown below. I am taking images from ImageStack (ImageJ) and I want to overlap two images which are in dicom format (.dcm). My problem is I want both images to be transparent as they overlap each other. I have checked that the images are different when passing to the overlap function and I have tried many things but I can't seem to make the images transparent, they overlap but they are not transparent. Any help would be greatly appreciated.

  public BufferedImage overlay(BufferedImage bii, BufferedImage biii){
            BufferedImage combined = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB);
            Graphics2D go = combined.createGraphics();
            image.setSlice(5);
            ImagePlus hello = new ImagePlus();
            hello.setImage(image.getImage());
            BufferedImage bello = hello.getBufferedImage();
            go.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            go.drawImage(bii,0, 0, null);
            go.drawImage(biii,98, 98, null);
            go.setComposite(AlphaComposite.Clear); 
            go.setComposite(AlphaComposite.Src);
            //go.fillRect(0, 0, 256, 256);
            go.dispose();
            return combined;
        }

Main function:

        ImageStack stack = image.getStack(); 
        Calibration cal = image.getCalibration(); 
        ImagePlus newImp = new ImagePlus( );
        stack.getSliceLabel(5);
        stack.getProcessor(5); 
        newImp.setCalibration( cal ); 
        ImageProcessor ip = stack.getProcessor(1); // specify number of slice 
        newImp.setProcessor(ip); 


        ImagePlus no3 = new ImagePlus();
        no3.setImage(newImp.getImage());
        BufferedImage bii= no3.getBufferedImage();

        ImagePlus bob = new ImagePlus( );
        stack.getSliceLabel(33);
        stack.getProcessor(33); 
        bob.setCalibration( cal ); 
        ImageProcessor bobp = stack.getProcessor(22); // specify number of slice 
        bob.setProcessor(bobp); 
        ImagePlus hello = new ImagePlus();
        hello.setImage(bob.getImage());
        BufferedImage bello = hello.getBufferedImage();

        BufferedImage overlayy = overlay(bii, bello);
        frame2 = new NFrame(image.getTitle(), image, save);
        JPanel pane = new JPanel(new BorderLayout());
        JLabel jLabel = new JLabel(new ImageIcon(overlayy));
        pane.add(jLabel);
        frame2.add(pane);
        frame2.setVisible(true);
        desktop.add(frame2);
1

1 Answers

1
votes

ImageJ 1.x has only limited support for transparent (ARGB) color images (e.g. see here).

One way to modify the alpha channel of an ARGB image is using the getChannel and setChannel methods of ij.process.ColorProcessor. See this BeanShell script for an example.

An easier way to combine two images by overlaying one image on top of another image with a certain opacity is using ImageJ's overlays. The following BeanShell script (porting it to Java should be straightforward) is a minimal example demonstrating the semi-transparent combination of two images:

import ij.IJ;
import ij.gui.ImageRoi;
import ij.gui.Overlay;

imp = IJ.openImage("http://imagej.nih.gov/ij/images/leaf.jpg");
imp2 = IJ.openImage("http://imagej.nih.gov/ij/images/clown.jpg");

ImageRoi roi = new ImageRoi(50, 200, imp2.getProcessor());
roi.setZeroTransparent(false);
roi.setOpacity(0.5);
ovl = new Overlay(roi);

imp.setOverlay(ovl);
imp.show();

You can try the script by pasting the code into Fiji's script editor, choosing Language > Beanshell, and pressing Run.

This will be the result:

Result of running the beanshell script

To export/save the image, you should run the Image > Overlay > Flatten command, or, in Java/Beanshell:

flattenedImp = imp.flatten();