如何在emgu-cv c#中将遮罩设置为GrabCut方法?

问题描述 投票:1回答:1

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

enter image description here

结果:

enter image description here

但是我想要T恤,所以我尝试使用这个面具(我在photoshop中制作了)

mask:

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

但是我用第一个口罩得到了相同的结果(没有T恤)

我的代码中的错误在哪里?

并且我尝试将Emgu.CV.CvEnum.GrabcutInitType.InitWithRect更改为Emgu.CV.CvEnum.GrabcutInitType.InitWithMask

我知道了

enter image description here

c# opencv image-processing emgucv
1个回答
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);

结果:

enter image description here

视频:https://www.youtube.com/watch?v=463TMas4vHU

© www.soinside.com 2019 - 2024. All rights reserved.