OpenCV (Emgu.CV) — compositing images with alpha

后端 未结 4 1993
刺人心
刺人心 2020-12-31 20:21

I\'m using Emgu.CV to perform some basic image manipulation and composition. My images are loaded as Image.

Question #1:

相关标签:
4条回答
  • 2020-12-31 21:11

    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>();
    
    0 讨论(0)
  • 2020-12-31 21:13

    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.

    0 讨论(0)
  • 2020-12-31 21:18

    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.

    0 讨论(0)
  • 2020-12-31 21:27

    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)));
          }
     }
    
    0 讨论(0)
提交回复
热议问题