目标:
- 使用OpenCV cv::inRange 函数进行基本的 阈值操作,
- 基于像素值在HSV色度空间的范围进行对象检测
理论:
- 前一篇文章中我们学习了如何使用cv::threshold 阈值函数进行阈值操作
- 本文我们将学习使用 cv::inRange 来进行处理
- 原理是一样的但是现在我们增加了一个我们所需要的 【像素值的范围】
HSV色度空间 HSV colorspace
HSV (hue色调, saturation饱和度, value明度) 是一种类似RGB色度模型的呈现颜色空间的模型。
因为H (色调)通道模拟了颜色类型,它在需要基于颜色进行对象分割的图像处理任务中非常有用。
饱和度的变化从呈现灰色阴影的不饱和到不含白色分量的全饱和。
明度通道则描述了颜色的亮度或者密度。
下图展示了HSV色度空间柱状图:
![HSV](https://docs.opencv.org/master/Threshold_inRange_HSV_colorspace.jpg)
由于RGB色度空间使用三通道进行色度编码,很难基本颜色进行对象分割。
![RGB](https://docs.opencv.org/master/Threshold_inRange_RGB_colorspace.jpg)
色度空间的相互转换可以使用 cv::cvtColor 。
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.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);
// 创建滑块用以调整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);
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;
}
编译完程序后运行结果类似下图:
原文参考:cv:InRange