ERROR: Background Subtraction from Video

asked 2015-03-25 01:50:29 -0600

CubbyBluePen gravatar image

updated 2015-03-25 03:12:05 -0600

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 ...
(more)
edit retag flag offensive close merge delete

Comments

"background subtraction does not functioning." - now, what is the error ?

berak gravatar imageberak ( 2015-03-25 02:59:32 -0600 )edit

it dont has any error but it only display and play the video..coding for background subtraction doesnt take place..

CubbyBluePen gravatar imageCubbyBluePen ( 2015-03-25 03:09:50 -0600 )edit

you can't read your own code ;) ?

berak gravatar imageberak ( 2015-03-25 03:13:51 -0600 )edit

actually i had copy the coding and change it a little bit and i am the beginner in using opencv.. anything wrong in my coding?

CubbyBluePen gravatar imageCubbyBluePen ( 2015-03-25 03:18:30 -0600 )edit

it exits with "incorrect input list" - meaning, you did not supply the nessecary arguments on the cmdline.

berak gravatar imageberak ( 2015-03-25 03:28:41 -0600 )edit

then, what i need to do?

CubbyBluePen gravatar imageCubbyBluePen ( 2015-03-25 03:43:48 -0600 )edit