opencv-088-视频分析(基于均值迁移的对象移动分析)

知识点

均值迁移移动对象分析,主要是基于直方图分布与反向投影实现移动对象的轨迹跟踪,其核心的思想是对反向投影之后的图像做均值迁移(meanshift)从而发现密度最高的区域,也是对象分布最大的域。完整的算法流程如下:

  1. 读取图像一帧
  2. HSV直方图
  3. 反向投影该帧
  4. 使用means shift寻找最大分布密度
  5. 更新窗口直至最后一帧

API

1
2
3
4
5
6
7
8
int cv::meanShift(
InputArray probImage,
Rect & window,
TermCriteria criteria
)
probImage输入图像,是直方图反向投影的结果
window 搜索窗口,ROI对象区域
criteria 均值迁移停止条件

代码(c++,python)

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
#include <opencv2/opencv.hpp>
#include <iostream>
#include <ctype.h>

using namespace cv;
using namespace std;

Mat image;
int trackObject = 0;

/*
* 视频分析(基于均值迁移的对象移动分析)
*/
int main() {
VideoCapture cap("../images/balltest.mp4");
Rect trackWindow;
int hsize = 16;
float hranges[] = {0, 180};
const float *phranges = hranges;

if (!cap.isOpened()) {
printf("could not open camera...\n");
return -1;
}

Mat frame, hsv, hue, mask, hist = Mat::zeros(200, 320, CV_8UC3), backproj;
cap.read(frame);
Rect selection = selectROI("CamShift Demo", frame, true, false);

while (true) {
bool ret = cap.read(frame);
if (!ret) break;
frame.copyTo(image);

cvtColor(image, hsv, COLOR_BGR2HSV);

inRange(hsv, Scalar(26, 43, 46), Scalar(34, 255, 255), mask);
int ch[] = {0, 0};
hue.create(hsv.size(), hsv.depth());
mixChannels(&hsv, 1, &hue, 1, ch, 1);

if (trackObject <= 0) {
// Object has been selected by user, set up CAMShift search properties once
Mat roi(hue, selection), maskroi(mask, selection);
calcHist(&roi, 1, 0, maskroi, hist, 1, &hsize, &phranges);
normalize(hist, hist, 0, 255, NORM_MINMAX);

trackWindow = selection;
trackObject = 1; // Don't set up again, unless user selects new ROI
}

// Perform meanShift
calcBackProject(&hue, 1, 0, hist, backproj, &phranges);
backproj &= mask;
meanShift(backproj, trackWindow, TermCriteria(TermCriteria::EPS | TermCriteria::COUNT, 10, 1));
rectangle(image, trackWindow, Scalar(0, 0, 255), 3, LINE_AA);

imshow("CamShift Demo", image);
char c = (char) waitKey(50);
if (c == 27)
break;
}

return 0;
}
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
import cv2 as cv
cap = cv.VideoCapture('D:/images/video/balltest.mp4')

# 读取第一帧
ret,frame = cap.read()
cv.namedWindow("CAS Demo", cv.WINDOW_AUTOSIZE)
x, y, w, h = cv.selectROI("CAS Demo", frame, True, False)
track_window = (x, y, w, h)

# 获取ROI直方图
roi = frame[y:y+h, x:x+w]
hsv_roi = cv.cvtColor(roi, cv.COLOR_BGR2HSV)
mask = cv.inRange(hsv_roi, (26, 43, 46), (34, 255, 255))
roi_hist = cv.calcHist([hsv_roi],[0],mask,[180],[0,180])
cv.normalize(roi_hist,roi_hist,0,255,cv.NORM_MINMAX)

# 设置搜索跟踪分析
term_crit = ( cv.TERM_CRITERIA_EPS | cv.TERM_CRITERIA_COUNT, 10, 1 )
while True:
ret, frame = cap.read()
if ret is False:
break;
hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
dst = cv.calcBackProject([hsv],[0],roi_hist,[0,180],1)

# 均值迁移,搜索更新roi区域
ret, track_window = cv.meanShift(dst, track_window, term_crit)

# 绘制窗口
x,y,w,h = track_window
cv.rectangle(frame, (x,y), (x+w,y+h), 255,2)
cv.imshow('CAS Demo',frame)
k = cv.waitKey(60) & 0xff
if k == 27:
break
cv.destroyAllWindows()
cap.release()

结果

代码地址

github