Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading mp4 (Go Pro) video using OpenCV

Tags:

c++

opencv

video

I am having difficulty reading in certain video files when using OpenCV with the C++ interface in Visual Studio 2013. I have been able to read in other video formats so believe my code is okay.

The problem video files have been taken using a Go Pro and are mp4. I can play them on the same machine I am using OpenCV on using classic media player. I have used MediaInfo to gather information on the video file:

Format : MPEG-4 Format profile : JVT Codec ID : avc1 File size : 126 MiB

I have tried providing fourcc codes explicitly using the set function of OpenCV and providing divx as the code and avc1 but with no luck.

My program code is as follows:

int main(int argc, char** argv) {
    if (argc != 2) {
        help(argv);
        system("Pause");
        return 1;
    }

    std::string arg = argv[1];
    VideoCapture capture;
    capture.set(CV_CAP_PROP_FOURCC, CV_FOURCC('A', 'V', 'C', '1'));
    capture.open(arg);
    if (!capture.isOpened()) {
        cerr << "Failed to open video file!\n" << endl;
        help(argv);
        system("Pause");
        return 1;
    }
    return process(capture);
}

   void help(char** argv) {
        cout << "\n TBC" << endl;
    }

    int process(VideoCapture& src) {
        Scalar const LOW_HSV_THRESHOLD(120, 45, 75);
        Scalar const HIGH_HSV_THRESHOLD(140, 55, 85);
        string windowName[] = { "Original", "HSV", "In Range", "Binary"};
        namedWindow(windowName[0], CV_WINDOW_KEEPRATIO); 
        namedWindow(windowName[1], CV_WINDOW_KEEPRATIO); 
        namedWindow(windowName[2], CV_WINDOW_KEEPRATIO);
        namedWindow(windowName[3], CV_WINDOW_KEEPRATIO);
        Mat matOriginal;
        Mat matHsv;
        Mat matInRange;
        Mat dst;
        src >> matOriginal;

        cvtColor(matOriginal, matHsv, CV_BGR2HSV);
        GaussianBlur(matHsv, matHsv, Size(5, 5), 1.5);

        cvtColor(matOriginal, matInRange, CV_BGR2GRAY);
        threshold(matInRange, dst, 100, 255, THRESH_BINARY);
        //inRange(matHsv, LOW_HSV_THRESHOLD, HIGH_HSV_THRESHOLD, matInRange);
        imshow(windowName[0], matOriginal);
        imshow(windowName[1], matHsv);
        imshow(windowName[2], matInRange);
        imshow(windowName[3], dst);

        waitKey(0);
        return 0;
    }

}

Any help would be hugely appreciated.

Many Thanks,

Kevin

like image 923
kev3kev3 Avatar asked Mar 15 '14 10:03

kev3kev3


People also ask

Can OpenCV capture video?

Capture Video from CameraOpenCV allows a straightforward interface to capture live stream with the camera (webcam). It converts video into grayscale and display it. We need to create a VideoCapture object to capture a video. It accepts either the device index or the name of a video file.


2 Answers

For documentation purposes, I have expanded my comment as a solution to this problem.

The prebuilt opencv libraries for windows might have not been compiled with ffmpeg support which enables a multitude of video formats to be read.

One quick'n'dirty workaround would be installing ffmpeg and adding it to the windows PATH environment variable.

The proper solution though, would be building the opencv library from source after installing ffmpeg. This is simplified by using CMake-GUI which allows you easily configure opencv's multiple options such as adding ffmpeg support(USE_FFMPEG flag). Cmake will also generate the Visual Studio project files so you can easily compile the library.

For a more detailed guide check out the video guide on the official opencv documentation page

like image 64
George Profenza Avatar answered Sep 28 '22 15:09

George Profenza


I found a quicker workaround which happened to work for me:

In the project Release folder (C:\Users\Test\Projects\MyProject\Release) copy the file opencv_ffmpeg2410.dll (2410 comes from the used version of opencv, it might be different for you). Run the project in the release mode in Visual Studio and it could work.

You don't need to do any other modifications: if it can read webcam capture, but not video files, all you have to do is copy opencv_ffmpeg2410.dll into the Release folder of your project (where you might have previously copied other used dll's from opencv's bin folder). Hope it helps.

like image 34
user2565010 Avatar answered Sep 28 '22 15:09

user2565010