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