Question

Could someone explain to me why OpenCV fails to open an .avi file with C++ API, but instead it opens it with C API ?

Here are the two code snippets:

C++:

Mat source;
VideoCapture stream("Video.avi");

if (!stream.isOpened()){
    std::cout << "Stream cannot be opened" << std::endl;
    return -1;
}
while(1)

    if(!stream.read(source)) {
        std::cout << "Error reading video frame" << endl;
    }

imshow("Source", source);

This fails to open Video.avi, and "Error reading video frame" is printed time after time. ffmpeg.dll is in the path and I have also installed ffdshow.

C:

CvCapture* stream = cvCreateFileCapture( "Video.avi" );
IplImage* source;
while(1) {
    source = cvQueryFrame( stream );
    if( !source ) printf("\n Problem");
    Mat src(source);
    imshow("source", src);
    if(waitKey(1) >= 0) break;
}

This opens Video.avi with no problem.

Thanks for your help !

PS. Maybe it's worth mentioning that Video.avi itself was created using OpenCV.

Was it helpful?

Solution

Your C and C++ code are not equal. This code will work:

int main( int argc, char** argv )
{
    Mat source;
    VideoCapture stream("video.avi");

    if (!stream.isOpened())
    {
        std::cout << "Stream cannot be opened" << std::endl;
        return -1;
    }

    while(1)
    {
        stream >> source;
        if(source.empty()) 
        {
            std::cout << "Error reading video frame" << endl;
        }
        imshow("Source", source);
        waitKey(20);
    }
    stream.release();
    getchar();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top