我试图通过创建遮罩并使用创建的遮罩减去背景来从图像中删除背景。当我将图片保存为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);
答案 0 :(得分:0)
var mask = new Image<Bgra, byte>(imgInput2.Size);
当我将蒙版图像数据类型更改为灰色时,似乎可以正常工作。
var mask = new Image<Grey, byte>(imgInput2.Size);