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; }
source share