OpenCV4开发入门教程159:基于颜色的对象跟踪

索引地址:系列索引

本文实现跟踪指定颜色,其实就是将指定颜色的目标明显的表示出来。既然是跟踪那么肯定是动态的目标。

我的移动硬盘是蓝色,本文就来跟踪它。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char* argv[])
{
// Create the capture object
// 0 -> input arg that specifies it should take the input from the webcam
VideoCapture cap(0);

if(!cap.isOpened())
{
cerr << "Unable to open the webcam. Exiting!" << endl;
return -1;
}

Mat frame, hsvImage, mask, outputImage;
char ch;

// Image size scaling factor for the input frames from the webcam
float scalingFactor = 0.75;

// Iterate until the user presses the Esc key
while(true)
{
// Initialize the output image before each iteration
outputImage = Scalar(0,0,0);

// Capture the current frame
cap >> frame;

// Check if 'frame' is empty
if(frame.empty())
break;

// Resize the frame
resize(frame, frame, Size(), scalingFactor, scalingFactor, INTER_AREA);

// Convert to HSV colorspace
cvtColor(frame, hsvImage, COLOR_BGR2HSV);

// Define the range of "blue" color in HSV colorspace
Scalar lowerLimit = Scalar(60,100,100);
Scalar upperLimit = Scalar(180,255,255);

// Threshold the HSV image to get only blue color
inRange(hsvImage, lowerLimit, upperLimit, mask);

// Compute bitwise-AND of input image and mask
bitwise_and(frame, frame, outputImage, mask=mask);

// Run median filter on the output to smoothen it
medianBlur(outputImage, outputImage, 5);

// Display the input and output image
imshow("Input", frame);
imshow("Output", outputImage);

// Get the keyboard input and check if it's 'Esc'
// 30 -> wait for 30 ms
// 27 -> ASCII value of 'ESC' key
ch = waitKey(30);
if (ch == 27) {
break;
}
}

return 0;
}

效果为:

colortracker

动态调整颜色范围

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Program to display a video from attached default camera device and detect colored blobs using
// simple R G and B thresholding Author: Samarth Manoj Brahmbhatt, University of Pennsylvania

//---------------------------------【头文件、命名空间包含部分】----------------------------
// 描述:包含程序所使用的头文件和命名空间
//------------------------------------------------------------------------------------------------
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;

//-----------------------------------【全局变量声明部分】---------------------------------------
// 描述:声明全局变量
//---------------------------------------------------------------------------------------------------
Mat frame, frame_thresholded;
int rgb_slider = 0, low_slider = 30, high_slider = 100;
int low_r = 30, low_g = 30, low_b = 30, high_r = 100, high_g = 100, high_b = 100;

//-----------------------------------【on_rgb_trackbar( )函数】------------------------------
// 描述:滑动条回调函数
//-------------------------------------------------------------------------------------------------
void on_rgb_trackbar(int, void *) {
switch (rgb_slider) {
case 0:
setTrackbarPos("Low threshold", "Segmentation", low_r);
setTrackbarPos("High threshold", "Segmentation", high_r);
break;
case 1:
setTrackbarPos("Low threshold", "Segmentation", low_g);
setTrackbarPos("High threshold", "Segmentation", high_g);
break;
case 2:
setTrackbarPos("Low threshold", "Segmentation", low_b);
setTrackbarPos("High threshold", "Segmentation", high_b);
break;
}
}

//------------------------------【on_low_thresh_trackbar( )函数】--------------------------
// 描述:滑动条回调函数
//-------------------------------------------------------------------------------------------------
void on_low_thresh_trackbar(int, void *) {
switch (rgb_slider) {
case 0:
low_r = min(high_slider - 1, low_slider);
setTrackbarPos("Low threshold", "Segmentation", low_r);
break;
case 1:
low_g = min(high_slider - 1, low_slider);
setTrackbarPos("Low threshold", "Segmentation", low_g);
break;
case 2:
low_b = min(high_slider - 1, low_slider);
setTrackbarPos("Low threshold", "Segmentation", low_b);
break;
}
}

//------------------------------【on_high_thresh_trackbar( )函数】-------------------------
// 描述:滑动条回调函数
//-------------------------------------------------------------------------------------------------
void on_high_thresh_trackbar(int, void *) {
switch (rgb_slider) {
case 0:
high_r = max(low_slider + 1, high_slider);
setTrackbarPos("High threshold", "Segmentation", high_r);
break;
case 1:
high_g = max(low_slider + 1, high_slider);
setTrackbarPos("High threshold", "Segmentation", high_g);
break;
case 2:
high_b = max(low_slider + 1, high_slider);
setTrackbarPos("High threshold", "Segmentation", high_b);
break;
}
}

//-----------------------------------【main( )函数】--------------------------------------------
// 描述:控制台应用程序的入口函数,我们的程序从这里开始
//-------------------------------------------------------------------------------------------------
int main() {
//从摄像头载入视频
VideoCapture cap(0);

if (!cap.isOpened()) {
cout << "Capture could not be opened succesfully" << endl;
return -1;
}

namedWindow("Video");
namedWindow("Segmentation");

createTrackbar("0. R\n1. G\n2.B", "Segmentation", &rgb_slider, 2, on_rgb_trackbar);
createTrackbar("Low threshold", "Segmentation", &low_slider, 255, on_low_thresh_trackbar);
createTrackbar("High threshold", "Segmentation", &high_slider, 255, on_high_thresh_trackbar);

double time = 0;
unsigned int frames = 0;
while (char(waitKey(1)) != 'q' && cap.isOpened()) {
frames++;
double t0 = getTickCount();
cap >> frame;
// 视频是否为空
if (frame.empty()) {
cout << "Video over" << endl;
break;
}

inRange(frame, Scalar(low_b, low_g, low_r), Scalar(high_b, high_g, high_r),
frame_thresholded);

imshow("Video", frame);
imshow("Segmentation", frame_thresholded);
time += (getTickCount() - t0) / getTickFrequency();
cout << frames / time << " fps" << endl;
}

return 0;
}

效果为

dyna

OpenCV4开发入门教程159:基于颜色的对象跟踪
https://feater.top/opencv/opencv-track-colored-object
作者
JackeyLea
发布于
2020年11月10日
许可协议