OpenCVSharp 4.5 使用InRange函数进行视频图像阈值分割

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值