简体   繁体   中英

How to save an image with transparent background in EmguCV

I am trying to remove the background from an image by creating a mask and subtracting the background using the mask created. When I save the image to png the background is being saved as white.

I tried to use other type like rgba and using the function imread to load the photo. Nothing worked.

//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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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