forked from upcAutoLang/BackgroundSplit-OpenCV
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
93 lines (82 loc) · 3.21 KB
/
Copy pathmain.cpp
File metadata and controls
93 lines (82 loc) · 3.21 KB
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
/*=================================================
* Version:
* v1.0: 原版程序由IplImage转换为Mat
===================================================
*/
#include "highgui.h"
#include "cv.h"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char *argv[])
{
VideoCapture capture;
capture = VideoCapture("./Video/Camera Road 01.avi");
if(!capture.isOpened())
{
capture = VideoCapture("../Video/Camera Road 01.avi");
if(!capture.isOpened())
{
capture = VideoCapture("../../Video/Camera Road 01.avi");
if(!capture.isOpened())
{
cout<<"ERROR: Did't find this video!"<<endl;
return 0;
}
}
}
// 用于遍历capture中的帧,通道数为3,需要转化为单通道才可以处理
Mat tmpFrame, tmpFrameF;
// 当前帧,单通道,uchar / Float
Mat currentFrame, currentFrameF;
// 上一帧,单通道,uchar / Float
Mat previousFrame, previousFrameF;
int frameNum = 0;
capture >> tmpFrame;
while(!tmpFrame.empty())
{
capture >> tmpFrame;
//tmpFrame=cvQueryFrame(capture);
frameNum++;
if(frameNum == 1)
{
// 第一帧先初始化各个结构,为它们分配空间
previousFrame.create(tmpFrame.size(), CV_8UC1);
currentFrame.create(tmpFrame.size(), CV_8UC1);
currentFrameF.create(tmpFrame.size(), CV_32FC1);
previousFrameF.create(tmpFrame.size(), CV_32FC1);
tmpFrameF.create(tmpFrame.size(), CV_32FC1);
}
if(frameNum >= 2)
{
// 转化为单通道灰度图,此时currentFrame已经存了tmpFrame的内容
cvtColor(tmpFrame, currentFrame, CV_BGR2GRAY);
currentFrame.convertTo(tmpFrameF, CV_32FC1);
previousFrame.convertTo(previousFrameF, CV_32FC1);
// 做差求绝对值
absdiff(tmpFrameF, previousFrameF, currentFrameF);
currentFrameF.convertTo(currentFrame, CV_8UC1);
/*
在currentFrameMat中找大于20(阈值)的像素点,把currentFrame中对应的点设为255
此处阈值可以帮助把车辆的阴影消除掉
*/
// threshold(currentFrameF, currentFrame, 20, 255.0, CV_THRESH_BINARY);
threshold(currentFrame, currentFrame, 30, 255, CV_THRESH_BINARY);
int g_nStructElementSize = 3; //结构元素(内核矩阵)的尺寸
// 获取自定义核
Mat element = getStructuringElement(MORPH_RECT,
Size(2 * g_nStructElementSize + 1, 2 * g_nStructElementSize + 1),
Point( g_nStructElementSize, g_nStructElementSize ));
// 膨胀
dilate(currentFrame, currentFrame, element);
// 腐蚀
erode(currentFrame, currentFrame, element);
}
//把当前帧保存作为下一次处理的前一帧
cvtColor(tmpFrame, previousFrame, CV_BGR2GRAY);
// 显示图像
imshow("Camera", tmpFrame);
imshow("Moving Area", currentFrame);
waitKey(25);
}
}