【问题标题】:how to set mask to GrabCut method in emgu-cv c#?如何在 emgu-cv c# 中为 GrabCut 方法设置掩码?
【发布时间】:2020-07-06 18:20:49
【问题描述】:

我正在尝试在 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

我明白了

【问题讨论】:

  • 我想我在最终图像中看到了与您的面具相匹配的 T 恤。你觉得它有什么问题?

标签: c# opencv image-processing emgucv


【解决方案1】:

这段代码现在可以工作了:)

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-30
    • 1970-01-01
    • 1970-01-01
    • 2017-06-03
    • 1970-01-01
    相关资源
    最近更新 更多