Skip to content

Commit

Permalink
v1.0
Browse files Browse the repository at this point in the history
- Now using blur, instead GaussianBlur. - Changed threshold.
- Background Sustractor now don't using dynamicsCast.
- Shadows now don't detected as foreground part.
  • Loading branch information
trylogout committed Aug 20, 2020
1 parent 643e9d1 commit dce7b71
Showing 1 changed file with 43 additions and 33 deletions.
76 changes: 43 additions & 33 deletions Source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ using namespace cv;


inline bool exists_test(const string& name) {
struct stat buffer;
return (stat(name.c_str(), &buffer) == 0);
struct stat buffer;
return (stat(name.c_str(), &buffer) == 0);
}

// /////////////////////////////////////////////////////
Expand All @@ -30,9 +30,10 @@ inline bool exists_test(const string& name) {
// 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)

void videoWriter(string filename, string videoOutputPath,
Size frameSize, double fps,
string dirPath)
{
VideoCapture inputVideo(dirPath);
Mat frame;
Expand All @@ -51,7 +52,7 @@ void videoWriter(string filename,string videoOutputPath,
glob(dirPath, fn, false);

size_t imgCount = fn.size();
for (size_t iCount = 0; iCount < imgCount; iCount++){
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;
Expand All @@ -65,27 +66,29 @@ void videoWriter(string filename,string videoOutputPath,

int main(int argc, char* argv[])
{
bool showFrames = false;
bool showFrames = true;
bool saveImages = false;
bool saveVideo = true;
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;
VideoCapture cap;

// make simple frame and MOG2 mask
// 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

// Init MOG2 BackgroundSubstractor
Ptr<BackgroundSubtractor> BackgroundSubstractor;
BackgroundSubstractor = createBackgroundSubtractorMOG2().dynamicCast<BackgroundSubtractor>();;
BackgroundSubstractor = createBackgroundSubtractorMOG2(false);

int iCount = 0;

Expand All @@ -96,64 +99,71 @@ int main(int argc, char* argv[])
if (frame.empty())
break;

// update the background model
// Update the background model
BackgroundSubstractor->apply(frame, MOGMask);

// some works with noises on frame
GaussianBlur(MOGMask, MOGMask, Size(11, 11), 3.5, 3.5);
threshold(MOGMask, MOGMask, 10, 255, THRESH_BINARY);
// Some works with noises on frame //
// Blur the foreground mask to reduce the effect of noise and false positives

// get the frame number and write it on the current frame
// 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),
cv::Scalar(255, 255, 255), -1);
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, cv::Scalar(0, 0, 0));
FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0));

// zeros images
// Zeros images
foregroundImg = Scalar::all(0);
backgroundImg = Scalar::all(0);

// using mask to cut foreground
// Using mask to cut foreground
frame.copyTo(foregroundImg, MOGMask);

// invert mask and cut background
// 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);
}

if (saveImages){
if (i_count != 1) {
string counter = std::to_string(iCount);
// 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);
}
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;
}

if (saveVideos){
// 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;
return 0;
}

0 comments on commit dce7b71

Please sign in to comment.