0
votes

I'm new to OpenCV and trying to run a simple program which reads a .avi file and display it frame by frame in a window. My system is OSX El Capitan. Here's my code:

#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"

#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;

int main( int argc, const char** argv )
{
    Mat frame;

    VideoCapture cap("crs01.avi");

    if(!cap.isOpened()){  // check if we succeeded
        cout << "Video not loaded!!" << endl;
        return -1;
    } else {
        cout << "Loaded!!" << endl;
    }

    int count = cap.get(CV_CAP_PROP_FRAME_COUNT);
    for(int i = 0; i < count; i++){
//    while(true) {
        cap >> frame;
        if(!frame.empty()){
            imshow("edges", frame);
        } else {
            cout << "EMPTY" << endl;
        }
        if(waitKey(30) >= 0)
            break;
    }
    cap.release();

    return 0;
}

However, the program always returns me empty frames. It can print "Loaded" but after that it keeps printing "EMPTY". I've tried this program on the built-in camera, i.e. VideoCapture cap(0);, as well as reading the other .avi files, and it works fine in both cases. I also tried to run a debugger and it turns out the frame is indeed empty after the "cap >> frame" line.

Anyone can help me out of it? Does this problem have anything to do with the Mac system? Thanks in advance!

1

1 Answers

-1
votes

Here's another way to do it.

cv::VideoCapture mycap;
std::string fileName="PATH_TO_FILE//FILENAME";
mycap.open(fileName);       
int totalFrameNumber = (int)mycap.get(CV_CAP_PROP_FRAME_COUNT);
cv::Mat newImg;
for (int counter = 0; counter<totalFrameNumber; counter++){
    bool frameIsCorrect = mycap.read(newImg);
    if (new_Img.cols>0)
    {
        cv::imshow("frame", newImg);
        cv::waitKey(1); //or waitKey(0) to pause on every new frame for a key press
    }
}