URL Image Matting

 

    private void GrabCutGood()
        {
          
            Mat image = Cv2.ImRead("C:\\Users\\E053707\\Pictures\\bing\\aa.jpg", ImreadModes.Color);
             
            Mat srcgary = new Mat();

            Mat dst = new Mat(image.Size(), MatType.CV_32SC1);
            Mat gaosi = new Mat();


         //   Cv2.CvtColor(image, srcgary, ColorConversionCodes.BGR2BGRA);

            Cv2.GaussianBlur(image, gaosi, new OpenCvSharp.Size(3, 3), 0);
            image = KMeansHelper.GetMat(gaosi, 120);


            Cv2.ImShow("image", image);

            Mat gaussian_image = new Mat();
            Mat USM_image = new Mat();

            Cv2.GaussianBlur(image,gaussian_image, new OpenCvSharp.Size(3, 3), 0);


            Cv2.AddWeighted(image, 1.5, gaussian_image, -0.5, 0, USM_image);

            Cv2.ConvertScaleAbs(USM_image, USM_image);


            Cv2.ImShow("USM_image", USM_image);

            Rect roi_rect = new Rect(0, 0, image.Rows-1, image.Cols-1);// Cv2.SelectROI(USM_image, false);

            Mat mask = Mat.Zeros(image.Size(), MatType.CV_8UC1);
            Mat mask_Color = Mat.Zeros(image.Size(), MatType.CV_8UC1);

            Mat bgdModel = new Mat();
            Mat fgdModel = new Mat();
            Cv2.GrabCut(USM_image, mask, roi_rect, bgdModel, fgdModel, 10, GrabCutModes.InitWithRect);

            Mat foreground = Mat.Zeros(image.Size(), image.Type());

            Mat foreground_roi = Mat.Zeros(image.Size(), MatType.CV_8UC3);

            for(int row = 0; row < image.Rows; row++)
            {
                for(int col = 0; col < image.Cols; col++)
                {
                    if (mask.At<byte>(row, col) == 0   )
                    {
                      mask_Color.At<Vec3b>(row, col) = new Vec3b(0, 0, 0);
                    }
                    else if (mask.At<byte>(row, col) == 1)
                    {
                        mask_Color.At<Vec3b>(row, col) = new Vec3b(255, 255, 255);
                        foreground_roi.At<Vec3b>(row, col) = new Vec3b(255, 255, 255);
                    }
                    else if (mask.At<byte>(row, col) == 2)
                    {
                        mask_Color.At<Vec3b>(row, col) = new Vec3b(80, 80, 80);
                    }
                    else if (mask.At<byte>(row, col) == 3)
                    {
                        mask_Color.At<Vec3b>(row, col) = new Vec3b(128, 128, 128);
                        foreground_roi.At<Vec3b>(row, col) = new Vec3b(255, 255, 255);
                    }
                }
            }
            Cv2.ImShow("mask", mask_Color);

            Cv2.ImShow("foreground_roi", foreground_roi);

            //Mat kernel = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));

            //Cv2.MorphologyEx(foreground_roi, foreground_roi, MorphTypes.Open, kernel);

            //Cv2.BitwiseAnd(foreground_roi, USM_image, foreground);
            //Cv2.ImShow("Foreground", foreground);


            //Mat background = Mat.Zeros(image.Size(), MatType.CV_8UC3);

            //Mat new_background = background.Clone();

            //for(int row = 0; row < background.Rows; row++)
            //{
            //    for(int col = 0; col < background.Cols; col++)
            //    {
            //        if(foreground.At<Vec3b>(row,col)[0]!=0&& foreground.At<Vec3b>(row, col)[1] != 0&& foreground.At<Vec3b>(row, col)[2] != 0)
            //        {
            //            new_background.At<Vec3b>(row, col) = image.At<Vec3b>(row, col);
            //        }
            //    }
            //}

            //Cv2.ImShow("new_background", new_background);


            //Mat gaus_background = Mat.Zeros(background.Size(), background.Type());


            //Cv2.GaussianBlur(background,gaus_background, new OpenCvSharp.Size(3, 3), 2,2);

            //for(int row = 0; row < gaus_background.Rows; row++)
            //{
            //    for(int col = 0; col < gaus_background.Cols; col++)
            //    {
            //        if (foreground.At<Vec3b>(row, col)[0] != 0 && foreground.At<Vec3b>(row, col)[1] != 0 && foreground.At<Vec3b>(row, col)[2] != 0)
            //        {
            //            gaus_background.At<Vec3b>(row, col) = foreground.At<Vec3b>(row, col);
            //        }
            //    }
            //}
            //Cv2.GaussianBlur(gaus_background, gaus_background, new OpenCvSharp.Size(3, 3), 1);
            //Cv2.ImShow("gaus_background", gaus_background);
        }

          public class WatershedSample  
    {
        public   void RunTest()
        {
             var srcImg = Cv2.ImRead("C:\\Users\\E053707\\Pictures\\bing\\aa.jpg", ImreadModes.AnyDepth | ImreadModes.AnyColor);
             var markers = new Mat(srcImg.Size(), MatType.CV_32SC1, Scalar.All(0));

            using (var window = new Window("image", srcImg))
            {
                  var dspImg = srcImg.Clone();

                // Mouse event  
                int seedNum = 0;
                window.SetMouseCallback((MouseEventTypes ev, int x, int y, MouseEventFlags flags, IntPtr userdata) =>
                {
                    if (ev == MouseEventTypes.LButtonDown)
                    {
                        seedNum++;
                        var pt = new Point(x, y);
                        markers.Circle(pt, 10, Scalar.All(seedNum), Cv2.FILLED, LineTypes.Link8);
                        dspImg.Circle(pt, 10, Scalar.White, 3, LineTypes.Link8);
                        window.Image = dspImg;
                    }
                });
                Window.WaitKey();
            }

            Cv2.Watershed(srcImg, markers);

            // draws watershed
             var dstImg = srcImg.Clone();
            for (int y = 0; y < markers.Height; y++)
            {
                for (int x = 0; x < markers.Width; x++)
                {
                    int idx = markers.Get<int>(y, x);
                    if (idx == -1)
                    {
                        dstImg.Rectangle(new Rect(x, y, 2, 2), Scalar.Red, -1);
                    }
                }
            }

            using (new Window("watershed transform", dstImg))
            {
                Window.WaitKey();
            }
        }
    }
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Farmwang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值