Sabzo
Sabzo

Reputation: 31

Unable to open Video after creating and Saving it Using the C API on OpenCV 2.4.13.2 on MacOSX

I'm unable to open Video after creating and Saving it Using the C API on OpenCV 2.4.13.2 on MacOSX and I've looked at a nubmer of tutorials which are outdated since C++ API is now favoredfor OpenCV. I've spent two days on tutorials and In particular I'm trying to reproduce the example here http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/opencv-intro.html#SECTION00073000000000000000

I'm able to start the camera and begin recording and writing to a file "out.avi" however I'm unable to open it with VLC or Quicktime.

This is my entire program:

#include <stdio.h>
#include <cv.h>
#include "highgui.h"
#include "cxcore.h"

int main(int argc, char **argv) {
  int key = 0;
  int i = 0;
  CvCapture *capture = 0; //The camera
  IplImage* frame = 0; //The images you bring out of the camera

  capture = cvCaptureFromCAM(0); //Open the camera
  if (!capture ){
     printf("Could not connect to camera\n" ); return 1;
  }

  cvQueryFrame(capture);
  double fps = cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
  int frame_height = cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT);
  int frame_width = cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH);

  cvNamedWindow("win", CV_WINDOW_AUTOSIZE);
  cvMoveWindow("win", 100, 100);

  CvVideoWriter *writer = cvCreateVideoWriter("out.avi", CV_FOURCC('D', 'I', 'V', 'X'), fps, cvSize(frame_width, frame_height), 1);
  if (!writer) {
    printf("CvVideoWriter failed\n");
    exit(1);
  }

  IplImage *img = 0;
  int nframes = 1000;

  for (i = 0; i < nframes; i++) {
    if (!cvGrabFrame(capture)) {
      printf("Unable to grab frame!\n");
      exit(0);
    }
    img = cvRetrieveFrame(capture, 0);
    cvWriteFrame(writer, img);
    cvShowImage("win", img);
    key = cvWaitKey(20);
  }

  return 0;
}

What am I doing wrong? Thanks in advance.

Upvotes: 1

Views: 90

Answers (1)

Sabzo
Sabzo

Reputation: 31

Error was being caused by an Unhandled Objective C exception in the C++ library. I documented the error on this OpenCV ticket https://github.com/opencv/opencv/issues/8305 and helped submit a pull request. Checking out the latest version of OpenCV 2.4 branch now addresses the issue above.

Upvotes: 1

Related Questions