Ask Your Question

Error reading video using VideoCapture while applying MOG

asked 2014-05-05 13:28:00 -0500

Suramrit gravatar image

I am trying to apply MOG background subtraction to an acquired video, stored in .avi format. I have taken the link at here but have changed it.

Im using the CaptureVideo as:

        #include "stdafx.h"
        #include <cv.h>
        #include <cxcore.h>
        #include <highgui.h>
        #include <cvaux.h>

        #include <opencv2/video/background_segm.hpp>
        #include <stdio.h>
        #include <opencv2/video/video.hpp>
        using namespace cv;
        using namespace std;
        Mat frame; //current frame
        Ptr<BackgroundSubtractor> pMOG; //MOG Background subtractor
        void processImages(char* firstFrameFilename);
        void processVideo(string name);//(char* videoFilename);

        string path = "C:\\Users\Suramrit Singh\Documents\Visual Studio 2010\Projects\opencvtest\opencvtest\walk2.avi"
                int main()
            namedWindow("FG Mask MOG");
            pMOG= new BackgroundSubtractorMOG(); //MOG approach
            //processImages("asd1.jpg");--- cant work on single images


            return EXIT_SUCCESS;
        void processVideo(string name){//(char* videoFilename) {
            //create the capture object
            VideoCapture capture(path);

            for(int i=0;i<6000;i++)
            capture >> frame;

            pMOG->operator()(frame, fgMaskMOG); // -----------------creatingproblem----------------------

            IplImage* image2=cvCloneImage(&(IplImage)frame);
            printf("%d", i);
            imshow("FG Mask MOG", fgMaskMOG);
            keyboard = waitKey(1);
           //delete capture object

Now if I change it to default cam VideoCapture capture(0), it works, but does not do so with the .avi file path provided and gives the following runtime error:

"Unhandled exception at 0x000007fd7835811c in opencvtest.exe: Microsoft C++ exception: cv::Exception at memory location 0x0081eb60.."

Please, I'm a newcomer to image processing and any help will be highly appreciated as I've been stuck at this problem for a few days now and can't seem to figure it out.

edit retag flag offensive close merge delete


what do you want with that IplImage there ? throw it out ! (use frame.clone() if that's what you wanted)

also your path contains single backslashes. always a bad idea. replace them with either a '/' or with '\\'

berak gravatar imageberak ( 2014-05-05 13:44:34 -0500 )edit

Sorry about that, it was for something I was trying earlier! Ive tried both using '/' and a '\', still get the same error.

Suramrit gravatar imageSuramrit ( 2014-05-05 13:52:59 -0500 )edit

you want either

capture >> frame;


not both. also, if the video is finished, you get empty frames from the capture, so throw in a :

 if ( frame.empty() )
       break; // THE END

and again, please kill that line with the useless(and wrong) IplImage

berak gravatar imageberak ( 2014-05-05 13:58:57 -0500 )edit

Yes, it seems I am getting empty frames from the capture even in the first iteration. Why would this be happening? Already removed the useless line

Suramrit gravatar imageSuramrit ( 2014-05-05 14:03:45 -0500 )edit

Also, I seem to be getting: Bad flag (Unrecognized or unsupported array type)

Suramrit gravatar imageSuramrit ( 2014-05-05 14:44:11 -0500 )edit

another way telling you it's empty/invalid (that's probably from imshow())

berak gravatar imageberak ( 2014-05-05 14:55:45 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2014-05-06 15:08:27 -0500

jwatte gravatar image

Single backslashes will be parsed by the compiler as character escapes (similar to "\n") and will be thrown away. You need to make EVERY backslash be a double backslash for the string that the compiler sees after parsing to make up a valid file path.

edit flag offensive delete link more


I find it quite dumb to give an answer which is exactly as a comment made by @berak, which brings no extra information... Try to avoid this...

StevenPuttemans gravatar imageStevenPuttemans ( 2014-05-07 04:36:04 -0500 )edit

The comment didn't explain why this was needed, and the requester didn't take the advice the first time, as can be seen in the code where only the first backslash is changed. Explaining "why" is the exact difference between a "comment" and an "answer."

jwatte gravatar imagejwatte ( 2014-05-07 16:25:03 -0500 )edit

Question Tools


Asked: 2014-05-05 13:28:00 -0500

Seen: 601 times

Last updated: May 06 '14