【问题标题】:How to change black color to transparent from a bitmap Mat?如何从位图垫将黑色更改为透明?
【发布时间】:2019-08-24 11:52:22
【问题描述】:

mat 或 umat 中间可以有空值吗?我需要它表现得像 .png 格式。如果没有,我怎样才能达到这个目的? Mat 和 Umat 对我来说就像一个矩形图像。

我尝试过矩形复制方法,抓取方法,但没有一种方法可以正常工作。

我开始的代码如下:

Bitmap bmpUImage = new Bitmap(plotBox.Image);
Image<Bgr, Byte> uimage = new Image<Bgr, byte>(bmpUImage);
UMat input = uimage.ToUMat();

mat 或 umat 的行为类似于 .png,在图像的中间部分有一些 cutout/null 值。

【问题讨论】:

  • 那么,您想要某些图像部分的透明度!?您所需要的只是为您的图像添加一个 Alpha 通道并相应地修改所需的部分。请查看this SO question, and the answers
  • 感谢 HansHirse 为我提供关于 alpha 通道的想法!由于我使用的是 emgu,因此我在这里找到了一个更好的链接,可以帮助其他人:stackoverflow.com/questions/45660427/…

标签: c# opencv emgucv


【解决方案1】:
///////////////// add black as transparency /////////////////
        Bitmap bmpUImage = new Bitmap(plotBox_Method.Image);

        Mat output = new Mat();
        Mat Shading_Image = new Image<Bgr, byte>(bmpUImage).Mat;

        // puting a black rectangle for transparent area later
        Shading_Image.CopyTo(output);
        CvInvoke.Rectangle(output, mask_rect, new MCvScalar(0,0,0), -1);

        Mat imageMat = output;
        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, 0, 255, Emgu.CV.CvEnum.ThresholdType.Binary);

        CvInvoke.Imshow("alpha", alpha);

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

        imgPlot.Image = finalMat.ToImage<Bgra, Byte>().ToBitmap(); 
        ///////////////// end of add black as transparency /////////////////

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-09
    • 2011-11-15
    • 2012-03-16
    • 1970-01-01
    相关资源
    最近更新 更多