main_updated.cpp

Updated source code with fixes - Vladislav Vinogradov, 2015-05-19 10:50 am

Download (1.3 kB)

 
1
#include <opencv2/cudaoptflow.hpp>
2
#include <opencv2/cudaimgproc.hpp>
3
#include <opencv2/highgui.hpp>
4
#include <opencv2/imgproc.hpp>
5
#include <opencv2/optflow.hpp>
6
#include <iostream>
7
8
using namespace std;
9
using namespace cv;
10
11
int main(int argc, char** argv)
12
{
13
    cout << "OpenCV version: " << CV_VERSION << endl;
14
    VideoCapture capture;
15
    char* video = argv[1];
16
    capture.open(video);
17
18
    if(!capture.isOpened()) {
19
        fprintf(stderr, "Could not initialize capturing..\n");
20
        return -1;
21
    }
22
23
    Mat frame;
24
    cuda::GpuMat d_frame, d_prev_grey, d_grey, d_flow;
25
    int frame_num = 0;
26
    for(;;)
27
    {
28
        // get a new frame
29
        capture >> frame;
30
        if(frame.empty())
31
            break;
32
        frame_num++;
33
34
        d_frame.upload(frame);
35
36
        if (frame_num == 1)
37
        {
38
            cuda::cvtColor(d_frame, d_prev_grey, CV_BGR2GRAY);
39
            continue;
40
        }
41
        cuda::cvtColor(d_frame, d_grey, CV_BGR2GRAY);
42
43
        // compute optical flow
44
        //cv::calcOpticalFlowFarneback(prev_grey, grey, flow, 0.5, 3, 15, 3, 7, 1.5, OPTFLOW_FARNEBACK_GAUSSIAN);
45
        Ptr<cuda::BroxOpticalFlow> broxFlow = cuda::BroxOpticalFlow::create();
46
        broxFlow->calc(d_prev_grey, d_grey, d_flow);
47
48
        // assign new img to old img
49
        d_prev_grey.swap(d_grey);
50
    }
51
}