opencvSharp4学习1 安装,读取,灰度,轮廓检测

wpf 使用opencvSharp,需要nuget安装四个库

 

 Mat imgscr = Cv2.ImRead(fName);//读取图像

            // Read image and preprocess

            //转换为灰度图像
            Mat gray = new Mat();
            
            //参数:1 原图矩阵容器 2:保存图像的矩阵容器 3:颜色转换通道(很多,查手册)
            Cv2.CvtColor(imgscr, gray, ColorConversionCodes.RGB2GRAY); //转为灰度空间图像,

            Mat outImage = new Mat(); //声明一个容器,装载改变后的图像

            Mat blur = new Mat();
            OpenCvSharp.Size ksize = new OpenCvSharp.Size(3, 3);
            Cv2.GaussianBlur(gray, blur, ksize, 0);

            //show_images([gray, blur])

            Mat edged = new Mat();
            //edged = cv2.Canny(blur, 50, 100)
            Cv2.Canny(blur, edged, 30, 90);

           
            Mat s1 = new Mat();

            int  size = 3 * 2 + 1; //要为奇数
            Mat se = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(size, size), new OpenCvSharp.Point(-1, -1));

            //膨胀
            Cv2.Dilate(edged, s1, se, new OpenCvSharp.Point(-1, -1), 1);

            Cv2.Erode(s1, s1, se, new OpenCvSharp.Point(-1, -1), 1);


           

            //获得轮廓
            OpenCvSharp.Point[][] contours;
            HierarchyIndex[] hierarchly;
            Cv2.FindContours(s1, out contours, out hierarchly, RetrievalModes.Tree, ContourApproximationModes.ApproxSimple, new OpenCvSharp.Point(0, 0));

            //将结果画出并返回结果
            Mat dst_Image = Mat.Zeros(s1.Size(), s1.Type());
            Random rnd = new Random();
            for (int i = 0; i < contours.Length; i++)
            {
                Scalar color = new Scalar(255, 0, 250);
                Cv2.DrawContours(imgscr, contours, i, color, 2, LineTypes.Link8, hierarchly);
            }
            ///(1)转为 bitmap:
            cnt.Content = "计数:" + contours.Length;

            BitmapSource map = OpenCvSharp.WpfExtensions.BitmapSourceConverter.ToBitmapSource(imgscr);

            outImg.Source = map;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值