如何在 emgu-cv c# 中将掩码设置为 GrabCut 方法?
how to set mask to GrabCut method in emgu-cv c#?
我正在尝试在 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;
我的头像:
结果:
但我想要这件 T 恤,所以我尝试使用这个面具(我在 photoshop 中制作)
面具:
我更改了掩码,所以我的代码变成了这样:
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);
结果:
我正在尝试在 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;
我的头像:
结果:
但我想要这件 T 恤,所以我尝试使用这个面具(我在 photoshop 中制作)
面具:
我更改了掩码,所以我的代码变成了这样:
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);
结果: