使用emgu.cv的Alpha合成图像 [英] Alpha composite images using emgu.cv

查看:85
本文介绍了使用emgu.cv的Alpha合成图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Emgu.CV(Nuget软件包2.4.2)没有告诉我实现OpenCV中可用的gpu :: alphaComp方法。

Emgu.CV (Nuget package 2.4.2) doesn't as far as I can tell implement the gpu::alphaComp method available in OpenCV.

在尝试实现这种特定类型的组合时,C#的运行速度令人难以置信,以至于它占用了我应用程序总CPU使用率的80%。

As such when trying to implement this specific type of composite it is incredible slow in C#, such that it takes up some 80% of the total cpu usage of my app.

我原来的解决方案效果很差。

This was my original solution that performs really badly.

    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#中优化以上内容?

Is there a way to optimise the above in C#?

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

I've additionally looked at using OpencvSharp, but that doesn't appear to provide access to gpu::alphaComp neither.

有没有可以进行alpha合成的OpenCV C#包装器库?

Is there any OpenCV C# wrapper library that can do alpha Compositing?

AddWeighted不能满足我的需要。

AddWeighted does not do what I need it to do.

尽管与此相似,但问题没有提供答案

Whilst similar, this question doesn't provide an answer

推荐答案

如此简单。

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

这篇关于使用emgu.cv的Alpha合成图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