Ask Your Question

Revision history [back]

Background Subtraction from Video

I had ran a coding for background subtraction from the video for offline. I used openCV 2.4.9 and Visual Studio 2013 to run the coding. The video can display and play but the problem is the coding for background subtraction does not functioning. Can anybody help me what is the wrong for my coding? Someone had tell me the error is

if (strcmp(argv[1], "-vid") == 0)
    {
        //input data coming from a video
        processVideo(argv[2]);
    }

so, what i need to do?

// BackgroundSubtraction_Success.cpp : Defines the entry point for the console application.
//

#include <stdio.h>
#include "stdafx.h"
#include <iostream>
#include <opencv2\core\core.hpp>
#include <opencv2\flann\flann.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2\photo\photo.hpp>
#include <opencv2\video\video.hpp>
#include <opencv2\features2d\features2d.hpp>
#include <opencv2\objdetect\objdetect.hpp>
#include <opencv2\calib3d\calib3d.hpp>
#include <opencv2\ml\ml.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\contrib\contrib.hpp>
//#include "opencv\cv.h"
//#include <opencv2\core\core_c.h>
//#include <opencv2\highgui\highgui_c.h>
//#include <opencv2\imgproc\imgproc_c.h>


using namespace cv;
using namespace std;

//global variables
Mat frame; //current frame
Mat fgMaskMOG; //fg mask generated by MOG method
Ptr <BackgroundSubtractorMOG> pMOG; //MOG Background Subtractor

int keyboard;

//function declarations
void help();
void processVideo(char*Background);

void help()
{
    cout
        << "----------------------------------------" << endl
        << endl
        << "This program begins with Motion Detection" << endl
        << "using Background Subtraction" << endl
        << endl
        << "------------------------------------------" << endl;

}

int main(int argc, char*argv[])
{
    VideoCapture cap("C:/Users/user/Documents/Visual Studio 2013/Projects/cobaan/NewOpenCV_Success/sample1.avi"); // open the video file for reading

    if (!cap.isOpened())  // if not success, exit program
    {
        cout << "Cannot open the video file" << endl;
        return -1;
    }

    //cap.set(CV_CAP_PROP_POS_MSEC, 300); //start the video at 300ms

    double fps = cap.get(CV_CAP_PROP_FPS); //get the frames per seconds of the video

    cout << "Frame per seconds : " << fps << endl;

    namedWindow("MyVideo", CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"

    while (1)
    {
        Mat frame;

        bool bSuccess = cap.read(frame); // read a new frame from video

        if (!bSuccess) //if not success, break loop
        {
            cout << "Cannot read the frame from video file" << endl;
            break;
        }

        imshow("MyVideo", frame); //show the frame in "MyVideo" window

        if (waitKey(30) == 27) //wait for 'esc' key press for 30 ms. If 'esc' key is pressed, break loop
        {
            cout << "esc key is pressed by user" << endl;
            break;
        }
    }
    //print help information
    help();
    //check for the input parameter correctness
    if (argc != 3)
    {
        cerr << "incorrect input list" << endl;
        cerr << "exiting..." << endl;
        return EXIT_FAILURE;
    }

    //create GUI windows
    namedWindow("Frame");
    namedWindow("FG Mask MOG");

    //call the constructor
    BackgroundSubtractorMOG bgmog;
    bgmog(frame, fgMaskMOG);

    //create background subtractor objects
    //pMOG = createBackgroundSubtractorMOG(); //MOG approach

    if (strcmp(argv[1], "-vid") == 0)
    {
        //input data coming from a video
        processVideo(argv[2]);
    }
    else
    {
        //error in reading input parameter
        cerr << "Please check the input parameters." << endl;
        cerr << "Exiting..." << endl;
        return EXIT_FAILURE;
    }

    //destroy GUI windows
    destroyAllWindows();
    return EXIT_SUCCESS;
}

//call video
void processVideo(char* MyVideo)
{

    //create the capture object
    VideoCapture capture(MyVideo);
    if (!capture.isOpened())
    {
        //error in opening the video input
        cerr << "Unable to open video file: " <<MyVideo << endl;
        exit(EXIT_FAILURE);
    }

    //read input data ESC or 'q' for quitting
    while ((char)keyboard != 'q' && (char)keyboard != 27)
    {
        //read the current frame
        if (!capture.read(frame))
        {
            cerr << "Unable to read next frame" << endl;
            cerr << "Exiting..." << endl;
            exit(EXIT_FAILURE);
        }

        //update the background model
        //pMOG->apply(frame, fgMaskMOG);

        //get the frame number and write it on the current frame
        stringstream ss;
        rectangle(frame, cv::Point(10, 2), cv::Point(100, 20), cv::Scalar(255, 255, 255), -1);
        ss << capture.get(CV_CAP_PROP_POS_FRAMES);
        string frameNumberString = ss.str();
        putText(frame, frameNumberString.c_str(), cv::Point(15, 15), FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));

        //show the current frame and fg masks
        imshow("Frame", frame);
        imshow("FG Mask MOG", fgMaskMOG);

        //get input from the keyboard
        keyboard = waitKey(30);
    }
    //delete capture object
    capture.release();
}

