-
Notifications
You must be signed in to change notification settings - Fork 10
/
gpuOpticalFlow.cpp
105 lines (89 loc) · 2.88 KB
/
gpuOpticalFlow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* Copyright (C) 2015 An Tran.
* This code is for research, please do not distribute it.
*
*/
#include <mex.h>
#include <opencv2/opencv.hpp>
#include <opencv2/cudaoptflow.hpp>
#include "mexopencv.hpp"
using namespace cv;
/**
* @brief myCudaBroxOpticalFlow
* To compute dense optical flow using Thomas Brox algorithm.
*
* @param prev_grey
* @param grey
* @param flow
*/
void myCudaBroxOpticalFlow(const Mat prev_grey, const Mat grey, Mat& flow)
{
cuda::GpuMat d_prev_grey, d_grey, d_flow;
d_prev_grey.upload( prev_grey );
d_grey.upload( grey );
Ptr<cuda::BroxOpticalFlow> broxFlow = cuda::BroxOpticalFlow::create();
broxFlow->calc(d_prev_grey, d_grey, d_flow);
d_flow.download( flow );
//MedianBlurFlow( flow, 5 );
}
/**
* @brief myCudaOpticalFlowDual_TVL1
* TVL1 optical flow seems to be also accurate in large displacement optical flow.
*
* @param prev_grey
* @param grey
* @param flow
*/
void myCudaOpticalFlowDual_TVL1(const Mat prev_grey, const Mat grey, Mat& flow)
{
cuda::GpuMat d_prev_grey, d_grey, d_flow;
d_prev_grey.upload( prev_grey );
d_grey.upload( grey );
// create algo with default parameters
Ptr<cuda::OpticalFlowDual_TVL1> tvl1Flow = cuda::OpticalFlowDual_TVL1::create();
tvl1Flow->calc(d_prev_grey, d_grey, d_flow);
//MedianBlurFlow(flow, 5);
d_flow.download( flow );
}
/**
* @brief Mex function to get TVL1 optical flow from a paire of images
* [flows] = cpuOpticalFlow(image_filename1, image_filename2);
*
*/
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
// Check the number of arguments
if (nrhs < 2)
mexErrMsgIdAndTxt("gpuOpticalFlow:error", "Wrong number of arguments");
MxArray input = MxArray(prhs[0]);
if (!input.isChar()) // focus when coding
mexErrMsgIdAndTxt("gpuOpticalFlow:error", "Image1 input argument is not a string");
std::string img1File = input.toString();
MxArray output = MxArray(prhs[1]);
if (!output.isChar()) // focus when coding
mexErrMsgIdAndTxt("gpuOpticalFlow:error", "Image2 folder argument is not a string");
std::string img2File = output.toString();
bool isTVL1 = true;
if (nrhs == 3) {
MxArray useTVL1 = MxArray(prhs[2]);
if (!useTVL1.isDouble())
mexErrMsgIdAndTxt("gpuOpticalFlow:error", "useTVL1 must be a double");
isTVL1 = useTVL1.toBool();
}
// compute optical flow
Mat grey1, grey2, flow;
Mat img1 = imread(img1File);
cvtColor(img1, grey1, CV_BGR2GRAY);
Mat img2 = imread(img2File);
cvtColor(img2, grey2, CV_BGR2GRAY);
if (isTVL1) {
myCudaOpticalFlowDual_TVL1(grey1, grey2, flow);
}
else {
grey1.convertTo(grey1, CV_32FC1);
grey2.convertTo(grey2, CV_32FC1);
myCudaBroxOpticalFlow(grey1, grey2, flow);
}
// assgin cv::Mat back to Matlab
plhs[0] = MxArray(flow);
}