-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGMM.cpp
More file actions
87 lines (64 loc) · 3.97 KB
/
GMM.cpp
File metadata and controls
87 lines (64 loc) · 3.97 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
// GMM.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <opencv2/opencv.hpp>
#include"opencv2/core/core.hpp"
#include"opencv2/highgui/highgui.hpp"
#include"opencv2/imgproc/imgproc.hpp"
#include"opencv2/video/background_segm.hpp"
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
std::string videoFile = "768x576.avi";
cv::VideoCapture capture;
capture.open(videoFile);
if (!capture.isOpened())
{
std::cout << "read video failure" << std::endl;
return -1;
}
/******************************************************/
// YOUR CODE HERE :
//声明一个类型为BackgroundSubtractorMOG2,名称为mog的 变量,调整其中的参数并查看效果
Ptr<BackgroundSubtractorMOG2> mog = createBackgroundSubtractorMOG2(100, 36, false);
/******************************************************/
cv::Mat foreground;
cv::Mat foregroundRGB;
cv::Mat background;
cv::Mat frame;
long frameNo = 0;
capture.read(frame);
string outputVideoPath = ".\\GMM.avi";
VideoWriter outputVideo;
outputVideo.open(outputVideoPath, CV_FOURCC('M', 'J', 'P', 'G'), 10.0, cvSize(frame.cols / 2, frame.rows / 2), true);
while (capture.read(frame))
{
++frameNo;
std::cout << frameNo << std::endl;
cv::Mat ff;
resize(frame, ff, cvSize(frame.cols / 2, frame.rows / 2));
//保存为视频
/******************************************************/
// YOUR CODE HERE :
//使用BackgroundSubtractorMOG2类的()运算符更新背景,找到前景
mog->apply(ff, foreground, 0.001);
/******************************************************/
// 腐蚀
cv::erode(foreground, foreground, cv::Mat());
// 膨胀
cv::dilate(foreground, foreground, cv::Mat());
mog->getBackgroundImage(background); // 返回当前背景图像
cv::imshow("video", ff);
cv::imshow("foreground", foreground);
cv::imshow("background", background);
cvtColor(foreground, foregroundRGB , COLOR_GRAY2RGB);
outputVideo << foregroundRGB;
if (cv::waitKey(25) > 0)
{
break;
}
}
return 0;
}