OpenCV (Emgu.CV) — compositing images with alpha

孤街醉人 提交于 2019-11-30 09:26:53
karlphillip

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:

EDIT:

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

You'll have to iterate through each pixel. I'm assuming image 1 is the frog image, and image 2 is the city image, with image1 always being bigger than image2.

//to simulate image1.AddInPlace(image2)
 int image2w = image2.Width;
 int image2h = image2.Height;
 int i,j;
 var alpha;
 for (i = 0; i < w; i++)
 {
     for (j = 0; j < h; j++)
     {
           //alpha=255 is opaque > image2 should be used
           alpha = image2[3][j,i].Intensity;
           image1[j, i] 
               = new Bgra(
               image2[j, i].Blue * alpha + (image1[j, i].Blue * (255-alpha)),
               image2[j, i].Green * alpha + (image1[j, i].Green * (255-alpha)),
               image2[j, i].Red * alpha + (image1[j, i].Red * (255-alpha)));
      }
 }

Using Osiris's suggestion as a starting point, and having checked out alpha compositing on Wikipedia, i ended up with the following which worked really nicely for my purposes.

This was used this with Emgucv. I was hoping that the opencv gpu::AlphaComposite methods were available in Emgucv which I believe would have done the following for me, but alas the version I am using didn't appear to have them implemented.

static public Image<Bgra, Byte> Overlay( Image<Bgra, Byte> image1, Image<Bgra, Byte> image2 )
        {

            Image<Bgra, Byte> result = image1.Copy();
            Image<Bgra, Byte> src = image2;
            Image<Bgra, Byte> dst = image1;

            int rows = result.Rows;
            int cols = result.Cols;
            for (int y = 0; y < rows; ++y)
            {
                for (int x = 0; x < cols; ++x)
                {
                    // http://en.wikipedia.org/wiki/Alpha_compositing
                    double  srcA = 1.0/255 * src.Data[y, x, 3];
                    double dstA = 1.0/255 * dst.Data[y, x, 3];
                    double outA = (srcA + (dstA - dstA * srcA));
                    result.Data[y, x, 0] = (Byte)(((src.Data[y, x, 0] * srcA) + (dst.Data[y, x, 0] * (1 - srcA))) / outA);  // Blue
                    result.Data[y, x, 1] = (Byte)(((src.Data[y, x, 1] * srcA) + (dst.Data[y, x, 1] * (1 - srcA))) / outA);  // Green
                    result.Data[y, x, 2] = (Byte)(((src.Data[y, x, 2] * srcA) + (dst.Data[y, x, 2] * (1 - srcA))) / outA); // Red
                    result.Data[y, x, 3] = (Byte)(outA*255);
                }
            }
            return result;
        }

A newer version, using emgucv methods. rather than a loop. Not sure it improves on performance. double unit = 1.0 / 255.0; Image[] dstS = dst.Split(); Image[] srcS = src.Split(); Image[] rs = result.Split();

        Image<Gray, double> srcA = srcS[3] * unit;
        Image<Gray, double> dstA = dstS[3] * unit;
        Image<Gray, double> outA = srcA.Add(dstA.Sub(dstA.Mul(srcA)));// (srcA + (dstA - dstA * srcA));

        // Red.
        rs[0] = srcS[0].Mul(srcA).Add(dstS[0].Mul(1 - srcA)).Mul(outA.Pow(-1.0)); // Mul.Pow is divide.
        rs[1] = srcS[1].Mul(srcA).Add(dstS[1].Mul(1 - srcA)).Mul(outA.Pow(-1.0));
        rs[2] = srcS[2].Mul(srcA).Add(dstS[2].Mul(1 - srcA)).Mul(outA.Pow(-1.0));
        rs[3] = outA.Mul(255);

        // Merge image back together.
        CvInvoke.cvMerge(rs[0], rs[1], rs[2], rs[3], result);
        return result.Convert<Bgra, Byte>();
masad

I found an interesting blog post on internet, which I think is related to what you are trying to do.

Please have a look at the Creating Overlays Method (archive.org link). You can use this idea to implement your own function to add two images in the way you mentioned above, making some particular areas in the image transparent while leaving the rest as it is.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!