1

我有一个要添加到另一个图像的透明背景的 PNG 图像。

我的问题是,当我加载IplImage背景时,背景不再透明 - 它变成白色。

如何在 javacv 中使用具有透明背景的图像?

IplImage src = cvLoadImage("2.png");
IplImage tmp = cvLoadImage("1.png");
cvSetImageROI(src, cvRect(41,28,tmp.width(),tmp.height())); // not the same size 
cvShowImage("1", src);  //before
cvCopy(src, tmp);
cvShowImage("2", src); //after
cvWaitKey(0);
cvResetImageROI(src);

尝试添加 alpha channl 但没有用:

Graphics g=src.getBufferedImage().getGraphics();
        Graphics2D g2d = (Graphics2D)g;
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
                                                    10 * 0.1f));
        BufferedImage a = new BufferedImage(tmp.width(), tmp.height(), BufferedImage.TYPE_INT_ARGB);

        src = IplImage.createFrom(a);
4

1 回答 1

2

thx andrew,你对 alpha thinge 的看法是对的 :) 确实花了我更多的时间去寻找同样有效的东西,但它是 :)

public static void combine()
{
    try{
              File path = new File("D:/proj2/javacv2");

    // load source images

      BufferedImage image = ImageIO.read(new File(path, "3.jpg"));

      BufferedImage overlay = ImageIO.read(new File(path, "test4a.png"));

     // BufferedImage image=src.getBufferedImage();
   // BufferedImage overlay =tmp.getBufferedImage();
    // create the new image, canvas size is the max. of both image sizes
    int w = Math.max(image.getWidth(), overlay.getWidth());
    int h = Math.max(image.getHeight(), overlay.getHeight());
      //int w=tmp.width();
     // int h=tmp.height();
    BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

    // paint both images, preserving the alpha channels
    Graphics g = combined.getGraphics();
    g.drawImage(image, 0, 0, null);
    g.drawImage(overlay, 41, 30, null);

    // Save as new image

       ImageIO.write(combined, "PNG", new File(path, "combined.png"));
    }catch(Exception e)
    {
        System.out.println("exception ");
    }

}
于 2012-09-10T21:58:04.803 回答