-
Notifications
You must be signed in to change notification settings - Fork 0
/
Source.cpp
169 lines (133 loc) · 5.25 KB
/
Source.cpp
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include<opencv2/opencv.hpp>
#include <iostream>
#include <sstream>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/video.hpp>
#include <opencv2/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/background_segm.hpp>
#include <string>
#include <fstream>
using namespace std;
using namespace cv;
inline bool exists_test(const string& name) {
struct stat buffer;
return (stat(name.c_str(), &buffer) == 0);
}
// /////////////////////////////////////////////////////
// Procedure to save videofiles from images
// inputs:
// filename = output video filename
// videoOutputPath = path to save output videofile
// frameSize = width and height of images
// fps = frmaes per second on output video file
// dirPath = path to dir with images
void videoWriter(string filename, string videoOutputPath,
Size frameSize, double fps,
string dirPath)
{
VideoCapture inputVideo(dirPath);
Mat frame;
VideoWriter outputVideo;
int codec = VideoWriter::fourcc('M', 'P', 'E', 'G');
outputVideo.open(videoOutputPath + filename, codec, fps, frameSize, true);
if (!outputVideo.isOpened())
{
cout << "[ERROR] Could not open the output video for write: " << filename << endl;
return;
}
vector<String> fn;
glob(dirPath, fn, false);
size_t imgCount = fn.size();
for (size_t iCount = 0; iCount < imgCount; iCount++) {
cout << "Frame " + to_string(iCount) + " out of " + to_string(imgCount) << endl;
frame = imread(fn[iCount]);
Mat xframe;
resize(frame, xframe, frameSize);
outputVideo.write(xframe);
}
outputVideo.release();
return;
}
int main(int argc, char* argv[])
{
bool showFrames = true;
bool saveImages = false;
bool saveVideo = false;
string filename = "path\\to\\videoFile";
string savedir = "path\\to\\dir\\toSaveFiles";
string foregroundPath = "path\\to\\dir\\withForegroundImages";
string backgroundPath = "path\\to\\dir\\withBackgroundImages";
VideoCapture cap;
// Make simple frame and MOG2 mask
Mat frame, MOGMask, foregroundImg, backgroundImage, backgroundImg;
double learning_rate = 0.5;
if (exists_test(filename)) {
cap.open(filename);
}
// Init MOG2 BackgroundSubstractor
Ptr<BackgroundSubtractor> BackgroundSubstractor;
BackgroundSubstractor = createBackgroundSubtractorMOG2(false);
int iCount = 0;
while (true) {
iCount++;
cap >> frame;
if (frame.empty())
break;
// Update the background model
BackgroundSubstractor->apply(frame, MOGMask);
// Some works with noises on frame //
// Blur the foreground mask to reduce the effect of noise and false positives
// Remove the shadow parts and the noise
// To remove the shadow just threshold the foreground image with value 127 as minimum. 128? 254?
blur(MOGMask, MOGMask, Size(5, 5), Point(-1, -1));
threshold(MOGMask, MOGMask, 128, 255, 0);
// Get the frame number and write it on the current frame
rectangle(frame, Point(10, 2), Point(100, 20),
Scalar(255, 255, 255), -1);
stringstream ss;
ss << cap.get(CAP_PROP_POS_FRAMES);
string frameNumberString = ss.str();
putText(frame, frameNumberString.c_str(), Point(15, 15),
FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0));
// Zeros images
foregroundImg = Scalar::all(0);
backgroundImg = Scalar::all(0);
// Using mask to cut foreground
frame.copyTo(foregroundImg, MOGMask);
// Invert mask and cut background
backgroundImage = 255 - MOGMask;
frame.copyTo(backgroundImg, backgroundImage);
// Show input frame, mask, bg and fg frames
if (showFrames){
imshow("Frame", frame);
imshow("BackGround", backgroundImg);
imshow("ForeGround", foregroundImg);
imshow("MOG2 Mask", MOGMask);
}
// Save images
if (saveImages) {
if (iCount != 1) {
string counter = to_string(iCount);
imwrite(savedir + "/original/Frames" + counter + ".jpg", frame);
imwrite(savedir + "/background/Frames" + counter + ".jpg", backgroundImg);
imwrite(savedir + "/foreground/Frames" + counter + ".jpg", foregroundImg);
}
}
int keyboard = waitKey(30);
if (keyboard == 'q' || keyboard == 27)
break;
}
// Save videofiles from Images. U need to save frames first
if (saveVideo) {
Size frameSize = Size((int)cap.get(CAP_PROP_FRAME_WIDTH), (int)cap.get(CAP_PROP_FRAME_HEIGHT));
int fps = (double)cap.get(CAP_PROP_FRAME_COUNT);
videoWriter("outputVideoForeground.mpg4", foregroundPath, frameSize, fps, savedir + "/foreground/");
videoWriter("outputVideoBackground.mp4", backgroundPath, frameSize, fps, savedir + "/background/");
}
return 0;
}