【问题标题】:How to save an image with transparent background in EmguCV如何在 EmguCV 中保存具有透明背景的图像
【发布时间】:2019-10-17 18:19:35
【问题描述】:

我正在尝试通过创建蒙版并使用创建的蒙版减去背景来从图像中移除背景。当我将图像保存为 png 时,背景被保存为白色。

我尝试使用其他类型,如 rgba 并使用函数 imread 加载照片。没有任何效果。

//loading the image  
var imgInput = new Image<Bgra,byte>(filePath);  

var imgInput2 = new Image<Rgb, byte>(imgInput.Size);  

//converting it to rgb                         
CvInvoke.CvtColor(imgInput,imgInput2,Emgu.CV.CvEnum.ColorConversion.Bgra2Rgb);

//not show here there is code to get the biggest contour

//generating a mask
var mask = new Image<Bgra, byte>(imgInput2.Size);
CvInvoke.Rectangle(mask, recBlack, new MCvScalar(255, 255, 255), thickness: -1);
//filling the mask with the biggest contour (the code not shown here)
CvInvoke.FillConvexPoly(mask, maxCountour.contour, new MCvScalar(0, 0,   0));
//removing the background using the mask with alpha set to zero
imgInput.Mat.SetTo(new MCvScalar(255, 255, 255, 0), mask);

//saving the png, here is the problem the background is not transparent
CvInvoke.Imwrite("test2.png", imgInput);

【问题讨论】:

    标签: c# opencv png emgucv


    【解决方案1】:
    var mask = new Image<Bgra, byte>(imgInput2.Size);
    

    当我将掩码图像数据类型更改为灰色时似乎可以工作。

    var mask = new Image<Grey, byte>(imgInput2.Size);
    

    【讨论】:

      猜你喜欢
      • 2021-08-24
      • 1970-01-01
      • 2011-09-23
      • 1970-01-01
      • 2012-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-14
      相关资源
      最近更新 更多