简体   繁体   English

更改通过cvGrabCut()C#获得的前景图像的背景色

[英]Changing background color of foreground image obtained by cvGrabCut() c#

I have used cvGrabCut() method from emgu cv in c# to extract foreground image. 我在c#中使用cvGrabCut() cv中的cvGrabCut()方法提取前景图像。 The result is satisfactory and I can extract foreground image with black background. 结果令人满意,我可以提取黑色背景的前景图像。 But now I have to change the obtained black color background image to some different color. 但是现在我必须将获得的黑色背景图像更改为某些不同的颜色。 How can I do that? 我怎样才能做到这一点? I have used the following code for foreground extraction: 我已使用以下代码进行前景提取:

mask1.Draw(rect, new Gray(3), 0);
//the models (internally used)
Matrix<double> bgModel = new Matrix<double>(1, 13 * 5);
Matrix<double> fgModel = new Matrix<double>(1, 13 * 5);
//initialization with mask
CvInvoke.CvGrabCut(image.Ptr, mask1.Ptr, ref rect, bgModel.Ptr, fgModel.Ptr, 10, Emgu.CV.CvEnum.GRABCUT_INIT_TYPE.INIT_WITH_MASK);
//GrabCut segmentation
CvInvoke.CvGrabCut(image.Ptr, mask1.Ptr, ref rect, bgModel.Ptr, fgModel.Ptr, 15, Emgu.CV.CvEnum.GRABCUT_INIT_TYPE.EVAL);
//Get the pixels marked as likely foreground
mask1 = mask1.And(new Gray(1));
Image<Bgr, Byte> result = image.Copy(mask1);
result.Save("foreground.jpg");

From the documentation it looks like you should change one or both of the 'new Gray(n)' with a 'new Bgr(b,g,r)' where the characters are respectively blue, green, and red (as in reverse RGB). 从文档看来,您应该用“新Bgr(b,g,r)”更改“新Gray(n)”之一或全部,其中字符分别是蓝色,绿色和红色(如反向RGB) )。

Look here Documentation 在这里查看文档

After getting the result from grabcut, you can iterate through the image and check for black pixels in 3-channels(RGB). 从抓取得到结果后,您可以遍历图像并检查3通道(RGB)中的黑色像素。 Just replace the corresponding color values for those detected black pixels. 只需为检测到的黑色像素替换相应的颜色值即可。 Sample: 样品:

rows,cols = output.shape[:2] 行,cols = output.shape [:2]

b,g,r = cv2.split(output)
    for i in range(rows):
        for j in range(cols):
            if(r[i, j] == 0 and g[i, j] == 0 and b[i, j] == 0):
                r[i, j] = 255
                g[i, j] = 255
                b[i, j] = 255

Here i am changing from black to white. 在这里,我从黑色变成白色。 But this code is in python. 但是这段代码在python中。 You can do similar in c#. 您可以在c#中执行类似的操作。

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

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