OpenCVSharp 4.5 跑一遍OpenCV官方教程(全为手敲代码,如有雷同都是我的错)
使用InRange函数进行视频图像阈值分割
OpenCV教程链接:https://docs.opencv.org/4.5.0/da/d97/tutorial_threshold_inRange.html
核心函数:InRange()
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class tutorial10 : ITutorial
{
static int max_value_H = 360 / 2;
static int max_value = 255;
static String window_capture_name = "Video Capture";
static String window_detection_name = "Object Detection";
static int low_H = 0, low_S = 0, low_V = 0;
static int high_H = max_value_H, high_S = max_value, high_V = max_value;
static void on_low_H_thresh_trackbar(int pos, IntPtr userdata)
{
low_H = Math.Min(high_H - 1, low_H);
Cv2.SetTrackbarPos("Low H", window_detection_name, low_H);
}
static void on_high_H_thresh_trackbar(int pos, IntPtr userdata)
{
high_H = Math.Max(high_H, low_H + 1);
Cv2.SetTrackbarPos("High H", window_detection_name, high_H);
}
static void on_low_S_thresh_trackbar(int pos, IntPtr userdata)
{
low_S = Math.Min(high_S - 1, low_S);
Cv2.SetTrackbarPos("Low S", window_detection_name, low_S);
}
static void on_high_S_thresh_trackbar(int pos, IntPtr userdata)
{
high_S = Math.Max(high_S, low_S + 1);
Cv2.SetTrackbarPos("High S", window_detection_name, high_S);
}
static void on_low_V_thresh_trackbar(int pos, IntPtr userdata)
{
low_V = Math.Min(high_V - 1, low_V);
Cv2.SetTrackbarPos("Low V", window_detection_name, low_V);
}
static void on_high_V_thresh_trackbar(int pos, IntPtr userdata)
{
high_V = Math.Max(high_V, low_V + 1);
Cv2.SetTrackbarPos("High V", window_detection_name, high_V);
}
public void Run()
{
VideoCapture cap = new VideoCapture(0, 0);
Cv2.NamedWindow(window_capture_name);
Cv2.NamedWindow(window_detection_name);
// Trackbars to set thresholds for HSV values
Cv2.CreateTrackbar("Low H", window_detection_name, ref low_H, max_value_H, on_low_H_thresh_trackbar);
Cv2.CreateTrackbar("High H", window_detection_name, ref high_H, max_value_H, on_high_H_thresh_trackbar);
Cv2.CreateTrackbar("Low S", window_detection_name, ref low_S, max_value, on_low_S_thresh_trackbar);
Cv2.CreateTrackbar("High S", window_detection_name, ref high_S, max_value, on_high_S_thresh_trackbar);
Cv2.CreateTrackbar("Low V", window_detection_name, ref low_V, max_value, on_low_V_thresh_trackbar);
Cv2.CreateTrackbar("High V", window_detection_name, ref high_V, max_value, on_high_V_thresh_trackbar);
Mat frame = new Mat();
Mat frame_HSV = new Mat();
Mat frame_threshold = new Mat();
while (true)
{
if (cap.Read(frame) == false)
{
continue;
}
// Convert from BGR to HSV colorspace
Cv2.CvtColor(frame, frame_HSV, ColorConversionCodes.BGR2HSV);
// Detect the object based on HSV Range Values
Cv2.InRange(frame_HSV, new Scalar(low_H, low_S, low_V), new Scalar(high_H, high_S, high_V), frame_threshold);
// Show the frames
Cv2.ImShow(window_capture_name, frame);
Cv2.ImShow(window_detection_name, frame_threshold);
char key = (char)Cv2.WaitKey(30);
if (key == 'q' || key == 27)
{
break;
}
}
}
}
}