返回:OpenCV系列文章目录(持续更新中......)
上一篇:OpenCV4.9基本阈值操作(44)
下一篇:利用Opencv4.9为图像添加边框(46)
目标
在本教程中,您将学习如何:
- 使用 OpenCV cv::inRange 函数执行基本阈值操作。
- 根据 HSV 色彩空间中的像素值范围检测对象。
理论
- 在上一教程中,我们学习了如何使用 cv::threshold 函数执行阈值。
- 在本教程中,我们将学习如何使用 cv::inRange 函数来做到这一点。
- 概念保持不变,但现在我们添加了一系列我们需要的像素值。
HSV 色彩空间
HSV(色调、饱和度、值)色彩空间是表示类似于 RGB 颜色模型的色彩空间的模型。由于色相通道对颜色类型进行建模,因此在需要根据颜色分割对象的图像处理任务中非常有用。饱和度的变化从不饱和到表示灰色阴影和完全饱和(无白色分量)。值通道描述颜色的亮度或强度。下图显示了 HSV 气缸。
作者:SharkD衍生作品:SharkD [CC BY-SA 3.0或GFDL],通过Wikimedia Commons
由于 RGB 色彩空间中的颜色是使用三个通道进行编码的,因此根据图像中的颜色分割图像中的对象更加困难。
作者:SharkD [GFDL或CC BY-SA 4.0],来自维基共享资源
颜色转换中介绍了使用 cv::cvtColor 函数从一个颜色空间转换为另一个颜色空间的公式
代码
C++
The tutorial code's is shown lines below. You can also download it from here
#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;
}
解释
C++
让我们检查一下程序的一般结构:
-
从默认或提供的捕获设备捕获视频流。
VideoCapture cap(argc > 1 ? atoi(argv[1]) : 0);
创建一个窗口以显示默认帧和阈值帧。
namedWindow(window_capture_name);
namedWindow(window_detection_name);
创建跟踪栏以设置 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);
}
- 有必要找到最大值和最小值,以避免出现阈值的高值小于低值等差异。
结果
- 编译此程序后,运行它。该程序将打开两个窗口
- 当您从跟踪栏设置范围值时,生成的帧将在另一个窗口中可见。
参考文献:
1、《Thresholding Operations using inRange》------Lorena García