OpenCV 2.4.9 hangs when reading frames from file [closed]
I have some pretty basic code, it works fine when using a webcam (input 0) but seems to hang when trying to read frames from a video file as opposed to a webcam, any ideas?
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
int main(int argc, char *argv[]) {
VideoCapture stream(argv[1]);
if (!stream.isOpened()) {
cout << "cannot open file";
}
while (true) {
Mat frame;
stream.read(frame);
imshow("preview", frame);
if (waitKey(30) >= 0)
break;
}
return 0;
}
can you provide some more info, regarding the video file. For example,, what type, resolution, fps, etc... moreover, which version of Opencv are you using. Your code seems ok, by the way what do you mean by hangs? does it freeze/exit or you notice some lag. Also, just out of curiosity try to replace the
stream.read(frame);
line withstream >> frame;
it is an alternative way to do the same action, and check if you still have the issue.I'm on opencv 2.4.9. I just tried the >> operator and it doesn't seem to make a difference. By hanging I mean it stops executing, and does not return (eg off stuck in a loop somewhere). I've tried a few video files, an avi, the opencv tree.avi example file and an m2ts. I'm compiling with:
g++ pkg-config --libs opencv
I would suggest to switch to the latest stable version which is the 2.4.11, and check again because I cannot see any mistake to your code. It should be working without any problem.
I'll give that a try. My sinking suspicion is that this is something broken in gstreamer. That might explain why webcam + images work just fine but videos don't.
yup that could be the case, it could also be related to the ffmpeg package. But for now try to switch to newest version in order to narrow down the possible causes.
Seems like this is ffpmeg or gstreamer. Upgrading version didn't help and I got my code working in a vm. For the record fedora opencv packages are broken in relation to gstreamer and/or ffmpeg.