Emgu C#OpenCV:使颜色黑色透明 [英] Emgu C# OpenCV: Make Color Black transparent

查看:722
本文介绍了Emgu C#OpenCV:使颜色黑色透明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用OpenCV实现一个功能,该功能可使图像的黑色变透明.我使用了

I'm currently trying to implement a function with OpenCV which makes the color black of an image transparent. I used this thread as a guideline. Currently it is not working and I'm not sure if it is because of my transfer to C# or another mistake.

public Image<Bgr, Byte> BlackTransparent(Image<Bgr, Byte> image)
        {
            Mat imageMat = image.Mat;
            Mat finalMat = new Mat(imageMat.Rows, imageMat.Cols, Emgu.CV.CvEnum.DepthType.Cv8U, 4);
            Mat tmp = new Mat(imageMat.Rows, imageMat.Cols, Emgu.CV.CvEnum.DepthType.Cv8U, 1);
            Mat alpha = new Mat(imageMat.Rows, imageMat.Cols, Emgu.CV.CvEnum.DepthType.Cv8U, 1);

            CvInvoke.CvtColor(imageMat, tmp, Emgu.CV.CvEnum.ColorConversion.Bgr2Gray);
            CvInvoke.Threshold(tmp, alpha, 100, 255, Emgu.CV.CvEnum.ThresholdType.Binary);


            VectorOfMat rgb = new VectorOfMat(3);

            CvInvoke.Split(imageMat, rgb);

            Mat[] rgba = { rgb[0], rgb[1], rgb[2], alpha };

            VectorOfMat vector = new VectorOfMat(rgba);

            CvInvoke.Merge(vector, finalMat);

            return finalMat.ToImage<Bgr, Byte>();
        }

如果有人有想法或建议,我将不胜感激.

If anybody has an idea or suggestion I would appreciate it.

布鲁诺

推荐答案

public Image<Bgra, Byte> BlackTransparent(Image<Bgr, Byte> image)
        {
            Mat imageMat = image.Mat;
            Mat finalMat = new Mat(imageMat.Rows, imageMat.Cols, Emgu.CV.CvEnum.DepthType.Cv8U, 4);
            Mat tmp = new Mat(imageMat.Rows, imageMat.Cols, Emgu.CV.CvEnum.DepthType.Cv8U, 1);
            Mat alpha = new Mat(imageMat.Rows, imageMat.Cols, Emgu.CV.CvEnum.DepthType.Cv8U, 1);

            CvInvoke.CvtColor(imageMat, tmp, Emgu.CV.CvEnum.ColorConversion.Bgr2Gray);
            CvInvoke.Threshold(tmp, alpha, 100, 255, Emgu.CV.CvEnum.ThresholdType.Binary);


            VectorOfMat rgb = new VectorOfMat(3);

            CvInvoke.Split(imageMat, rgb);

            Mat[] rgba = { rgb[0], rgb[1], rgb[2], alpha };

            VectorOfMat vector = new VectorOfMat(rgba);

            CvInvoke.Merge(vector, finalMat);

            return finalMat.ToImage<Bgra, Byte>();
        }

函数的返回类型错误...应该是bgra色彩空间,而不是bgr色彩空间

The return type of the function was wrong ... instead of the bgr colorspace it should be the bgra colorspace

这篇关于Emgu C#OpenCV:使颜色黑色透明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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