Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV (Emgu.CV) -- compositing images with alpha

I'm using Emgu.CV to perform some basic image manipulation and composition. My images are loaded as Image<Bgra,Byte>.

Question #1: When I use the Image<,>.Add() method, the images are always blended together, regardless of the alpha value. Instead I'd like them to be composited one atop the other, and use the included alpha channel to determine how the images should be blended. So if I call image1.Add(image2) any fully opaque pixels in image2 would completely cover the pixels from image1, while semi-transparent pixels would be blended based on the alpha value.

Here's what I'm trying to do in visual form. There's a city image with some "transparent holes" cut out, and a frog behind. This is what it should look like:

enter image description here

And this is what openCV produces.

This is what OpenCV (Emgu.CV) produces when I call "add"

How can I get this effect with OpenCV? And will it be as fast as calling Add()?

Question #2: is there a way to perform this composition in-place instead of creating a new image with each call to Add()? (e.g. image1.AddImageInPlace(image2) modifies the bytes of image1?)

NOTE: Looking for answers within Emgu.CV, which I'm using because of how well it handles perspective warping.

like image 492
roufamatic Avatar asked Aug 14 '12 18:08

roufamatic


1 Answers

Before OpenCV 2.4 there was no support of PNGs with alpha channel.

To verify if your current version supports it, print the number of channels after loading an image that you are certain to be RGBA. If it supports, the application will output the number 4, else it will output number 3 (RGB). Using the C API you would do:

IplImage* t_img = cvLoadImage(argv[1], CV_LOAD_IMAGE_UNCHANGED);
if (!t_img)
{
    printf("!!! Unable to load transparent image.\n");
    return -1;
}
printf("Channels: %d\n", t_img->nChannels);

If you can't update OpenCV:

  • There are some posts around that try to bypass this limitation but I haven't tested them myself;
  • The easiest solution would be to use another API to load the image and blend it, check blImageBlending;
  • Another alternative, not as lightweight, is to use Qt.

If your version already supports PNGs with RGBA:

  • Take a look at Emulating photoshop’s blending modes in OpenCV. It implements several Photoshop blending modes and I imagine you are capable of converting that code to .Net.

EDIT:

I had to deal with this problem recently and I've demonstrated how to deal with it on this answer.

like image 88
karlphillip Avatar answered Sep 26 '22 06:09

karlphillip