OpenCVSharp 4.5 跑一遍OpenCV官方教程(全为手敲代码,如有雷同都是我的错)
阈值
OpenCV教程链接:https://docs.opencv.org/4.5.0/db/d8e/tutorial_threshold.html
核心语句
- threshold
using OpenCvSharp;
using System;
namespace ConsoleApp1
{
class tutorial9 : ITutorial
{
static int threshold_value = 0;
static int threshold_type = 3;
static int max_value = 255;
static int max_type = 4;
static int max_binary_value = 255;
static Mat src, src_gray, dst;
static string window_name = "Threshold Demo";
static string trackbar_type = "Type: \n 0: Binary \n 1: Binary Inverted \n 2: Truncate \n 3: To Zero \n 4: To Zero Inverted";
static string trackbar_value = "Value";
static void Threshold_Demo(int pos , IntPtr userdata)
{
/* 0: Binary
1: Binary Inverted
2: Threshold Truncated
3: Threshold to Zero
4: Threshold to Zero Inverted
*/
Cv2.Threshold(src_gray, dst, threshold_value, max_binary_value, (ThresholdTypes)threshold_type);
Cv2.ImShow(window_name, dst);
}
public void Run()
{
string imageName = "I:\\csharp\\images\\stuff.jpg";
src = Cv2.ImRead(imageName, ImreadModes.Color); // Load an image
if (src.Empty())
{
Console.WriteLine("Cannot read the image:{0}", imageName);
return;
}
src_gray = new Mat();
dst = new Mat();
Cv2.CvtColor(src, src_gray, ColorConversionCodes.BGR2GRAY); // Convert the image to Gray
Cv2.NamedWindow(window_name, WindowFlags.AutoSize); // Create a window to display results
Cv2.CreateTrackbar(trackbar_type,
window_name, ref threshold_type,
max_type, Threshold_Demo); // Create a Trackbar to choose type of Threshold
Cv2.CreateTrackbar(trackbar_value,
window_name, ref threshold_value,
max_value, Threshold_Demo); // Create a Trackbar to choose Threshold value
Threshold_Demo(0, IntPtr.Zero); // Call the function to initialize
Cv2.WaitKey();
}
}
}