繁体   English   中英

如何在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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM