Reading a video with openCV
Asked Answered
F

3

9

I have a video engine2.avi that I want to read and show with openCV. Here's my code:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;

int main(int argc, char** argv)
{
    string filename = "D:\\BMDvideos\\engine2.avi";
    VideoCapture capture(filename);
    Mat frame;

    if( !capture.isOpened() )
        throw "Error when reading steam_avi";

    namedWindow("w", 1);
    for( ; ; )
    {
        capture >> frame;
        //if(!frame)
        //    break;
        imshow("w", frame);
        waitKey(20); // waits to display frame
    }
    waitKey(0);
}

This code doesn't work if my file has the codec YUV 4:2:2 (UYVY) (I recorded the video using Direct-Show), but works when I use a video I grabbed whit openCV !!

Has anybody an Idea how this could work ?

UPDATE:

After reading some links, suggesting that catching exception will solve the problem, I modified my code. It didn't help, but here is the modified code:

cv::VideoCapture cap("d:\\BMDvideos\\engine2.avi");
cv::Mat frame;

try
{
    cap >> frame;
}
catch(cv::Exception ex)
{
    std::cout << ex.what() << std::endl;
}
catch(...)
{
    std::cout << "Unknown exception" << std::endl;  
}

The program crashes in cap>>frame. I readed similar questions but they use a frame in YUV (4:2:0), while my video has UYVY (4:2:2). How can I convert this into RGB color model?

UPDATE 2:

After karlphillip's suggestion, I used OpenCV2.4.3, but I still got the same error using the code below:

#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\opencv.hpp>
using namespace cv;
using namespace std;

int main(){
    cv::Mat frame;
    cv::VideoCapture cap("d:\\BMDvideos\\B\\Aufnahme.avi");
    if(!cap.isOpened())
    {
        cout << "Error can't find the file"<<endl;
    }

    while(1){
        if(!cap.read(frame))
            imshow("",frame);

        cv::waitKey(33);
    }
    return 0;
}
Frederickson answered 12/2, 2013 at 13:22 Comment(0)
G
12

Here is a couple of links that might help you:

Edit:

I must clear something first: OpenCV is capable of reading YUV frames from a video file because it's the underlying library (FFmpeg/GStreamer) that does the job. OpenCV also supports converting between a specific type of YUV and RGB through cvCvtColor() with CV_YCrCb2RGB or CV_RGBYCrCb.

Upon examining your question again, I noticed you didn't specify the kind of error that happened. You could do a better job at dealing with a possible failure from the capture interface by printing a message to the screen instead of throwing it.

I tested the video file you shared and I had no problems playing it on a window using the following code:

#include <cv.h>
#include <highgui.h>

#include <iostream>

int main(int argc, char* argv[])
{
    cv::VideoCapture cap(argv[1]);
    if (!cap.isOpened())
    {
        std::cout << "!!! Failed to open file: " << argv[1] << std::endl;
        return -1;
    }

    cv::Mat frame;
    for(;;)
    {

        if (!cap.read(frame))             
            break;

        cv::imshow("window", frame);

        char key = cvWaitKey(10);
        if (key == 27) // ESC
            break;
    }

    return 0;
}

If, for some reason, the capture interface fails to open the file it will quit the application immediately, instead of going further just to crash at cap.read(frame).

Grussing answered 12/2, 2013 at 18:12 Comment(10)
thanks @karlphilip for your answer. But in those links they're explain how to convert a frame that's in YUV 4:2:2 to RGB, I have a video when I try to get a frame from it the whole program crashs !!Frederickson
Upload the video somewhere and let us test it.Grussing
Don't you have smaller videos laying around? I don't have time to download 1GB. :(Grussing
well it's will be just 2sec because the input device gives a HD 1900x1080 and that's a row data so I can change it ????Frederickson
can I convert it using ffmpeg and than opening it with openCV? without losing data ?Frederickson
Updated answer. Consider up voting it if it helped you, or even clicking on the checkbox to select it as the official answer to your question. By doing these things you are helping future visitors like yourself.Grussing
well with your answer I understand my problem better that's why I did't, I thought it'S smt good for you ! and I already tested the function read() and it didn't help , thanks anyway for helpFrederickson
If you are not using OpenCV 2.4.3 I suggest you upgrade.Grussing
@karlphilip, I just want to say the upgrade to openCV2.4.3 didn't change anything I still get the same error when Itry itFrederickson
Consider editing the question to insert the full code of a short, compilable example that reproduces the problem you are facing. With my code on my machine it doesnt happen.Grussing
T
0

If you are just looking out for displaying the video, here's the code that worked for me, Please check if it helps you in any way.

#include <stdio.h>
#include <opencv2/opencv.hpp>
int main(){
CvCapture *camera=cvCaptureFromFile("C:\\test.avi");
if (camera==NULL)
    printf("camera is null\n");
else
    printf("camera is not null");

cvNamedWindow("img");
while (cvWaitKey(10)!=atoi("q")){
    double t1=(double)cvGetTickCount();
    IplImage *img=cvQueryFrame(camera);
    /*if(img){
        cvSaveImage("C:/opencv.jpg",img);
    }*/
    double t2=(double)cvGetTickCount();
    printf("time: %gms  fps: %.2g\n",(t2-t1)/(cvGetTickFrequency()*1000.), 1000./((t2-t1)/(cvGetTickFrequency()*1000.)));
    cvShowImage("img",img);
}
cvReleaseCapture(&camera);
}

Hope this helps you.

Tenner answered 18/12, 2013 at 10:27 Comment(2)
this code just read a video that was my problem, the codec was the issue! but thanks anyway !Frederickson
If the issue was with the codec and if you are still facing problem, make sure you install ffmpeg codec. Check this link for installation of ffmpeg.Tenner
B
0

I realised that, the VideoCapture object needs opencv_ffmpeg310_64.dll. Once you copy this dll in your binary folder your VideoCapture object should be able to read video file.

Basifixed answered 10/3, 2018 at 17:58 Comment(1)
That's a quick fix! Thank you so much!! This problem has haunted me for two days!!Stour

© 2022 - 2024 — McMap. All rights reserved.