如何在emgu-cv c#中将遮罩设置为GrabCut方法?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在emgu-cv c#中将遮罩设置为GrabCut方法?相关的知识,希望对你有一定的参考价值。
我正在尝试在emgu cv中使用GrabCut方法
这是我的代码
Matrix<double> bg = new Matrix<double>(1, 65);
bg.SetZero();
Matrix<double> fg = new Matrix<double>(1, 65);
fg.SetZero();
Image<Gray, byte> mask = new Image<Gray, byte>(img.Size);
Rectangle rect = new Rectangle(img.Cols / 4, 0, (int)((double)img.Width / (0.75)), img.Height );
CvInvoke.GrabCut(img, mask, rect,
bg, fg, 5, Emgu.CV.CvEnum.GrabcutInitType.InitWithRect);
for (int x = 0; x < mask.Cols; x++)
for (int y = 0; y < mask.Rows; y++)
if (mask[y, x].Intensity == new Gray(1).Intensity || mask[y, x].Intensity == new Gray(3).Intensity)
mask[y, x] = new Gray(1);
else
mask[y, x] = new Gray(0);
img = img.Mul(mask.Convert<Bgr,byte>());
imageBox3.Image = img;
我的img:
结果:
但是我想要T恤,所以我尝试使用这个面具(我在photoshop中制作了)
mask:
我改变了面具,所以我的代码变成了这样:
Matrix<double> bg = new Matrix<double>(1, 65);
bg.SetZero();
Matrix<double> fg = new Matrix<double>(1, 65);
fg.SetZero();
Image<Gray, byte> mask = new Image<Gray, byte>(@"C:\Users\iP\Desktop\exaples\mas.jpg");
//here i set the only white pixels (foreground object ) to 1 and 0 for else
for (int x = 0; x < mask.Cols; x++)
for (int y = 0; y < mask.Rows; y++)
if (mask[y, x].Intensity > new Gray(200).Intensity)
mask[y, x] = new Gray(1);
else
mask[y, x] = new Gray(0);
Rectangle rect = new Rectangle(img.Cols / 4, 0, (int)((double)img.Width / (0.75)), img.Height );
CvInvoke.GrabCut(img, mask, rect,
bg, fg, 5, Emgu.CV.CvEnum.GrabcutInitType.InitWithRect);
for (int x = 0; x < mask.Cols; x++)
for (int y = 0; y < mask.Rows; y++)
if (mask[y, x].Intensity == new Gray(1).Intensity || mask[y, x].Intensity == new Gray(3).Intensity)
mask[y, x] = new Gray(1);
else
mask[y, x] = new Gray(0);
img = img.Mul(mask.Convert<Bgr,byte>());
CvInvoke.Imshow("result", img);
但是我用第一个口罩得到了相同的结果(没有T恤)
我的代码中的错误在哪里?
并且我尝试将Emgu.CV.CvEnum.GrabcutInitType.InitWithRect更改为Emgu.CV.CvEnum.GrabcutInitType.InitWithMask
我知道了
答案
此代码现在正在运行:)
Matrix<double> bg = new Matrix<double>(1, 65);
bg.SetZero();
Matrix<double> fg = new Matrix<double>(1, 65);
fg.SetZero();
Image<Gray, byte> mask = new Image<Gray, byte>(img.Size);
Rectangle rect = new Rectangle(img.Cols / 4, 0, (int)((double)img.Width / (0.75)), img.Height );
CvInvoke.GrabCut(img, mask, rect,
bg, fg, 5, Emgu.CV.CvEnum.GrabcutInitType.InitWithRect);
Image<Gray, byte> mask2 = new Image<Gray, byte>(@"C:\Users\iP\Desktop\exaples\mas.jpg");
////here i set the only white pixels (foreground object ) to 1 and 0 for else
for (int x = 0; x < mask.Cols; x++)
for (int y = 0; y < mask.Rows; y++)
if (mask2[y, x].Intensity > new Gray(200).Intensity)
mask[y, x] = new Gray(1);
else
CvInvoke.GrabCut(img, mask, rect,
bg, fg, 5, Emgu.CV.CvEnum.GrabcutInitType.InitWithMask);
for (int x = 0; x < mask.Cols; x++)
for (int y = 0; y < mask.Rows; y++)
if (mask[y, x].Intensity == new Gray(1).Intensity || mask[y, x].Intensity == new Gray(3).Intensity)
mask[y, x] = new Gray(1);
else
mask[y, x] = new Gray(0);
img = img.Mul(mask.Convert<Bgr,byte>());
CvInvoke.Imshow("result", img);
结果:
视频:https://www.youtube.com/watch?v=463TMas4vHU
以上是关于如何在emgu-cv c#中将遮罩设置为GrabCut方法?的主要内容,如果未能解决你的问题,请参考以下文章