OpenCV 2.4.9 hangs when reading frames from file [closed]

asked 2015-03-24 17:52:47 -0600

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;
}
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by sturkmen
close date 2020-10-05 09:21:03.384411

Comments

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 with stream >> frame; it is an alternative way to do the same action, and check if you still have the issue.

theodore gravatar imagetheodore ( 2015-03-25 08:22:23 -0600 )edit

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

aosmith gravatar imageaosmith ( 2015-03-25 11:17:30 -0600 )edit

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.

theodore gravatar imagetheodore ( 2015-03-25 12:14:36 -0600 )edit

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.

aosmith gravatar imageaosmith ( 2015-03-25 12:19:02 -0600 )edit

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.

theodore gravatar imagetheodore ( 2015-03-25 12:32:36 -0600 )edit

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.

aosmith gravatar imageaosmith ( 2015-03-25 23:12:03 -0600 )edit