Robust Local Optical Flow Libary Documentation  V 1.2
 All Classes Files Functions Variables Enumerations Enumerator Macros Pages
RLOFSample.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * COPYRIGHT: *
3  * *
4  * This file is the property of the author and Communication Systems Group, *
5  * Technische Universität Berlin. All rights reserved. *
6  * *
7  * It may not be publicly disclosed, distributed, used, copied or modified *
8  * without prior written authorization by a representative of *
9  * Communication Systems Group, Technische Universität Berlin or the author. *
10  * Any modified version of this document needs to contain this header. *
11  ******************************************************************************/
12 
13 /******************************************************************************
14  * THERMS IF USAGE: *
15  * PERSONAL, NON-COMMERCIAL or ACADEMIC USAGE: *
16  * You are free to use this software for whatever you like. If you use this *
17  * algorithm for a scientific publication, please cite the one of the *
18  * following paper: *
19  * *
20  * @INPROCEEDINGS{ICIPSenst2014, *
21  * AUTHOR = {Tobias Senst and Thilo Borgmann and Ivo Keller and Thomas *
22  * Sikora}, *
23  * TITLE = {Cross based Robust Local Optical Flow}, *
24  * BOOKTITLE = {21th IEEE International Conference on Image Processing}, *
25  * YEAR = {2014}, *
26  * MONTH = okt, *
27  * PAGES = {1967--1971}, *
28  * ADDRESS = {Paris, France}, *
29  * } *
30  * *
31  * @INPROCEEDINGS{ICIPSenst2013, *
32  * AUTHOR = {Tobias Senst and Jonas Geistert and Ivo Keller and Thomas *
33  * Sikora}, *
34  * TITLE = {Robust Local Optical Flow Estimation using Bilinear Equations for *
35  * Sparse Motion Estimation}, *
36  * BOOKTITLE = {20th IEEE International Conference on Image Processing}, *
37  * YEAR = {2013}, *
38  * MONTH = sep, *
39  * PAGES = {2499--2503}, *
40  * ADDRESS = {Melbourne, Australia}, *
41  * DOI = {10.1109/ICIP.2013.6738515}, *
42  * } *
43  * *
44  * @ARTICLE{TCSVTSenst2012, *
45  * AUTHOR = {Tobias Senst and Volker Eiselein and Thomas Sikora}, *
46  * TITLE = {Robust Local Optical Flow for Feature Tracking}, *
47  * JOURNAL = {IEEE Transactions on Circuits and Systems for Video Technology},*
48  * YEAR = {2012}, *
49  * MONTH = sep, *
50  * PAGES = {1377--1387}, *
51  * VOLUME = {22}, *
52  * NUMBER = {9}, *
53  * DOI = {10.1109/TCSVT.2012.2202070} *
54  * } *
55  * *
56  * @INPROCEEDINGS{WACVSenst2011, *
57  * AUTHOR = {Tobias Senst and Volker Eiselein and Rubén Heras Evangelio and *
58  * Thomas Sikora}, *
59  * TITLE = {Robust Modified L2 Local Optical Flow Estimation and Feature *
60  * Tracking}, *
61  * BOOKTITLE = {IEEE Workshop on Motion and Video Computing}, *
62  * YEAR = {2011}, *
63  * MONTH = jan, *
64  * EDITOR = {Eric Mortensen}, *
65  * PAGES = {685--690}, *
66  * ADDRESS = {Kona, USA}, *
67  * DOI = {10.1109/WACV.2011.5711571}, *
68  * } *
69  * *
70  * COMMERCIAL USAGE: *
71  * It is not allowed to use any content of this package for any commercial *
72  * use or any advertisement for upcoming commercial products. If you want to*
73  * use any content for such a purpose please contact: *
74  * Prof. Dr.-Ing. Thomas Sikora <sikora@nue.tu-berlin.de>. *
75  ******************************************************************************/
76 
77 /******************************************************************************
78  * WARRANTIES: *
79  * *
80  * Software provided by Technische Universität Berlin with this document is *
81  * provided "AS IS" and any express of implied warranties including, but *
82  * not limited to, the implied warranties of merchantability and fitness *
83  * for a particular purpose are disclaimed. *
84  * In no event shall the author or contributors be liable for any direct, *
85  * indirect, incidental, special, exemplary, or consequential damages *
86  * (including, but not limited to, procurement of substitute goods or *
87  * services, loss of use, data, or profits or business interruption) caused in*
88  * any way out of the use of this software, even if advised of the possibility*
89  * of such damage. *
90  ******************************************************************************/
102 #include <OpticalFlow/RLOFTools.hpp>
103 #include <iostream>
104 
107 enum TrackerType { DENSE = 0
108  , GFT
109  };
110 
112 
117 void demo()
118 {
119  const TrackerType trackerType = DENSE; // or DENSE
120 
121  const RLOFType rlofType = CPU_PLK; // or CPU_CROSS_BEPLK ...
122 
123  // create parameter instances for each type of PLK/RLOF method (exampleS)
124  CPLKParameter PLKParameter; // PLK OpenCV implementation refer to CPU_PLK / GPU_PLK
125  CRobustFlowParameter RLOFParameter; // RLOF see <a href="index.html#Senst2012">Senst et al. 2012</a> refer to CPU_RLOF / GPU_RLOF
126 
127  CPLKParameter BEPLKLParameter; // BEPLK see <a href="index.html#Senst2013">Senst et al. 2013</a> refer to CPU_BEPLK
128  BEPLKLParameter.solverType(CLocalFlowParameter::ITER_BE);
129  CRobustFlowParameter BERLOFParameter; // BERLOF see <a href="index.html#Senst2013">Senst et al. 2013</a> refer to CPU_BERLOF / GPU_BERLOF
130  BERLOFParameter.solverType(CLocalFlowParameter::ITER_BE);
131 
132  CCrossPLKParameter CB_PLKParameter; // CB_BEPLK see <a href="index.html#Senst2014">Senst et al. 2014</a> refer to CPU_CROSS_BEPLK
133  CB_PLKParameter.solverType(CLocalFlowParameter::ITER_BE);
134  CCrossRobustFlowParameter CB_RLOFParameter; // CB_BERLOF see <a href="index.html#Senst2014">Senst et al. 2014</a> refer to CPU_CROSS_BERLOF
135  CB_RLOFParameter.solverType(CLocalFlowParameter::ITER_BE);
136 
137  IFeatureTracker * tracker = NULL;
138  // select the method HERE
139  CLocalFlowParameter * trackerParameter = &CB_PLKParameter;
140  try
141  {
142  std::string filename1("../Doc/ErnstReuter1.png");
143  std::string filename2("../Doc/ErnstReuter2.png");
144  cv::Mat img1;
145  cv::Mat img2;
146  cv::Mat rgbOutput;
147  img1 = cv::imread(filename1);
148  img2 = cv::imread(filename2);
149 
150  // initialise PLK/RLOF method old version
151  //IFeatureTracker * rlof = RLOFFactory::createFTInstance(rlofType);
152  // initialise PLK/RLOF method new version. NOTE Only a view methods support the GPU.
153  tracker = RLOFFactory::createFTInstance(trackerParameter);
154 
155  /* the parameter could be modified here e.g
156  CB_PLKParameter.level(1);
157  */
158 
159  if( trackerType == DENSE)
160  {
161  // run rlof for the whole image
162  cv::Mat U;
163  cv::Mat V;
164  double time = static_cast<double>(cv::getTickCount());
165  denseMotionField(img1, img2, tracker, *trackerParameter, U, V);
166  std::cout << "Dense (ms): " << (static_cast<double>(cv::getTickCount()) - time) / (cvGetTickFrequency() * 1000.) << std::endl;
167 
168  // colormap conversion
169  FlowToRGB(U, V, rgbOutput, 5);
170  }
171  else if (trackerType == GFT)
172  {
173  // run KLT like Good Features to Track + RLOF
174  std::vector<cv::Point2f> ptList;
175  std::vector<CRPoint> ptList1;
176  std::vector<CRPoint> ptList2;
177  // detect Good Features to Track
178  double time = static_cast<double>(cv::getTickCount());
179  cv::goodFeaturesToTrack(img1, ptList, 15000, 0.01, 3);
180  cvTocr(ptList1, ptList);
181  // track them
182 
183  tracker->run(img1.ptr<uchar>(),
184  img1.step1(),
185  img2.ptr<uchar>(),
186  img2.step1(),
187  img1.rows,
188  img1.cols,
189  ptList1,
190  ptList2,
191  &CB_PLKParameter);
192  // draw the result
193  std::cout << "GFT (ms): " << (static_cast<double>(cv::getTickCount()) - time) / (cvGetTickFrequency() * 1000.) << std::endl;
194  cv::cvtColor(img1,rgbOutput, CV_GRAY2RGB);
195  for(unsigned int n = 0 ; n < ptList1.size(); n++)
196  {
197  cv::Point pt1(static_cast<int>(ptList1[n].x), static_cast<int>(ptList1[n].y));
198  cv::Point pt2(static_cast<int>(ptList2[n].x), static_cast<int>(ptList2[n].y));
199  cv::circle(rgbOutput, pt1, 2, cv::Scalar(255,0,0), -1);
200  cv::line(rgbOutput, pt1, pt2, cv::Scalar(0, 255, 0), 1);
201  }
202 
203  }
204  delete tracker;
205  // display results
206  cv::namedWindow("Motion Field",0);
207  cv::imshow("Motion Field", rgbOutput);
208  cv::waitKey(-1);
209 
210  }
211  catch(std::exception & e)
212  {
213  std::cout << e.what()<<std::endl;
214  system("pause");
215  delete tracker;
216  }
217 }
218 
224 int main(int argc, char** argv)
225 {
226  // Perform initialization test
227  test0();
228  // Perform accuracy test
229  test1();
230  // Example
231  demo();
232  return 1;
233 }
234 
int main(int argc, char **argv)
Definition: RLOFSample.cpp:224
void solverType(SolverType val)
RLOF parameter class The Robust Local Optical Flow parameter class contains additional parameters use...
void FlowToRGB(const cv::Mat &dx, const cv::Mat &dy, cv::Mat &rgbFlow, const float &max_size=-1, bool use_value=false, cv::Mat &sat=cv::Mat())
Convert motion field to RGB colormap.
Definition: RLOFTools.hpp:180
Pyramidal Lucas Kanade parameter class The Pyramidal Lucas Kanade parameter class. Please use a instance of this class to run the PLK version. The parameter class is associated to PLK.
void test0()
Definition: RLOFTools.hpp:342
static IFeatureTracker * createFTInstance(RLOFType type=CPU_BEPLK)
RLOF Interface.
PLK/RLOF parameter base class The parameter base class provides basic interfaces to set up the algori...
void test1()
Definition: RLOFTools.hpp:276
Cross based Pyramidal Lucas Kanade parameter class The cross based Pyramidal Lucas Kanade parameter c...
TrackerType
Definition: RLOFSample.cpp:107
RLOFType
Definition: RLOFFactory.h:110
static void cvTocr(std::vector< CRPoint > &lhs, const std::vector< cv::Point2f > &rhs)
Definition: cvwrapper.hpp:126
Motion estimation and illustration.
static void denseMotionField(const cv::Mat &img1, const cv::Mat &img2, IFeatureTracker *tracker, FeatureTrackerContext &param, cv::Mat &U, cv::Mat &V)
Definition: RLOFTools.hpp:116
virtual void run(const uchar *imgPtr1, unsigned int step1, const uchar *imgPtr2, unsigned int step2, unsigned int nRows, unsigned int nCols, std::vector< CRPoint > &prevPoints, std::vector< CRPoint > &currPoints, const FeatureTrackerContext *context)
void demo()
Demo programm.
Definition: RLOFSample.cpp:117
Cross based RLOF parameter class The cross based Robust Local Optical Flow parameter class contains i...