OpenCVSharp 4.5 Canny边缘检测

用OpenCVSharp 4.5 跑一遍OpenCV官方教程

官方OpenCV教程链接:https://docs.opencv.org/4.5.0/da/d5c/tutorial_canny_detector.html

核心函数:Canny

 

using System;
using OpenCvSharp;

namespace ConsoleApp1
{
    class tutorial14 : ITutorial
    {
        // Declare the variables we are going to use            

        static int lowThreshold = 0;
        static int max_lowThreshold = 100;
        static int ratio = 3;
        static int kernel_size = 3;

        static Mat src, src_gray;
        static Mat dst, detected_edges;
        const string window_name = "Edge Map";

        static void CannyThreshold(int pos, IntPtr userdata)
        {
            using (detected_edges = new Mat())
            {
                Cv2.Blur(src_gray, detected_edges, new Size(3, 3));
                Cv2.Canny(detected_edges, detected_edges, lowThreshold, lowThreshold * ratio, kernel_size);
                dst = new Mat();
                src.CopyTo(dst, detected_edges);
                Cv2.ImShow(window_name, dst);
            }
        }

        public void Run()
        {            
            string imageName = "I:\\csharp\\images\\lena.jpg";
            src = new Mat(imageName, ImreadModes.Color);
            if(src.Empty())
            {
                Console.WriteLine("Could not open or find the image!");
                return;
            }
            src_gray = new Mat();
            Cv2.CvtColor(src, src_gray, ColorConversionCodes.BGR2GRAY);
            Cv2.NamedWindow(window_name, WindowFlags.AutoSize);
            Cv2.CreateTrackbar("Min Threshold:", window_name, ref lowThreshold, max_lowThreshold, CannyThreshold);
            CannyThreshold(0, IntPtr.Zero);
            Cv2.WaitKey(0);
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值