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

How to save an image with transparent background in 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);