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