Goal
在本教程中,您将学习如何:
使用 OpenCV cv::inRange 函数执行基本阈值操作。
根据 HSV 颜色空间中的像素值范围检测对象。
Theory
在上一教程中,我们学习了如何使用 cv::threshold 函数执行阈值处理。
在本教程中,我们将学习如何使用 cv::inRange 函数来实现。
概念保持不变,但现在我们添加了我们需要的一系列像素值。
HSV colorspace
HSV(色相hue、饱和度saturation、值value)颜色空间是一种表示颜色空间的模型,类似于 RGB 颜色模型。 由于色调通道对颜色类型进行建模,因此在需要根据其颜色分割对象的图像处理任务中非常有用。 饱和度的变化从不饱和到表示灰色阴影和完全饱和(无白色分量)。 值Value通道描述颜色的亮度或强度。 下一张图片显示了 HSV 圆柱。
由 SharkD 衍生作品:SharkD [CC BY-SA 3.0 或 GFDL],来自 Wikimedia Commons
由于 RGB 颜色空间中的颜色是使用三个通道编码的,因此根据颜色对图像中的对象进行分割更加困难。
来自 Wikimedia Commons 的 SharkD [GFDL 或 CC BY-SA 4.0]
颜色转换中描述了用于使用 cv::cvtColor 函数从一个颜色空间转换到另一个颜色空间的公式
Code
教程代码如下所示。 你也可以从这里下载opencv/Threshold_inRange.cpp at 4.x · opencv/opencv · GitHub
#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;
}
Explanation
让我们检查一下程序的一般结构:
- 从默认或提供的捕获设备捕获视频流。
VideoCapture cap(argc > 1 ? atoi(argv[1]) : 0);
- 创建一个窗口以显示默认帧和阈值帧。
namedWindow(window_capture_name);
namedWindow(window_detection_name);
- 创建轨迹栏以设置 HSV 值的范围
// 用于设置 HSV 值阈值的跟踪栏
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);
// 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);
}
- 必须找到最大值和最小值,以避免出现阈值高值小于低值等错误。
Results
编译此程序后,运行它。 该程序将打开两个窗口
当您从滑动条设置范围值时,生成的帧将在另一个窗口中可见。
在实际项目开发过程中,可以通过调整滑动条观察窗口输出来选择合适的阈值。