1

I have a PNG image with transparent background that is to be added to another image.

My problem is that when I am loading the IplImage the background is not transparent anymore - it turns white.

How do I use image with transparent background in 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);

tryed to add alpha channl but didnt work:

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
    If the BG image has no alpha-channel, the Graphics object for it will not support transparency in other images painted to it. Try writing the BG image first to an image the same size but with alpha, then write the FG image to that image. Sep 10, 2012 at 3:30
  • how do i do that:)writing the BG image first to an image the same size but with alpha? Sep 10, 2012 at 4:30
  • i tryed java2d (never used it befor so not 100% sure am doing the right thing) add code to post for what i tryed but it didnt work when i only did it to one it gave me excption sens they didnt have the same number of channls so i did it to the other one to but i just got 2 black images ;/ Sep 10, 2012 at 11:55
  • 1
    "If the BG image has no alpha-channel, the Graphics object for it will not support transparency in other images painted to it." A simple test here using transparent over solid suggests that is WRONG. So it comes back to - I don't know what the problem is here, sorry. See if you get the same effect using those 2 images. Sep 10, 2012 at 18:55

1 Answer 1

2

thx andrew you were right about alpha thinge :)did take me bit more serching to find same thing that works but here it is :)

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

}
2
  • i cant u posted as a comment and i cant accpect my own answers :P Sep 11, 2012 at 0:44
  • I've accepted my own answer before. SO makes you wait longer before you can do that. ;) Sep 11, 2012 at 0:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.