【发布时间】:2019-07-23 06:02:54
【问题描述】:
朋友们!有两个输入图像。一个背景图像,另一个蒙版图像。我需要得到面具的彩色部分。
我需要得到什么: background、mask、result image
但我得到了完全不同的东西:background、mask、result image
我的代码在 C# 中:
//Read files
Mat img1 = CvInvoke.Imread(Environment.CurrentDirectory + "\\Test\\All1.jpg");
Mat img = CvInvoke.Imread(Environment.CurrentDirectory + "\\Test\\OriginalMask.jpg");
// Threshold and MedianBlur mask
CvInvoke.Threshold(img, img, 0, 255, Emgu.CV.CvEnum.ThresholdType.BinaryInv);
CvInvoke.MedianBlur(img, img, 13);
// without this conversion, an error appears: (mtype == CV_8U || mtype == CV_8S) && _mask.sameSize(*psrc1)
CvInvoke.CvtColor(img, img, Emgu.CV.CvEnum.ColorConversion.Rgb2Gray);
CvInvoke.BitwiseNot(img1, img1, img);
//Save file
img1.Save(Environment.CurrentDirectory + "\\Test\\Result.jpg");
第一个问题:怎样才能达到图中的效果?
第二个问题:不转换掩码为什么会报错:(mtype == CV_8U || mtype == CV_8S) && _mask.sameSize(*psrc1)
第三个问题:如何在最终的图像中制作透明背景而不是白色背景?
解决方案不必在 C# 中。该解决方案适用于任何编程语言,因为语法 OpenCV 大致相同。提前谢谢你。
【问题讨论】:
-
关于你的第一个和第三个问题,请看下面我的回答。对于第二个问题:我认为,
img(也)作为三通道(彩色)图像加载,因为没有设置说明符。threshold和medianBlur也适用于彩色图像,而bitwise_not仅适用于单通道(灰度)图像。因此,如果事先没有转换,则会出现错误消息。
标签: c# python c++ windows opencv