ERROR: Background Subtraction from Video

I had ran a coding for background subtraction from the video for offline. I used openCV 2.4.9 and Visual Studio 2013 to run the coding. The video can display and play but the problem is the coding for background subtraction does not functioning. Can anybody help me what is the wrong for my coding? Someone had tell me the error is

if (strcmp(argv[1], "-vid") == 0)
    {
        //input data coming from a video
        processVideo(argv[2]);
    }

so, what i need to do?

// BackgroundSubtraction_Success.cpp : Defines the entry point for the console application.
//

#include <stdio.h>
#include "stdafx.h"
#include <iostream>
#include <opencv2\core\core.hpp>
#include <opencv2\flann\flann.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2\photo\photo.hpp>
#include <opencv2\video\video.hpp>
#include <opencv2\features2d\features2d.hpp>
#include <opencv2\objdetect\objdetect.hpp>
#include <opencv2\calib3d\calib3d.hpp>
#include <opencv2\ml\ml.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\contrib\contrib.hpp>
//#include "opencv\cv.h"
//#include <opencv2\core\core_c.h>
//#include <opencv2\highgui\highgui_c.h>
//#include <opencv2\imgproc\imgproc_c.h>


using namespace cv;
using namespace std;

//global variables
Mat frame; //current frame
Mat fgMaskMOG; //fg mask generated by MOG method
Ptr <BackgroundSubtractorMOG> pMOG; //MOG Background Subtractor

int keyboard;

//function declarations
void help();
void processVideo(char*Background);

void help()
{
    cout
        << "----------------------------------------" << endl
        << endl
        << "This program begins with Motion Detection" << endl
        << "using Background Subtraction" << endl
        << endl
        << "------------------------------------------" << endl;

}

