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;
내 이미지 :
결과 :
근데 티셔츠를 원해서이 마스크를 써보려고 했어요 (포토샵에서 만들었어요)
마스크 :
그리고 마스크를 변경하여 코드가 다음과 같이되었습니다.
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);
하지만 첫 번째 마스크로 같은 결과를 얻습니다 (티셔츠없이)
내 코드의 실수는 어디에 있습니까?
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);
결과 :
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다