[英]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.