opencv基础: 范围阈值

485 阅读3分钟

1. 目标

在本文,将学习如何:

  • 使用 OpenCV cv::inRange函数执行范围阈值操作。
  • 根据 HSV 颜色空间中的像素值范围检测对象。

2. 理论

  • 在上文中,学习了如何使用cv::threshold函数执行阈值处理。
  • 在本文中,将学习如何使用cv::inRange函数来实现。
  • 概念保持不变,但现在使用像素值的范围。

3. HSV 色彩空间

HSV(色调、饱和度、亮度)颜色空间是一种表示颜色空间的模型,类似于 RGB 颜色模型。由于色调通道对颜色类型进行建模,因此在需要根据其颜色分割对象的图像处理任务中非常有用。饱和度的变化从不饱和(灰色阴影)和完全饱和(无白色分量)。亮度通道描述颜色的亮度或强度。下一张图片显示了 HSV 颜色空间柱体。

Threshold_inRange_HSV_colorspace.jpg

由 SharkD 衍生作品:SharkD [CCB-SA 3.0 或 GFDL],来自 Wikimedia Commons

由于 RGB 颜色空间中的颜色是使用三个通道编码的,因此根据颜色对图像中的对象进行分割比较困难。

Threshold_inRange_RGB_colorspace.jpg

来自 Wikimedia Commons 的 SharkD [GFDL 或 CC BY-SA 4.0]

颜色转换中描述了用于使用cv::cvtColor函数从一个颜色空间转换到另一个颜色空间的公式

4. 代码

教程代码如下所示。你也可以从这里下载


#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/videoio.hpp"
#include <iostream>
using namespace cv;
const int max_value_H = 360/2;
const int max_value = 255;
const String window_capture_name = "Video Capture";
const String window_detection_name = "Object Detection";
int low_H = 0, low_S = 0, low_V = 0;
int high_H = max_value_H, high_S = max_value, high_V = max_value;
static void on_low_H_thresh_trackbar(int, void *)
{
    low_H = min(high_H-1, low_H);
    setTrackbarPos("Low H", window_detection_name, low_H);
}
static void on_high_H_thresh_trackbar(int, void *)
{
    high_H = max(high_H, low_H+1);
    setTrackbarPos("High H", window_detection_name, high_H);
}
static void on_low_S_thresh_trackbar(int, void *)
{
    low_S = min(high_S-1, low_S);
    setTrackbarPos("Low S", window_detection_name, low_S);
}
static void on_high_S_thresh_trackbar(int, void *)
{
    high_S = max(high_S, low_S+1);
    setTrackbarPos("High S", window_detection_name, high_S);
}
static void on_low_V_thresh_trackbar(int, void *)
{
    low_V = min(high_V-1, low_V);
    setTrackbarPos("Low V", window_detection_name, low_V);
}
static void on_high_V_thresh_trackbar(int, void *)
{
    high_V = max(high_V, low_V+1);
    setTrackbarPos("High V", window_detection_name, high_V);
}
int main(int argc, char* argv[])
{
    VideoCapture cap(argc > 1 ? atoi(argv[1]) : 0);
    namedWindow(window_capture_name);
    namedWindow(window_detection_name);
    // Trackbars to set thresholds for HSV values
    createTrackbar("Low H", window_detection_name, &low_H, max_value_H, on_low_H_thresh_trackbar);
    createTrackbar("High H", window_detection_name, &high_H, max_value_H, on_high_H_thresh_trackbar);
    createTrackbar("Low S", window_detection_name, &low_S, max_value, on_low_S_thresh_trackbar);
    createTrackbar("High S", window_detection_name, &high_S, max_value, on_high_S_thresh_trackbar);
    createTrackbar("Low V", window_detection_name, &low_V, max_value, on_low_V_thresh_trackbar);
    createTrackbar("High V", window_detection_name, &high_V, max_value, on_high_V_thresh_trackbar);
    Mat frame, frame_HSV, frame_threshold;
    while (true) {
        cap >> frame;
        if(frame.empty())
        {
            break;
        }
        // Convert from BGR to HSV colorspace
        cvtColor(frame, frame_HSV, COLOR_BGR2HSV);
        // Detect the object based on HSV Range Values
        inRange(frame_HSV, Scalar(low_H, low_S, low_V), Scalar(high_H, high_S, high_V), frame_threshold);
        // Show the frames
        imshow(window_capture_name, frame);
        imshow(window_detection_name, frame_threshold);
        char key = (char) waitKey(30);
        if (key == 'q' || key == 27)
        {
            break;
        }
    }
    return 0;
}

5. 代码解释

下边查看一下代码的主体结构:

  • 从默认或提供的捕获设备捕获视频流。

VideoCapture cap(argc > 1 ? atoi(argv[1]) : 0);
  • 创建一个窗口以显示默认框架和阈值框架。

 namedWindow(window_capture_name);
 namedWindow(window_detection_name);
  • 创建轨迹栏以设置 HSV 值的范围

// 用于设置 HSV 值阈值的轨迹栏


// Trackbars to set thresholds for HSV values
    createTrackbar("Low H", window_detection_name, &low_H, max_value_H, on_low_H_thresh_trackbar);
    createTrackbar("High H", window_detection_name, &high_H, max_value_H, on_high_H_thresh_trackbar);
    createTrackbar("Low S", window_detection_name, &low_S, max_value, on_low_S_thresh_trackbar);
    createTrackbar("High S", window_detection_name, &high_S, max_value, on_high_S_thresh_trackbar);
    createTrackbar("Low V", window_detection_name, &low_V, max_value, on_low_V_thresh_trackbar);
    createTrackbar("High V", window_detection_name, &high_V, max_value, on_high_V_thresh_trackbar);
  • 直到用户希望程序退出执行以下操作

 cap >> frame;
        if(frame.empty())
        {
            break;
        }
        // Convert from BGR to HSV colorspace
        cvtColor(frame, frame_HSV, COLOR_BGR2HSV);
        // Detect the object based on HSV Range Values
        inRange(frame_HSV, Scalar(low_H, low_S, low_V), Scalar(high_H, high_S, high_V), frame_threshold);
  • 显示图像

 // Show the frames
 imshow(window_capture_name, frame);
 imshow(window_detection_name, frame_threshold);
  • 对于控制较低范围的轨迹栏,例如色调值:

static void on_low_H_thresh_trackbar(int, void *)
{
    low_H = min(high_H-1, low_H);
    setTrackbarPos("Low H", window_detection_name, low_H);
}
static void on_low_H_thresh_trackbar(int, void *)
{
    low_H = min(high_H-1, low_H);
    setTrackbarPos("Low H", window_detection_name, low_H);
}
  • 对于控制上限范围的轨迹栏,例如色调值:

static void on_high_H_thresh_trackbar(int, void *)
{
    high_H = max(high_H, low_H+1);
    setTrackbarPos("High H", window_detection_name, high_H);
}
  • 必须找到最大值和最小值,以避免出现阈值高值小于低值等差异。

6. 结果

  • 编译并运行。该程序将打开两个窗口

  • 当从轨迹栏设置范围值时,生成的帧将在另一个窗口中可见。

    Threshold_inRange_Tutorial_Result_input.jpeg

    Threshold_inRange_Tutorial_Result_output.jpeg

原文地址