int main(int argc, char*argv[])
{
    VideoCapture cap("C:/Users/user/Documents/Visual Studio 2013/Projects/cobaan/NewOpenCV_Success/sample1.avi"); // open the video file for reading

    if (!cap.isOpened())  // if not success, exit program
    {
        cout << "Cannot open the video file" << endl;
        return -1;
    }

    //cap.set(CV_CAP_PROP_POS_MSEC, 300); //start the video at 300ms

    double fps = cap.get(CV_CAP_PROP_FPS); //get the frames per seconds of the video

    cout << "Frame per seconds : " << fps << endl;

    namedWindow("MyVideo", CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"

    while (1)
    {
        Mat frame;

        bool bSuccess = cap.read(frame); // read a new frame from video

        if (!bSuccess) //if not success, break loop
        {
            cout << "Cannot read the frame from video file" << endl;
            break;
        }

        imshow("MyVideo", frame); //show the frame in "MyVideo" window

        if (waitKey(30) == 27) //wait for 'esc' key press for 30 ms. If 'esc' key is pressed, break loop
        {
            cout << "esc key is pressed by user" << endl;
            break;
        }
    }
    //print help information
    help();
    //check for the input parameter correctness
    if (argc != 3)
    {
        cerr << "incorrect input list" << endl;
        cerr << "exiting..." << endl;
        return EXIT_FAILURE;
    }

    //create GUI windows
    namedWindow("Frame");
    namedWindow("FG Mask MOG");

    //call the constructor
    BackgroundSubtractorMOG bgmog;
    bgmog(frame, fgMaskMOG);

    //create background subtractor objects
    //pMOG = createBackgroundSubtractorMOG(); //MOG approach

    if (strcmp(argv[1], "-vid") == 0)
    {
        //input data coming from a video
        processVideo(argv[2]);
    }
    else
    {
        //error in reading input parameter
        cerr << "Please check the input parameters." << endl;
        cerr << "Exiting..." << endl;
        return EXIT_FAILURE;
    }

    //destroy GUI windows
    destroyAllWindows();
    return EXIT_SUCCESS;
}

//call video
void processVideo(char* MyVideo)
{

    //create the capture object
    VideoCapture capture(MyVideo);
    if (!capture.isOpened())
    {
        //error in opening the video input
        cerr << "Unable to open video file: " <<MyVideo << endl;
        exit(EXIT_FAILURE);
    }

    //read input data ESC or 'q' for quitting
    while ((char)keyboard != 'q' && (char)keyboard != 27)
    {
        //read the current frame
        if (!capture.read(frame))
        {
            cerr << "Unable to read next frame" << endl;
            cerr << "Exiting..." << endl;
            exit(EXIT_FAILURE);
        }

        //update the background model
        //pMOG->apply(frame, fgMaskMOG);

        //get the frame number and write it on the current frame
        stringstream ss;
        rectangle(frame, cv::Point(10, 2), cv::Point(100, 20), cv::Scalar(255, 255, 255), -1);
        ss << capture.get(CV_CAP_PROP_POS_FRAMES);
        string frameNumberString = ss.str();
        putText(frame, frameNumberString.c_str(), cv::Point(15, 15), FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));

        //show the current frame and fg masks
        imshow("Frame", frame);
        imshow("FG Mask MOG", fgMaskMOG);

        //get input from the keyboard
        keyboard = waitKey(30);
    }
    //delete capture object
    capture.release();
}

ERROR: Background Subtraction from Video

image description I had ran a coding for background subtraction from the video for offline. I used openCV 2.4.9 and Visual Studio 2013 to run the coding. The video can display and play but the problem is the coding for background subtraction does not functioning. Can anybody help me what is the wrong for my coding? Someone had tell me the error is

if (strcmp(argv[1], "-vid") == 0)
    {
        //input data coming from a video
        processVideo(argv[2]);
    }

so, what i need to do?

// BackgroundSubtraction_Success.cpp : Defines the entry point for the console application.
//

#include <stdio.h>
#include "stdafx.h"
#include <iostream>
#include <opencv2\core\core.hpp>
#include <opencv2\flann\flann.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2\photo\photo.hpp>
#include <opencv2\video\video.hpp>
#include <opencv2\features2d\features2d.hpp>
#include <opencv2\objdetect\objdetect.hpp>
#include <opencv2\calib3d\calib3d.hpp>
#include <opencv2\ml\ml.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\contrib\contrib.hpp>
//#include "opencv\cv.h"
//#include <opencv2\core\core_c.h>
//#include <opencv2\highgui\highgui_c.h>
//#include <opencv2\imgproc\imgproc_c.h>


using namespace cv;
using namespace std;

