【问题标题】:Alpha composite images using emgu.cv使用 emgu.cv 的 Alpha 合成图像
【发布时间】:2014-07-10 16:36:14
【问题描述】:

Emgu.CV(Nuget 包 2.4.2)据我所知并没有实现 OpenCV 中可用的 gpu::alphaComp 方法。

因此,当尝试实现这种特定类型的复合时,它在 C# 中的速度非常慢,以至于它占用了我的应用程序总 CPU 使用量的大约 80%。

这是我最初的解决方案,效果非常糟糕。

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

有没有办法在 C# 中优化上述内容?

我还研究过使用 OpencvSharp,但这似乎也不能提供对 gpu::alphaComp 的访问。

是否有任何可以进行 alpha 合成的 OpenCV C# 包装库?

AddWeighted 没有做我需要做的事情。

虽然类似,但question 没有提供答案

【问题讨论】:

    标签: c# opencv emgucv


    【解决方案1】:

    如此简单。

        public static Image<Bgra, Byte> Overlay(Image<Bgra, Byte> target, Image<Bgra, Byte> overlay)
        {
            Bitmap bmp = target.Bitmap;
            Graphics gra = Graphics.FromImage(bmp);
            gra.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
            gra.DrawImage(overlay.Bitmap, new Point(0, 0));
    
            return target;
        }
    

    【讨论】:

    • C# 在我的设置中找不到 Bgra。我在 Emgu 文档中找到了它,但代码似乎没有定义它。很遗憾,因为我现在真的可以使用此代码。我需要在“空白”框架上合成一张图像,看起来这段代码可以做到。
    • 我结束了上述代码的核心,它工作得很好!!!谢谢!!!!!!!!!
    猜你喜欢
    • 2012-08-11
    • 1970-01-01
    • 2013-01-06
    • 2023-03-22
    • 1970-01-01
    • 2015-04-11
    • 2012-10-12
    • 1970-01-01
    • 2019-06-12
    相关资源
    最近更新 更多