Reading videos with openCV

I have a video engine2.avi that I want to read and show using openCV. Here is 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 does not work if there is a YUV 4:2:2 (UYVY) codec in my file (I recorded a video using Direct-Show), but it works when I use a video that I captured whit openCV !!

Any ideas how this might work?

UPDATE:

After reading some links suggesting that catching exception would solve the problem, I changed my code. This did not 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 to cap>>frame . I read similar questions, but they use the frame in YUV (4: 2: 0), and my video has UYVY (4: 2: 2). How can I convert this to a color RGB model?

UPDATE 2:

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

 #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; } 
+6
source share
3 answers

Here are some links that may help you:

Edit

I have to clarify something first: OpenCV is able to read YUV frames from a video file because it is the main library (FFmpeg / GStreamer) that does the job. OpenCV also supports conversion between a specific type of YUV and RGB through cvCvtColor() using CV_YCrCb2RGB or CV_RGBYCrCb .

After reviewing your question again, I noticed that you did not indicate the error that occurred. You could do a better job with the solution possible from the capture interface by typing a message on the screen instead of throw .

I tested the video file that you shared and I had no problems playing it in the 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 cannot open the file, it will immediately leave the application, instead of moving on just to crash in cap.read(frame) .

+6
source

If you just look at the video, here is the code that worked for me, Please check if this helps you.

 #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.

0
source

I realized that the object VideoCapture needs opencv_ffmpeg310_64.dll. After copying this library to your binary folder, your VideoCapture object should be able to read the video file.

-1
source

All Articles