//global variables
Mat frame; //current frame
Mat fgMaskMOG; //fg mask generated by MOG method
Ptr <BackgroundSubtractorMOG> pMOG; //MOG Background Subtractor

int keyboard;

//function declarations
void help();
void processVideo(char*Background);

void help()
{
    cout
        << "----------------------------------------" << endl
        << endl
        << "This program begins with Motion Detection" << endl
        << "using Background Subtraction" << endl
        << endl
        << "------------------------------------------" << endl;

}

int main(int argc, char*argv[])
{
    VideoCapture cap("C:/Users/user/Documents/Visual Studio 2013/Projects/cobaan/NewOpenCV_Success/sample1.avi"); // open the video file for reading

    if (!cap.isOpened())  // if not success, exit program
    {
        cout << "Cannot open the video file" << endl;
        return -1;
    }

    //cap.set(CV_CAP_PROP_POS_MSEC, 300); //start the video at 300ms

    double fps = cap.get(CV_CAP_PROP_FPS); //get the frames per seconds of the video

    cout << "Frame per seconds : " << fps << endl;

    namedWindow("MyVideo", CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"

    while (1)
    {
        Mat frame;

        bool bSuccess = cap.read(frame); // read a new frame from video

        if (!bSuccess) //if not success, break loop
        {
            cout << "Cannot read the frame from video file" << endl;
            break;
        }

        imshow("MyVideo", frame); //show the frame in "MyVideo" window

        if (waitKey(30) == 27) //wait for 'esc' key press for 30 ms. If 'esc' key is pressed, break loop
        {
            cout << "esc key is pressed by user" << endl;
            break;
        }
    }
    //print help information
    help();
    //check for the input parameter correctness
    if (argc != 3)
    {
        cerr << "incorrect input list" << endl;
        cerr << "exiting..." << endl;
        return EXIT_FAILURE;
    }

    //create GUI windows
    namedWindow("Frame");
    namedWindow("FG Mask MOG");

    //call the constructor
    BackgroundSubtractorMOG bgmog;
    bgmog(frame, fgMaskMOG);

    //create background subtractor objects
    //pMOG = createBackgroundSubtractorMOG(); //MOG approach

    if (strcmp(argv[1], "-vid") == 0)
    {
        //input data coming from a video
        processVideo(argv[2]);
    }
    else
    {
        //error in reading input parameter
        cerr << "Please check the input parameters." << endl;
        cerr << "Exiting..." << endl;
        return EXIT_FAILURE;
    }

    //destroy GUI windows
    destroyAllWindows();
    return EXIT_SUCCESS;
}

//call video
void processVideo(char* MyVideo)
{

    //create the capture object
    VideoCapture capture(MyVideo);
    if (!capture.isOpened())
    {
        //error in opening the video input
        cerr << "Unable to open video file: " <<MyVideo << endl;
        exit(EXIT_FAILURE);
    }

    //read input data ESC or 'q' for quitting
    while ((char)keyboard != 'q' && (char)keyboard != 27)
    {
        //read the current frame
        if (!capture.read(frame))
        {
            cerr << "Unable to read next frame" << endl;
            cerr << "Exiting..." << endl;
            exit(EXIT_FAILURE);
        }

        //update the background model
        //pMOG->apply(frame, fgMaskMOG);

        //get the frame number and write it on the current frame
        stringstream ss;
        rectangle(frame, cv::Point(10, 2), cv::Point(100, 20), cv::Scalar(255, 255, 255), -1);
        ss << capture.get(CV_CAP_PROP_POS_FRAMES);
        string frameNumberString = ss.str();
        putText(frame, frameNumberString.c_str(), cv::Point(15, 15), FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));

        //show the current frame and fg masks
        imshow("Frame", frame);
        imshow("FG Mask MOG", fgMaskMOG);

        //get input from the keyboard
        keyboard = waitKey(30);
    }
    //delete capture object
    capture.release();
}