2014-07-16 128 views
0

所以我搜索了這個問題,並得到了很多點擊,因此我能夠拿出下面顯示的代碼。我從ImageStack(ImageJ)拍攝圖像,我想重疊兩張以dicom格式(.dcm)顯示的圖像。我的問題是我希望這兩個圖像在彼此重疊時都是透明的。我已經檢查過,當傳遞到重疊函數時圖像是不同的,我嘗試了很多東西,但我似乎無法使圖像透明,它們重疊,但它們不透明。任何幫助將不勝感激。使兩個BufferedImages重疊和透明

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

主要功能:

 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

ImageJ的1.x中具有用於透明(ARGB)彩色圖像僅提供有限的支持(例如,參見here)。

修改ARGB圖像的alpha通道的一種方法是使用getChannelsetChannel方法ij.process.ColorProcessor。示例見this BeanShell script

通過將一張圖像疊加在另一張具有某種不透明度的圖像之上來組合兩張圖像的更簡單方法是使用ImageJ的overlays。下面BeanShell script(它移植到Java應該是直接的)是一個最小的例子來展示兩幅圖像的半透明組合:

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

您可以將代碼粘貼到Fiji's script editor嘗試劇本,選擇語言>的BeanShell,並按運行

這將是結果:

Result of running the beanshell script

要導出/保存圖像,您應該運行圖像>覆蓋>平展命令,或者,在Java/BeanShell的:

flattenedImp = imp.flatten(); 
+0

我嘗試了你所做的,但沒有讓圖像變得透明。同樣的問題依然存在。 –

+0

我添加了一個圖像,說明我的腳本的預期結果。這不是你在上面運行beanshell代碼時得到的嗎? –

+0

好吧,我通過使用imp2.setRoi(roi1)得到了它。我沒有使用beanshell代碼,所以這就是爲什麼我沒有得到它。我嘗試覆蓋,但它似乎並沒有在我的工作。你也可以做這個兩個或更多的圖像? –