Как установить маску для метода GrabCut в emgu-cv c#? - PullRequest
1 голос
/ 26 марта 2020

я пытаюсь использовать метод GrabCut в emgu cv

это мой код

 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:

enter image description here

результат:

enter image description here

но я хочу футболку, поэтому я попытался использовать эту маску (я сделал это в фотошопе)

маска:

enter image description here

и я изменил маску, чтобы мой код стал таким:

 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 to
Emgu.CV.CvEnum.GrabcutInitType.InitWithMask

, и я получаю это

enter image description here

1 Ответ

1 голос
/ 26 марта 2020

этот код работает сейчас :)

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);

результат:

enter image description here

видео: https://www.youtube.com/watch?v=463TMas4vHU

...