1 /*M///////////////////////////////////////////////////////////////////////////////////////
\r
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
\r
5 // By downloading, copying, installing or using the software you agree to this license.
\r
6 // If you do not agree to this license, do not download, install,
\r
7 // copy or use the software.
\r
10 // License Agreement
\r
11 // For Open Source Computer Vision Library
\r
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
\r
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
\r
15 // Third party copyrights are property of their respective owners.
\r
17 // Redistribution and use in source and binary forms, with or without modification,
\r
18 // are permitted provided that the following conditions are met:
\r
20 // * Redistribution's of source code must retain the above copyright notice,
\r
21 // this list of conditions and the following disclaimer.
\r
23 // * Redistribution's in binary form must reproduce the above copyright notice,
\r
24 // this list of conditions and the following disclaimer in the documentation
\r
25 // and/or other materials provided with the distribution.
\r
27 // * The name of the copyright holders may not be used to endorse or promote products
\r
28 // derived from this software without specific prior written permission.
\r
30 // This software is provided by the copyright holders and contributors "as is" and
\r
31 // any express or implied warranties, including, but not limited to, the implied
\r
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
\r
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
\r
34 // indirect, incidental, special, exemplary, or consequential damages
\r
35 // (including, but not limited to, procurement of substitute goods or services;
\r
36 // loss of use, data, or profits; or business interruption) however caused
\r
37 // and on any theory of liability, whether in contract, strict liability,
\r
38 // or tort (including negligence or otherwise) arising in any way out of
\r
39 // the use of this software, even if advised of the possibility of such damage.
\r
52 using namespace std;
\r
54 class CV_OptFlowTest : public CvTest
\r
62 bool runDense(const Point& shift = Point(3, 0));
\r
66 CV_OptFlowTest::CV_OptFlowTest(): CvTest( "algorithm-opticalflow", "?" )
\r
68 support_testing_modes = CvTS::CORRECTNESS_CHECK_MODE;
\r
70 CV_OptFlowTest::~CV_OptFlowTest() {}
\r
73 Mat copnvert2flow(const Mat& velx, const Mat& vely)
\r
75 Mat flow(velx.size(), CV_32FC2);
\r
76 for(int y = 0 ; y < flow.rows; ++y)
\r
77 for(int x = 0 ; x < flow.cols; ++x)
\r
78 flow.at<Point2f>(y, x) = Point2f(velx.at<float>(y, x), vely.at<float>(y, x));
\r
82 void calcOpticalFlowLK( const Mat& prev, const Mat& curr, Size winSize, Mat& flow )
\r
84 Mat velx(prev.size(), CV_32F), vely(prev.size(), CV_32F);
\r
85 CvMat cvvelx = velx; CvMat cvvely = vely;
\r
86 CvMat cvprev = prev; CvMat cvcurr = curr;
\r
87 cvCalcOpticalFlowLK( &cvprev, &cvcurr, winSize, &cvvelx, &cvvely );
\r
88 flow = copnvert2flow(velx, vely);
\r
91 void calcOpticalFlowBM( const Mat& prev, const Mat& curr, Size bSize, Size shiftSize, Size maxRange, int usePrevious, Mat& flow )
\r
93 Size sz((curr.cols - bSize.width)/shiftSize.width, (curr.rows - bSize.height)/shiftSize.height);
\r
94 Mat velx(sz, CV_32F), vely(sz, CV_32F);
\r
96 CvMat cvvelx = velx; CvMat cvvely = vely;
\r
97 CvMat cvprev = prev; CvMat cvcurr = curr;
\r
98 cvCalcOpticalFlowBM( &cvprev, &cvcurr, bSize, shiftSize, maxRange, usePrevious, &cvvelx, &cvvely);
\r
99 flow = copnvert2flow(velx, vely);
\r
102 void calcOpticalFlowHS( const Mat& prev, const Mat& curr, int usePrevious, double lambda, TermCriteria criteria, Mat& flow)
\r
104 Mat velx(prev.size(), CV_32F), vely(prev.size(), CV_32F);
\r
105 CvMat cvvelx = velx; CvMat cvvely = vely;
\r
106 CvMat cvprev = prev; CvMat cvcurr = curr;
\r
107 cvCalcOpticalFlowHS( &cvprev, &cvcurr, usePrevious, &cvvelx, &cvvely, lambda, criteria );
\r
108 flow = copnvert2flow(velx, vely);
\r
111 void calcAffineFlowPyrLK( const Mat& prev, const Mat& curr,
\r
112 const vector<Point2f>& prev_features, vector<Point2f>& curr_features,
\r
113 vector<uchar>& status, vector<float>& track_error, vector<float>& matrices,
\r
114 TermCriteria criteria = TermCriteria(TermCriteria::COUNT+TermCriteria::EPS,30, 0.01),
\r
115 Size win_size = Size(15, 15), int level = 3, int flags = 0)
\r
117 CvMat cvprev = prev;
\r
118 CvMat cvcurr = curr;
\r
120 size_t count = prev_features.size();
\r
121 curr_features.resize(count);
\r
122 status.resize(count);
\r
123 track_error.resize(count);
\r
124 matrices.resize(count * 6);
\r
126 cvCalcAffineFlowPyrLK( &cvprev, &cvcurr, 0, 0,
\r
127 (const CvPoint2D32f*)&prev_features[0], (CvPoint2D32f*)&curr_features[0], &matrices[0],
\r
128 (int)count, win_size, level, (char*)&status[0], &track_error[0], criteria, flags );
\r
131 double showFlowAndCalcError(const string& name, const Mat& gray, const Mat& flow,
\r
132 const Rect& where, const Point& d,
\r
133 bool showImages = false, bool writeError = false)
\r
135 const int mult = 16;
\r
140 resize(gray, tmp, gray.size() * mult, 0, 0, INTER_NEAREST);
\r
141 cvtColor(tmp, cflow, CV_GRAY2BGR);
\r
143 const float m2 = 0.3f;
\r
144 const float minVel = 0.1f;
\r
146 for(int y = 0; y < flow.rows; ++y)
\r
147 for(int x = 0; x < flow.cols; ++x)
\r
149 Point2f f = flow.at<Point2f>(y, x);
\r
151 if (f.x * f.x + f.y * f.y > minVel * minVel)
\r
153 Point p1 = Point(x, y) * mult;
\r
154 Point p2 = Point(cvRound((x + f.x*m2) * mult), cvRound((y + f.y*m2) * mult));
\r
156 line(cflow, p1, p2, CV_RGB(0, 255, 0));
\r
157 circle(cflow, Point(x, y) * mult, 2, CV_RGB(255, 0, 0));
\r
161 rectangle(cflow, (where.tl() + d) * mult, (where.br() + d - Point(1,1)) * mult, CV_RGB(0, 0, 255));
\r
162 namedWindow(name, 1); imshow(name, cflow);
\r
165 double angle = atan2((float)d.y, (float)d.x);
\r
169 Mat inner = flow(where);
\r
170 for(int y = 0; y < inner.rows; ++y)
\r
171 for(int x = 0; x < inner.cols; ++x)
\r
173 const Point2f f = inner.at<Point2f>(y, x);
\r
175 if (f.x == 0 && f.y == 0)
\r
180 double a = atan2(f.y, f.x);
\r
181 error += fabs(angle - a);
\r
183 double res = all ? numeric_limits<double>::max() : error / (inner.cols * inner.rows);
\r
186 cout << "Error " + name << " = " << res << endl;
\r
192 Mat generateImage(const Size& sz, bool doBlur = true)
\r
195 Mat mat(sz, CV_8U);
\r
197 for(int y = 0; y < mat.rows; ++y)
\r
198 for(int x = 0; x < mat.cols; ++x)
\r
199 mat.at<uchar>(y, x) = (uchar)rng;
\r
201 blur(mat, mat, Size(3, 3));
\r
205 Mat generateSample(const Size& sz)
\r
207 Mat smpl(sz, CV_8U);
\r
209 Point sc(smpl.cols/2, smpl.rows/2);
\r
210 rectangle(smpl, Point(0,0), sc - Point(1,1), Scalar(255), CV_FILLED);
\r
211 rectangle(smpl, sc, Point(smpl.cols, smpl.rows), Scalar(255), CV_FILLED);
\r
215 bool CV_OptFlowTest::runDense(const Point& d)
\r
217 Size matSize(40, 40);
\r
218 Size movSize(8, 8);
\r
220 Mat smpl = generateSample(movSize);
\r
221 Mat prev = generateImage(matSize);
\r
222 Mat curr = prev.clone();
\r
224 Rect rect(Point(prev.cols/2, prev.rows/2) - Point(movSize.width/2, movSize.height/2), movSize);
\r
226 Mat flowLK, flowBM, flowHS, flowFB, flowBM_received, m1;
\r
228 m1 = prev(rect); smpl.copyTo(m1);
\r
229 m1 = curr(Rect(rect.tl() + d, rect.br() + d)); smpl.copyTo(m1);
\r
231 calcOpticalFlowLK( prev, curr, Size(15, 15), flowLK);
\r
232 calcOpticalFlowBM( prev, curr, Size(15, 15), Size(1, 1), Size(15, 15), 0, flowBM_received);
\r
233 calcOpticalFlowHS( prev, curr, 0, 5, TermCriteria(TermCriteria::MAX_ITER, 400, 0), flowHS);
\r
234 calcOpticalFlowFarneback( prev, curr, flowFB, 0.5, 3, std::max(d.x, d.y) + 10, 100, 6, 2, 0);
\r
236 flowBM.create(prev.size(), CV_32FC2);
\r
237 flowBM = Scalar(0);
\r
238 Point origin((flowBM.cols - flowBM_received.cols)/2, (flowBM.rows - flowBM_received.rows)/2);
\r
239 Mat wcp = flowBM(Rect(origin, flowBM_received.size()));
\r
240 flowBM_received.copyTo(wcp);
\r
242 double errorLK = showFlowAndCalcError("LK", prev, flowLK, rect, d);
\r
243 double errorBM = showFlowAndCalcError("BM", prev, flowBM, rect, d);
\r
244 double errorFB = showFlowAndCalcError("FB", prev, flowFB, rect, d);
\r
245 double errorHS = showFlowAndCalcError("HS", prev, flowHS, rect, d); (void)errorHS;
\r
248 const double thres = 0.2;
\r
249 if (errorLK > thres || errorBM > thres || errorFB > thres /*|| errorHS > thres /**/)
\r
251 ts->set_failed_test_info(CvTS::FAIL_MISMATCH);
\r
258 bool CV_OptFlowTest::runSparse()
\r
260 Mat prev = imread(string(ts->get_data_path()) + "optflow/rock_1.bmp", 0);
\r
261 Mat next = imread(string(ts->get_data_path()) + "optflow/rock_2.bmp", 0);
\r
263 if (prev.empty() || next.empty())
\r
265 ts->set_failed_test_info( CvTS::FAIL_INVALID_TEST_DATA );
\r
270 cvtColor(prev, cprev, CV_GRAY2BGR);
\r
271 cvtColor(next, cnext, CV_GRAY2BGR);
\r
273 vector<Point2f> prev_pts;
\r
274 vector<Point2f> next_ptsOpt;
\r
275 vector<Point2f> next_ptsAff;
\r
276 vector<uchar> status_Opt;
\r
277 vector<uchar> status_Aff;
\r
278 vector<float> error;
\r
279 vector<float> matrices;
\r
281 Size netSize(10, 10);
\r
282 Point2f center = Point(prev.cols/2, prev.rows/2);
\r
284 for(int i = 0 ; i < netSize.width; ++i)
\r
285 for(int j = 0 ; j < netSize.width; ++j)
\r
287 Point2f p(i * float(prev.cols)/netSize.width, j * float(prev.rows)/netSize.height);
\r
288 prev_pts.push_back((p - center) * 0.5f + center);
\r
291 calcOpticalFlowPyrLK( prev, next, prev_pts, next_ptsOpt, status_Opt, error );
\r
292 calcAffineFlowPyrLK ( prev, next, prev_pts, next_ptsAff, status_Aff, error, matrices);
\r
294 const double expected_shift = 25;
\r
295 const double thres = 1;
\r
296 for(size_t i = 0; i < prev_pts.size(); ++i)
\r
298 circle(cprev, prev_pts[i], 2, CV_RGB(255, 0, 0));
\r
302 circle(cnext, next_ptsOpt[i], 2, CV_RGB(0, 0, 255));
\r
303 Point2f shift = prev_pts[i] - next_ptsOpt[i];
\r
305 double n = sqrt(shift.ddot(shift));
\r
306 if (fabs(n - expected_shift) > thres)
\r
308 ts->set_failed_test_info(CvTS::FAIL_MISMATCH);
\r
315 circle(cnext, next_ptsAff[i], 4, CV_RGB(0, 255, 0));
\r
316 Point2f shift = prev_pts[i] - next_ptsAff[i];
\r
318 double n = sqrt(shift.ddot(shift));
\r
319 if (fabs(n - expected_shift) > thres)
\r
321 ts->set_failed_test_info(CvTS::FAIL_MISMATCH);
\r
328 /*namedWindow("P"); imshow("P", cprev);
\r
329 namedWindow("N"); imshow("N", cnext);
\r
336 void CV_OptFlowTest::run( int /* start_from */)
\r
339 if (!runDense(Point(3, 0)))
\r
342 if (!runDense(Point(0, 3)))
\r
345 //if (!runDense(Point(3, 3))) return; //probably LK works incorrectly in this case.
\r
350 ts->set_failed_test_info(CvTS::OK);
\r
353 CV_OptFlowTest optFlow_test;
\r