opencv-077-视频读取与处理

知识点

OpenCV中对视频内容的处理本质上对读取视频的关键帧进行解析图像,然后对图像进行各种处理,OpenCV的VideoCapture是一个视频读取与解码的API接口,支持各种视频格式、网络视频流、摄像头读取。正常的视频处理与分析,主要是针对读取到每一帧图像,衡量一个算法处理是否能够满足实时要求的时候通常通过FPS(每秒多少帧的处理能力)。一般情况下每秒大于5帧基本上可以认为是在进行视频处理。

代码(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
#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

void process_frame(Mat &image, int opts);

/*
* 视频读取与处理
*/
int main() {
VideoCapture capture("../images/roadcars.avi");
if (!capture.isOpened()){
cout << "could not open video.." << endl;
return -1;
}
namedWindow("input");

int fps = capture.get(CAP_PROP_FPS);
int width = capture.get(CAP_PROP_FRAME_WIDTH);
int height = capture.get(CAP_PROP_FRAME_HEIGHT);
int num_of_frames = capture.get(CAP_PROP_FRAME_COUNT);
printf("frame width: %d, frame height: %d, FPS : %d \n", width, height, fps);

Mat frame;
int index = 0;
while(capture.isOpened()){
bool ret = capture.read(frame);
if (!ret) break;
imshow("input", frame);
char c = waitKey(50);
if (c >= 49){
index = c - 49;
}
process_frame(frame, index);
imshow("result", frame);
if (c == 27){
break;
}
}

waitKey(0);
return 0;
}

void process_frame(Mat &image, int opts) {
Mat dst = image.clone();
if (opts == 0){
bitwise_not(image, dst);
}
if (opts == 1){
GaussianBlur(image, dst, Size(0,0), 15);
}
if (opts == 2){
Canny(image, dst, 100, 200);
}
dst.copyTo(image);
dst.release();
}
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
import cv2 as cv

capture = cv.VideoCapture("../images/roadcars.avi")
height = capture.get(cv.CAP_PROP_FRAME_HEIGHT)
width = capture.get(cv.CAP_PROP_FRAME_WIDTH)
count = capture.get(cv.CAP_PROP_FRAME_COUNT)
fps = capture.get(cv.CAP_PROP_FPS)
print(height, width, count, fps)


def process(image, opt=1):
dst = None
if opt == 0:
dst = cv.bitwise_not(image)
if opt == 1:
dst = cv.GaussianBlur(image, (0, 0), 15)
if opt == 2:
dst = cv.Canny(image, 100, 200)
return dst


index = 0
while(True):
ret, frame = capture.read()
if ret is True:
cv.imshow("video-input", frame)
c = cv.waitKey(50)
if c >= 49:
index = c -49
result = process(frame, index)
cv.imshow("result", result)
print(c)
if c == 27: #ESC
break
else:
break
cv.waitKey(0)
cv.destroyAllWindows()

结果

代码地址

github