c# - 如何在 EmguCV 中保存具有透明背景的图像

标签 c# opencv png emgucv

我试图通过创建蒙版并使用创建的蒙版减去背景来从图像中删除背景。当我将图像保存为 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);

最佳答案

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

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

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

关于c# - 如何在 EmguCV 中保存具有透明背景的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56406268/

相关文章:

c# - 使用用户名和密码连接到 SQL Server

c# - 打开可空 bool 值 : case goes to null when value is true

python - 有什么简单的方法可以计算弯曲扇区中图像的像素

macos - OpenCV——记录浏览器窗口而不是捕获相机输出?

html - 更改使用 css + png 构建的按钮的状态

php - 在 PHP 中将 JPG/GIF 图像转换为 PNG?

c# - 如何在 Unity 中为 Android 游戏仅获取 "jump"命令的初始触摸

c# - 在 C# 中将字符串转换为控件名称

image - 为什么颜色反转时 1bpp PNG 图像文件大小会增加?

opencv - 实时图像稳定