test_homography.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Copyright (C) 2015, Itseez Inc., all rights reserved.
  16. // Third party copyrights are property of their respective owners.
  17. //
  18. // Redistribution and use in source and binary forms, with or without modification,
  19. // are permitted provided that the following conditions are met:
  20. //
  21. // * Redistribution's of source code must retain the above copyright notice,
  22. // this list of conditions and the following disclaimer.
  23. //
  24. // * Redistribution's in binary form must reproduce the above copyright notice,
  25. // this list of conditions and the following disclaimer in the documentation
  26. // and/or other materials provided with the distribution.
  27. //
  28. // * The name of the copyright holders may not be used to endorse or promote products
  29. // derived from this software without specific prior written permission.
  30. //
  31. // This software is provided by the copyright holders and contributors "as is" and
  32. // any express or implied warranties, including, but not limited to, the implied
  33. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  34. // In no event shall the Intel Corporation or contributors be liable for any direct,
  35. // indirect, incidental, special, exemplary, or consequential damages
  36. // (including, but not limited to, procurement of substitute goods or services;
  37. // loss of use, data, or profits; or business interruption) however caused
  38. // and on any theory of liability, whether in contract, strict liability,
  39. // or tort (including negligence or otherwise) arising in any way out of
  40. // the use of this software, even if advised of the possibility of such damage.
  41. //
  42. //M*/
  43. #include "test_precomp.hpp"
  44. namespace opencv_test { namespace {
  45. #define CALIB3D_HOMOGRAPHY_ERROR_MATRIX_SIZE 1
  46. #define CALIB3D_HOMOGRAPHY_ERROR_MATRIX_DIFF 2
  47. #define CALIB3D_HOMOGRAPHY_ERROR_REPROJ_DIFF 3
  48. #define CALIB3D_HOMOGRAPHY_ERROR_RANSAC_MASK 4
  49. #define CALIB3D_HOMOGRAPHY_ERROR_RANSAC_DIFF 5
  50. #define MESSAGE_MATRIX_SIZE "Homography matrix must have 3*3 sizes."
  51. #define MESSAGE_MATRIX_DIFF "Accuracy of homography transformation matrix less than required."
  52. #define MESSAGE_REPROJ_DIFF_1 "Reprojection error for current pair of points more than required."
  53. #define MESSAGE_REPROJ_DIFF_2 "Reprojection error is not optimal."
  54. #define MESSAGE_RANSAC_MASK_1 "Sizes of inliers/outliers mask are incorrect."
  55. #define MESSAGE_RANSAC_MASK_2 "Mask mustn't have any outliers."
  56. #define MESSAGE_RANSAC_MASK_3 "All values of mask must be 1 (true) or 0 (false)."
  57. #define MESSAGE_RANSAC_MASK_4 "Mask of inliers/outliers is incorrect."
  58. #define MESSAGE_RANSAC_MASK_5 "Inlier in original mask shouldn't be outlier in found mask."
  59. #define MESSAGE_RANSAC_DIFF "Reprojection error for current pair of points more than required."
  60. #define MAX_COUNT_OF_POINTS 303
  61. #define MIN_COUNT_OF_POINTS 4
  62. #define COUNT_NORM_TYPES 3
  63. #define METHODS_COUNT 4
  64. int NORM_TYPE[COUNT_NORM_TYPES] = {cv::NORM_L1, cv::NORM_L2, cv::NORM_INF};
  65. int METHOD[METHODS_COUNT] = {0, cv::RANSAC, cv::LMEDS, cv::RHO};
  66. using namespace cv;
  67. using namespace std;
  68. namespace HomographyTestUtils {
  69. static const float max_diff = 0.032f;
  70. static const float max_2diff = 0.020f;
  71. static const int image_size = 100;
  72. static const double reproj_threshold = 3.0;
  73. static const double sigma = 0.01;
  74. static bool check_matrix_size(const cv::Mat& H)
  75. {
  76. return (H.rows == 3) && (H.cols == 3);
  77. }
  78. static bool check_matrix_diff(const cv::Mat& original, const cv::Mat& found, const int norm_type, double &diff)
  79. {
  80. diff = cvtest::norm(original, found, norm_type);
  81. return diff <= max_diff;
  82. }
  83. static int check_ransac_mask_1(const Mat& src, const Mat& mask)
  84. {
  85. if (!(mask.cols == 1) && (mask.rows == src.cols)) return 1;
  86. if (countNonZero(mask) < mask.rows) return 2;
  87. for (int i = 0; i < mask.rows; ++i) if (mask.at<uchar>(i, 0) > 1) return 3;
  88. return 0;
  89. }
  90. static int check_ransac_mask_2(const Mat& original_mask, const Mat& found_mask)
  91. {
  92. if (!(found_mask.cols == 1) && (found_mask.rows == original_mask.rows)) return 1;
  93. for (int i = 0; i < found_mask.rows; ++i) if (found_mask.at<uchar>(i, 0) > 1) return 2;
  94. return 0;
  95. }
  96. static void print_information_1(int j, int N, int _method, const Mat& H)
  97. {
  98. cout << endl; cout << "Checking for homography matrix sizes..." << endl; cout << endl;
  99. cout << "Type of srcPoints: "; if ((j>-1) && (j<2)) cout << "Mat of CV_32FC2"; else cout << "vector <Point2f>";
  100. cout << " Type of dstPoints: "; if (j % 2 == 0) cout << "Mat of CV_32FC2"; else cout << "vector <Point2f>"; cout << endl;
  101. cout << "Count of points: " << N << endl; cout << endl;
  102. cout << "Method: "; if (_method == 0) cout << 0; else if (_method == 8) cout << "RANSAC"; else if (_method == cv::RHO) cout << "RHO"; else cout << "LMEDS"; cout << endl;
  103. cout << "Homography matrix:" << endl; cout << endl;
  104. cout << H << endl; cout << endl;
  105. cout << "Number of rows: " << H.rows << " Number of cols: " << H.cols << endl; cout << endl;
  106. }
  107. static void print_information_2(int j, int N, int _method, const Mat& H, const Mat& H_res, int k, double diff)
  108. {
  109. cout << endl; cout << "Checking for accuracy of homography matrix computing..." << endl; cout << endl;
  110. cout << "Type of srcPoints: "; if ((j>-1) && (j<2)) cout << "Mat of CV_32FC2"; else cout << "vector <Point2f>";
  111. cout << " Type of dstPoints: "; if (j % 2 == 0) cout << "Mat of CV_32FC2"; else cout << "vector <Point2f>"; cout << endl;
  112. cout << "Count of points: " << N << endl; cout << endl;
  113. cout << "Method: "; if (_method == 0) cout << 0; else if (_method == 8) cout << "RANSAC"; else if (_method == cv::RHO) cout << "RHO"; else cout << "LMEDS"; cout << endl;
  114. cout << "Original matrix:" << endl; cout << endl;
  115. cout << H << endl; cout << endl;
  116. cout << "Found matrix:" << endl; cout << endl;
  117. cout << H_res << endl; cout << endl;
  118. cout << "Norm type using in criteria: "; if (NORM_TYPE[k] == 1) cout << "INF"; else if (NORM_TYPE[k] == 2) cout << "L1"; else cout << "L2"; cout << endl;
  119. cout << "Difference between matrices: " << diff << endl;
  120. cout << "Maximum allowed difference: " << max_diff << endl; cout << endl;
  121. }
  122. static void print_information_3(int _method, int j, int N, const Mat& mask)
  123. {
  124. cout << endl; cout << "Checking for inliers/outliers mask..." << endl; cout << endl;
  125. cout << "Type of srcPoints: "; if ((j>-1) && (j<2)) cout << "Mat of CV_32FC2"; else cout << "vector <Point2f>";
  126. cout << " Type of dstPoints: "; if (j % 2 == 0) cout << "Mat of CV_32FC2"; else cout << "vector <Point2f>"; cout << endl;
  127. cout << "Count of points: " << N << endl; cout << endl;
  128. cout << "Method: "; if (_method == RANSAC) cout << "RANSAC" << endl; else if (_method == cv::RHO) cout << "RHO" << endl; else cout << _method << endl;
  129. cout << "Found mask:" << endl; cout << endl;
  130. cout << mask << endl; cout << endl;
  131. cout << "Number of rows: " << mask.rows << " Number of cols: " << mask.cols << endl; cout << endl;
  132. }
  133. static void print_information_4(int _method, int j, int N, int k, int l, double diff)
  134. {
  135. cout << endl; cout << "Checking for accuracy of reprojection error computing..." << endl; cout << endl;
  136. cout << "Method: "; if (_method == 0) cout << 0 << endl; else cout << "CV_LMEDS" << endl;
  137. cout << "Type of srcPoints: "; if ((j>-1) && (j<2)) cout << "Mat of CV_32FC2"; else cout << "vector <Point2f>";
  138. cout << " Type of dstPoints: "; if (j % 2 == 0) cout << "Mat of CV_32FC2"; else cout << "vector <Point2f>"; cout << endl;
  139. cout << "Sigma of normal noise: " << sigma << endl;
  140. cout << "Count of points: " << N << endl;
  141. cout << "Number of point: " << k << endl;
  142. cout << "Norm type using in criteria: "; if (NORM_TYPE[l] == 1) cout << "INF"; else if (NORM_TYPE[l] == 2) cout << "L1"; else cout << "L2"; cout << endl;
  143. cout << "Difference with noise of point: " << diff << endl;
  144. cout << "Maximum allowed difference: " << max_2diff << endl; cout << endl;
  145. }
  146. static void print_information_5(int _method, int j, int N, int l, double diff)
  147. {
  148. cout << endl; cout << "Checking for accuracy of reprojection error computing..." << endl; cout << endl;
  149. cout << "Method: "; if (_method == 0) cout << 0 << endl; else cout << "CV_LMEDS" << endl;
  150. cout << "Type of srcPoints: "; if ((j>-1) && (j<2)) cout << "Mat of CV_32FC2"; else cout << "vector <Point2f>";
  151. cout << " Type of dstPoints: "; if (j % 2 == 0) cout << "Mat of CV_32FC2"; else cout << "vector <Point2f>"; cout << endl;
  152. cout << "Sigma of normal noise: " << sigma << endl;
  153. cout << "Count of points: " << N << endl;
  154. cout << "Norm type using in criteria: "; if (NORM_TYPE[l] == 1) cout << "INF"; else if (NORM_TYPE[l] == 2) cout << "L1"; else cout << "L2"; cout << endl;
  155. cout << "Difference with noise of points: " << diff << endl;
  156. cout << "Maximum allowed difference: " << max_diff << endl; cout << endl;
  157. }
  158. static void print_information_6(int _method, int j, int N, int k, double diff, bool value)
  159. {
  160. cout << endl; cout << "Checking for inliers/outliers mask..." << endl; cout << endl;
  161. cout << "Method: "; if (_method == RANSAC) cout << "RANSAC" << endl; else if (_method == cv::RHO) cout << "RHO" << endl; else cout << _method << endl;
  162. cout << "Type of srcPoints: "; if ((j>-1) && (j<2)) cout << "Mat of CV_32FC2"; else cout << "vector <Point2f>";
  163. cout << " Type of dstPoints: "; if (j % 2 == 0) cout << "Mat of CV_32FC2"; else cout << "vector <Point2f>"; cout << endl;
  164. cout << "Count of points: " << N << " " << endl;
  165. cout << "Number of point: " << k << " " << endl;
  166. cout << "Reprojection error for this point: " << diff << " " << endl;
  167. cout << "Reprojection error threshold: " << reproj_threshold << " " << endl;
  168. cout << "Value of found mask: "<< value << endl; cout << endl;
  169. }
  170. static void print_information_7(int _method, int j, int N, int k, double diff, bool original_value, bool found_value)
  171. {
  172. cout << endl; cout << "Checking for inliers/outliers mask..." << endl; cout << endl;
  173. cout << "Method: "; if (_method == RANSAC) cout << "RANSAC" << endl; else if (_method == cv::RHO) cout << "RHO" << endl; else cout << _method << endl;
  174. cout << "Type of srcPoints: "; if ((j>-1) && (j<2)) cout << "Mat of CV_32FC2"; else cout << "vector <Point2f>";
  175. cout << " Type of dstPoints: "; if (j % 2 == 0) cout << "Mat of CV_32FC2"; else cout << "vector <Point2f>"; cout << endl;
  176. cout << "Count of points: " << N << " " << endl;
  177. cout << "Number of point: " << k << " " << endl;
  178. cout << "Reprojection error for this point: " << diff << " " << endl;
  179. cout << "Reprojection error threshold: " << reproj_threshold << " " << endl;
  180. cout << "Value of original mask: "<< original_value << " Value of found mask: " << found_value << endl; cout << endl;
  181. }
  182. static void print_information_8(int _method, int j, int N, int k, int l, double diff)
  183. {
  184. cout << endl; cout << "Checking for reprojection error of inlier..." << endl; cout << endl;
  185. cout << "Method: "; if (_method == RANSAC) cout << "RANSAC" << endl; else if (_method == cv::RHO) cout << "RHO" << endl; else cout << _method << endl;
  186. cout << "Sigma of normal noise: " << sigma << endl;
  187. cout << "Type of srcPoints: "; if ((j>-1) && (j<2)) cout << "Mat of CV_32FC2"; else cout << "vector <Point2f>";
  188. cout << " Type of dstPoints: "; if (j % 2 == 0) cout << "Mat of CV_32FC2"; else cout << "vector <Point2f>"; cout << endl;
  189. cout << "Count of points: " << N << " " << endl;
  190. cout << "Number of point: " << k << " " << endl;
  191. cout << "Norm type using in criteria: "; if (NORM_TYPE[l] == 1) cout << "INF"; else if (NORM_TYPE[l] == 2) cout << "L1"; else cout << "L2"; cout << endl;
  192. cout << "Difference with noise of point: " << diff << endl;
  193. cout << "Maximum allowed difference: " << max_2diff << endl; cout << endl;
  194. }
  195. } // HomographyTestUtils::
  196. TEST(Calib3d_Homography, accuracy)
  197. {
  198. using namespace HomographyTestUtils;
  199. for (int N = MIN_COUNT_OF_POINTS; N <= MAX_COUNT_OF_POINTS; ++N)
  200. {
  201. RNG& rng = cv::theRNG();
  202. float *src_data = new float [2*N];
  203. for (int i = 0; i < N; ++i)
  204. {
  205. src_data[2*i] = (float)cvtest::randReal(rng)*image_size;
  206. src_data[2*i+1] = (float)cvtest::randReal(rng)*image_size;
  207. }
  208. cv::Mat src_mat_2f(1, N, CV_32FC2, src_data),
  209. src_mat_2d(2, N, CV_32F, src_data),
  210. src_mat_3d(3, N, CV_32F);
  211. cv::Mat dst_mat_2f, dst_mat_2d, dst_mat_3d;
  212. vector <Point2f> src_vec, dst_vec;
  213. for (int i = 0; i < N; ++i)
  214. {
  215. float *tmp = src_mat_2d.ptr<float>()+2*i;
  216. src_mat_3d.at<float>(0, i) = tmp[0];
  217. src_mat_3d.at<float>(1, i) = tmp[1];
  218. src_mat_3d.at<float>(2, i) = 1.0f;
  219. src_vec.push_back(Point2f(tmp[0], tmp[1]));
  220. }
  221. double fi = cvtest::randReal(rng)*2*CV_PI;
  222. double t_x = cvtest::randReal(rng)*sqrt(image_size*1.0),
  223. t_y = cvtest::randReal(rng)*sqrt(image_size*1.0);
  224. double Hdata[9] = { cos(fi), -sin(fi), t_x,
  225. sin(fi), cos(fi), t_y,
  226. 0.0f, 0.0f, 1.0f };
  227. cv::Mat H_64(3, 3, CV_64F, Hdata), H_32;
  228. H_64.convertTo(H_32, CV_32F);
  229. dst_mat_3d = H_32*src_mat_3d;
  230. dst_mat_2d.create(2, N, CV_32F); dst_mat_2f.create(1, N, CV_32FC2);
  231. for (int i = 0; i < N; ++i)
  232. {
  233. float *tmp_2f = dst_mat_2f.ptr<float>()+2*i;
  234. tmp_2f[0] = dst_mat_2d.at<float>(0, i) = dst_mat_3d.at<float>(0, i) /= dst_mat_3d.at<float>(2, i);
  235. tmp_2f[1] = dst_mat_2d.at<float>(1, i) = dst_mat_3d.at<float>(1, i) /= dst_mat_3d.at<float>(2, i);
  236. dst_mat_3d.at<float>(2, i) = 1.0f;
  237. dst_vec.push_back(Point2f(tmp_2f[0], tmp_2f[1]));
  238. }
  239. for (int i = 0; i < METHODS_COUNT; ++i)
  240. {
  241. const int method = METHOD[i];
  242. switch (method)
  243. {
  244. case 0:
  245. case LMEDS:
  246. {
  247. Mat H_res_64 [4] = { cv::findHomography(src_mat_2f, dst_mat_2f, method),
  248. cv::findHomography(src_mat_2f, dst_vec, method),
  249. cv::findHomography(src_vec, dst_mat_2f, method),
  250. cv::findHomography(src_vec, dst_vec, method) };
  251. for (int j = 0; j < 4; ++j)
  252. {
  253. if (!check_matrix_size(H_res_64[j]))
  254. {
  255. print_information_1(j, N, method, H_res_64[j]);
  256. CV_Error(CALIB3D_HOMOGRAPHY_ERROR_MATRIX_SIZE, MESSAGE_MATRIX_SIZE);
  257. return;
  258. }
  259. double diff;
  260. for (int k = 0; k < COUNT_NORM_TYPES; ++k)
  261. if (!check_matrix_diff(H_64, H_res_64[j], NORM_TYPE[k], diff))
  262. {
  263. print_information_2(j, N, method, H_64, H_res_64[j], k, diff);
  264. CV_Error(CALIB3D_HOMOGRAPHY_ERROR_MATRIX_DIFF, MESSAGE_MATRIX_DIFF);
  265. return;
  266. }
  267. }
  268. continue;
  269. }
  270. case cv::RHO:
  271. case RANSAC:
  272. {
  273. cv::Mat mask [4]; double diff;
  274. Mat H_res_64 [4] = { cv::findHomography(src_mat_2f, dst_mat_2f, method, reproj_threshold, mask[0]),
  275. cv::findHomography(src_mat_2f, dst_vec, method, reproj_threshold, mask[1]),
  276. cv::findHomography(src_vec, dst_mat_2f, method, reproj_threshold, mask[2]),
  277. cv::findHomography(src_vec, dst_vec, method, reproj_threshold, mask[3]) };
  278. for (int j = 0; j < 4; ++j)
  279. {
  280. if (!check_matrix_size(H_res_64[j]))
  281. {
  282. print_information_1(j, N, method, H_res_64[j]);
  283. CV_Error(CALIB3D_HOMOGRAPHY_ERROR_MATRIX_SIZE, MESSAGE_MATRIX_SIZE);
  284. return;
  285. }
  286. for (int k = 0; k < COUNT_NORM_TYPES; ++k)
  287. if (!check_matrix_diff(H_64, H_res_64[j], NORM_TYPE[k], diff))
  288. {
  289. print_information_2(j, N, method, H_64, H_res_64[j], k, diff);
  290. CV_Error(CALIB3D_HOMOGRAPHY_ERROR_MATRIX_DIFF, MESSAGE_MATRIX_DIFF);
  291. return;
  292. }
  293. int code = check_ransac_mask_1(src_mat_2f, mask[j]);
  294. if (code)
  295. {
  296. print_information_3(method, j, N, mask[j]);
  297. switch (code)
  298. {
  299. case 1: { CV_Error(CALIB3D_HOMOGRAPHY_ERROR_RANSAC_MASK, MESSAGE_RANSAC_MASK_1); break; }
  300. case 2: { CV_Error(CALIB3D_HOMOGRAPHY_ERROR_RANSAC_MASK, MESSAGE_RANSAC_MASK_2); break; }
  301. case 3: { CV_Error(CALIB3D_HOMOGRAPHY_ERROR_RANSAC_MASK, MESSAGE_RANSAC_MASK_3); break; }
  302. default: break;
  303. }
  304. return;
  305. }
  306. }
  307. continue;
  308. }
  309. default: continue;
  310. }
  311. }
  312. Mat noise_2f(1, N, CV_32FC2);
  313. rng.fill(noise_2f, RNG::NORMAL, Scalar::all(0), Scalar::all(sigma));
  314. cv::Mat mask(N, 1, CV_8UC1);
  315. for (int i = 0; i < N; ++i)
  316. {
  317. float *a = noise_2f.ptr<float>()+2*i, *_2f = dst_mat_2f.ptr<float>()+2*i;
  318. _2f[0] += a[0]; _2f[1] += a[1];
  319. mask.at<bool>(i, 0) = !(sqrt(a[0]*a[0]+a[1]*a[1]) > reproj_threshold);
  320. }
  321. for (int i = 0; i < METHODS_COUNT; ++i)
  322. {
  323. const int method = METHOD[i];
  324. switch (method)
  325. {
  326. case 0:
  327. case LMEDS:
  328. {
  329. Mat H_res_64 [4] = { cv::findHomography(src_mat_2f, dst_mat_2f),
  330. cv::findHomography(src_mat_2f, dst_vec),
  331. cv::findHomography(src_vec, dst_mat_2f),
  332. cv::findHomography(src_vec, dst_vec) };
  333. for (int j = 0; j < 4; ++j)
  334. {
  335. if (!check_matrix_size(H_res_64[j]))
  336. {
  337. print_information_1(j, N, method, H_res_64[j]);
  338. CV_Error(CALIB3D_HOMOGRAPHY_ERROR_MATRIX_SIZE, MESSAGE_MATRIX_SIZE);
  339. return;
  340. }
  341. Mat H_res_32; H_res_64[j].convertTo(H_res_32, CV_32F);
  342. cv::Mat dst_res_3d(3, N, CV_32F), noise_2d(2, N, CV_32F);
  343. for (int k = 0; k < N; ++k)
  344. {
  345. Mat tmp_mat_3d = H_res_32*src_mat_3d.col(k);
  346. dst_res_3d.at<float>(0, k) = tmp_mat_3d.at<float>(0, 0) /= tmp_mat_3d.at<float>(2, 0);
  347. dst_res_3d.at<float>(1, k) = tmp_mat_3d.at<float>(1, 0) /= tmp_mat_3d.at<float>(2, 0);
  348. dst_res_3d.at<float>(2, k) = tmp_mat_3d.at<float>(2, 0) = 1.0f;
  349. float *a = noise_2f.ptr<float>()+2*k;
  350. noise_2d.at<float>(0, k) = a[0]; noise_2d.at<float>(1, k) = a[1];
  351. for (int l = 0; l < COUNT_NORM_TYPES; ++l)
  352. if (cv::norm(tmp_mat_3d, dst_mat_3d.col(k), NORM_TYPE[l]) - cv::norm(noise_2d.col(k), NORM_TYPE[l]) > max_2diff)
  353. {
  354. print_information_4(method, j, N, k, l, cv::norm(tmp_mat_3d, dst_mat_3d.col(k), NORM_TYPE[l]) - cv::norm(noise_2d.col(k), NORM_TYPE[l]));
  355. CV_Error(CALIB3D_HOMOGRAPHY_ERROR_REPROJ_DIFF, MESSAGE_REPROJ_DIFF_1);
  356. return;
  357. }
  358. }
  359. for (int l = 0; l < COUNT_NORM_TYPES; ++l)
  360. if (cv::norm(dst_res_3d, dst_mat_3d, NORM_TYPE[l]) - cv::norm(noise_2d, NORM_TYPE[l]) > max_diff)
  361. {
  362. print_information_5(method, j, N, l, cv::norm(dst_res_3d, dst_mat_3d, NORM_TYPE[l]) - cv::norm(noise_2d, NORM_TYPE[l]));
  363. CV_Error(CALIB3D_HOMOGRAPHY_ERROR_REPROJ_DIFF, MESSAGE_REPROJ_DIFF_2);
  364. return;
  365. }
  366. }
  367. continue;
  368. }
  369. case cv::RHO:
  370. case RANSAC:
  371. {
  372. cv::Mat mask_res [4];
  373. Mat H_res_64 [4] = { cv::findHomography(src_mat_2f, dst_mat_2f, method, reproj_threshold, mask_res[0]),
  374. cv::findHomography(src_mat_2f, dst_vec, method, reproj_threshold, mask_res[1]),
  375. cv::findHomography(src_vec, dst_mat_2f, method, reproj_threshold, mask_res[2]),
  376. cv::findHomography(src_vec, dst_vec, method, reproj_threshold, mask_res[3]) };
  377. for (int j = 0; j < 4; ++j)
  378. {
  379. if (!check_matrix_size(H_res_64[j]))
  380. {
  381. print_information_1(j, N, method, H_res_64[j]);
  382. CV_Error(CALIB3D_HOMOGRAPHY_ERROR_MATRIX_SIZE, MESSAGE_MATRIX_SIZE);
  383. return;
  384. }
  385. int code = check_ransac_mask_2(mask, mask_res[j]);
  386. if (code)
  387. {
  388. print_information_3(method, j, N, mask_res[j]);
  389. switch (code)
  390. {
  391. case 1: { CV_Error(CALIB3D_HOMOGRAPHY_ERROR_RANSAC_MASK, MESSAGE_RANSAC_MASK_1); break; }
  392. case 2: { CV_Error(CALIB3D_HOMOGRAPHY_ERROR_RANSAC_MASK, MESSAGE_RANSAC_MASK_3); break; }
  393. default: break;
  394. }
  395. return;
  396. }
  397. cv::Mat H_res_32; H_res_64[j].convertTo(H_res_32, CV_32F);
  398. cv::Mat dst_res_3d = H_res_32*src_mat_3d;
  399. for (int k = 0; k < N; ++k)
  400. {
  401. dst_res_3d.at<float>(0, k) /= dst_res_3d.at<float>(2, k);
  402. dst_res_3d.at<float>(1, k) /= dst_res_3d.at<float>(2, k);
  403. dst_res_3d.at<float>(2, k) = 1.0f;
  404. float *p = dst_mat_2f.ptr<float>()+2*k;
  405. dst_mat_3d.at<float>(0, k) = p[0];
  406. dst_mat_3d.at<float>(1, k) = p[1];
  407. double diff = cv::norm(dst_res_3d.col(k), dst_mat_3d.col(k), NORM_L2);
  408. if (mask_res[j].at<bool>(k, 0) != (diff <= reproj_threshold))
  409. {
  410. print_information_6(method, j, N, k, diff, mask_res[j].at<bool>(k, 0));
  411. CV_Error(CALIB3D_HOMOGRAPHY_ERROR_RANSAC_MASK, MESSAGE_RANSAC_MASK_4);
  412. return;
  413. }
  414. if (mask.at<bool>(k, 0) && !mask_res[j].at<bool>(k, 0))
  415. {
  416. print_information_7(method, j, N, k, diff, mask.at<bool>(k, 0), mask_res[j].at<bool>(k, 0));
  417. CV_Error(CALIB3D_HOMOGRAPHY_ERROR_RANSAC_MASK, MESSAGE_RANSAC_MASK_5);
  418. return;
  419. }
  420. if (mask_res[j].at<bool>(k, 0))
  421. {
  422. float *a = noise_2f.ptr<float>()+2*k;
  423. dst_mat_3d.at<float>(0, k) -= a[0];
  424. dst_mat_3d.at<float>(1, k) -= a[1];
  425. cv::Mat noise_2d(2, 1, CV_32F);
  426. noise_2d.at<float>(0, 0) = a[0]; noise_2d.at<float>(1, 0) = a[1];
  427. for (int l = 0; l < COUNT_NORM_TYPES; ++l)
  428. {
  429. diff = cv::norm(dst_res_3d.col(k), dst_mat_3d.col(k), NORM_TYPE[l]);
  430. if (diff - cv::norm(noise_2d, NORM_TYPE[l]) > max_2diff)
  431. {
  432. print_information_8(method, j, N, k, l, diff - cv::norm(noise_2d, NORM_TYPE[l]));
  433. CV_Error(CALIB3D_HOMOGRAPHY_ERROR_RANSAC_DIFF, MESSAGE_RANSAC_DIFF);
  434. return;
  435. }
  436. }
  437. }
  438. }
  439. }
  440. continue;
  441. }
  442. default: continue;
  443. }
  444. }
  445. delete[]src_data;
  446. src_data = NULL;
  447. }
  448. }
  449. TEST(Calib3d_Homography, EKcase)
  450. {
  451. float pt1data[] =
  452. {
  453. 2.80073029e+002f, 2.39591217e+002f, 2.21912201e+002f, 2.59783997e+002f,
  454. 2.16053192e+002f, 2.78826569e+002f, 2.22782532e+002f, 2.82330383e+002f,
  455. 2.09924820e+002f, 2.89122559e+002f, 2.11077698e+002f, 2.89384674e+002f,
  456. 2.25287689e+002f, 2.88795532e+002f, 2.11180801e+002f, 2.89653503e+002f,
  457. 2.24126404e+002f, 2.90466064e+002f, 2.10914429e+002f, 2.90886963e+002f,
  458. 2.23439362e+002f, 2.91657715e+002f, 2.24809387e+002f, 2.91891602e+002f,
  459. 2.09809082e+002f, 2.92891113e+002f, 2.08771164e+002f, 2.93093231e+002f,
  460. 2.23160095e+002f, 2.93259460e+002f, 2.07874023e+002f, 2.93989990e+002f,
  461. 2.08963638e+002f, 2.94209839e+002f, 2.23963165e+002f, 2.94479645e+002f,
  462. 2.23241791e+002f, 2.94887817e+002f, 2.09438782e+002f, 2.95233337e+002f,
  463. 2.08901886e+002f, 2.95762878e+002f, 2.21867981e+002f, 2.95747711e+002f,
  464. 2.24195511e+002f, 2.98270905e+002f, 2.09331345e+002f, 3.05958191e+002f,
  465. 2.24727875e+002f, 3.07186035e+002f, 2.26718842e+002f, 3.08095795e+002f,
  466. 2.25363953e+002f, 3.08200226e+002f, 2.19897797e+002f, 3.13845093e+002f,
  467. 2.25013474e+002f, 3.15558777e+002f
  468. };
  469. float pt2data[] =
  470. {
  471. 1.84072723e+002f, 1.43591202e+002f, 1.25912483e+002f, 1.63783859e+002f,
  472. 2.06439407e+002f, 2.20573929e+002f, 1.43801437e+002f, 1.80703903e+002f,
  473. 9.77904129e+000f, 2.49660202e+002f, 1.38458405e+001f, 2.14502701e+002f,
  474. 1.50636337e+002f, 2.15597183e+002f, 6.43103180e+001f, 2.51667648e+002f,
  475. 1.54952499e+002f, 2.20780014e+002f, 1.26638412e+002f, 2.43040924e+002f,
  476. 3.67568909e+002f, 1.83624954e+001f, 1.60657944e+002f, 2.21794052e+002f,
  477. -1.29507828e+000f, 3.32472443e+002f, 8.51442242e+000f, 4.15561554e+002f,
  478. 1.27161377e+002f, 1.97260361e+002f, 5.40714645e+000f, 4.90978302e+002f,
  479. 2.25571690e+001f, 3.96912415e+002f, 2.95664978e+002f, 7.36064959e+000f,
  480. 1.27241104e+002f, 1.98887573e+002f, -1.25569367e+000f, 3.87713226e+002f,
  481. 1.04194012e+001f, 4.31495758e+002f, 1.25868874e+002f, 1.99751617e+002f,
  482. 1.28195480e+002f, 2.02270355e+002f, 2.23436356e+002f, 1.80489182e+002f,
  483. 1.28727692e+002f, 2.11185410e+002f, 2.03336639e+002f, 2.52182083e+002f,
  484. 1.29366486e+002f, 2.12201904e+002f, 1.23897598e+002f, 2.17847351e+002f,
  485. 1.29015259e+002f, 2.19560623e+002f
  486. };
  487. int npoints = (int)(sizeof(pt1data)/sizeof(pt1data[0])/2);
  488. Mat p1(1, npoints, CV_32FC2, pt1data);
  489. Mat p2(1, npoints, CV_32FC2, pt2data);
  490. Mat mask;
  491. Mat h = findHomography(p1, p2, RANSAC, 0.01, mask);
  492. ASSERT_TRUE(!h.empty());
  493. cv::transpose(mask, mask);
  494. Mat p3, mask2;
  495. int ninliers = countNonZero(mask);
  496. Mat nmask[] = { mask, mask };
  497. merge(nmask, 2, mask2);
  498. perspectiveTransform(p1, p3, h);
  499. mask2 = mask2.reshape(1);
  500. p2 = p2.reshape(1);
  501. p3 = p3.reshape(1);
  502. double err = cvtest::norm(p2, p3, NORM_INF, mask2);
  503. printf("ninliers: %d, inliers err: %.2g\n", ninliers, err);
  504. ASSERT_GE(ninliers, 10);
  505. ASSERT_LE(err, 0.01);
  506. }
  507. TEST(Calib3d_Homography, fromImages)
  508. {
  509. Mat img_1 = imread(cvtest::TS::ptr()->get_data_path() + "cv/optflow/image1.png", 0);
  510. Mat img_2 = imread(cvtest::TS::ptr()->get_data_path() + "cv/optflow/image2.png", 0);
  511. Ptr<ORB> orb = ORB::create();
  512. vector<KeyPoint> keypoints_1, keypoints_2;
  513. Mat descriptors_1, descriptors_2;
  514. orb->detectAndCompute( img_1, Mat(), keypoints_1, descriptors_1, false );
  515. orb->detectAndCompute( img_2, Mat(), keypoints_2, descriptors_2, false );
  516. //-- Step 3: Matching descriptor vectors using Brute Force matcher
  517. BFMatcher matcher(NORM_HAMMING,false);
  518. std::vector< DMatch > matches;
  519. matcher.match( descriptors_1, descriptors_2, matches );
  520. double max_dist = 0; double min_dist = 100;
  521. //-- Quick calculation of max and min distances between keypoints
  522. for( int i = 0; i < descriptors_1.rows; i++ )
  523. {
  524. double dist = matches[i].distance;
  525. if( dist < min_dist ) min_dist = dist;
  526. if( dist > max_dist ) max_dist = dist;
  527. }
  528. //-- Draw only "good" matches (i.e. whose distance is less than 3*min_dist )
  529. std::vector< DMatch > good_matches;
  530. for( int i = 0; i < descriptors_1.rows; i++ )
  531. {
  532. if( matches[i].distance <= 100 )
  533. good_matches.push_back( matches[i]);
  534. }
  535. //-- Localize the model
  536. std::vector<Point2f> pointframe1;
  537. std::vector<Point2f> pointframe2;
  538. for( int i = 0; i < (int)good_matches.size(); i++ )
  539. {
  540. //-- Get the keypoints from the good matches
  541. pointframe1.push_back( keypoints_1[ good_matches[i].queryIdx ].pt );
  542. pointframe2.push_back( keypoints_2[ good_matches[i].trainIdx ].pt );
  543. }
  544. Mat H0, H1, inliers0, inliers1;
  545. double min_t0 = DBL_MAX, min_t1 = DBL_MAX;
  546. for( int i = 0; i < 10; i++ )
  547. {
  548. double t = (double)getTickCount();
  549. H0 = findHomography( pointframe1, pointframe2, RANSAC, 3.0, inliers0 );
  550. t = (double)getTickCount() - t;
  551. min_t0 = std::min(min_t0, t);
  552. }
  553. int ninliers0 = countNonZero(inliers0);
  554. for( int i = 0; i < 10; i++ )
  555. {
  556. double t = (double)getTickCount();
  557. H1 = findHomography( pointframe1, pointframe2, RHO, 3.0, inliers1 );
  558. t = (double)getTickCount() - t;
  559. min_t1 = std::min(min_t1, t);
  560. }
  561. int ninliers1 = countNonZero(inliers1);
  562. double freq = getTickFrequency();
  563. printf("nfeatures1 = %d, nfeatures2=%d, matches=%d, ninliers(RANSAC)=%d, "
  564. "time(RANSAC)=%.2fmsec, ninliers(RHO)=%d, time(RHO)=%.2fmsec\n",
  565. (int)keypoints_1.size(), (int)keypoints_2.size(),
  566. (int)good_matches.size(), ninliers0, min_t0*1000./freq, ninliers1, min_t1*1000./freq);
  567. ASSERT_TRUE(!H0.empty());
  568. ASSERT_GE(ninliers0, 80);
  569. ASSERT_TRUE(!H1.empty());
  570. ASSERT_GE(ninliers1, 80);
  571. }
  572. TEST(Calib3d_Homography, minPoints)
  573. {
  574. float pt1data[] =
  575. {
  576. 2.80073029e+002f, 2.39591217e+002f, 2.21912201e+002f, 2.59783997e+002f
  577. };
  578. float pt2data[] =
  579. {
  580. 1.84072723e+002f, 1.43591202e+002f, 1.25912483e+002f, 1.63783859e+002f
  581. };
  582. int npoints = (int)(sizeof(pt1data)/sizeof(pt1data[0])/2);
  583. printf("npoints = %d\n", npoints); // npoints = 2
  584. Mat p1(1, npoints, CV_32FC2, pt1data);
  585. Mat p2(1, npoints, CV_32FC2, pt2data);
  586. Mat mask;
  587. // findHomography should raise an error since npoints < MIN_COUNT_OF_POINTS
  588. EXPECT_THROW(findHomography(p1, p2, RANSAC, 0.01, mask), cv::Exception);
  589. }
  590. TEST(Calib3d_Homography, not_normalized)
  591. {
  592. Mat_<double> p1({5, 2}, {-1, -1, -2, -2, -1, 1, -2, 2, -1, 0});
  593. Mat_<double> p2({5, 2}, {0, -1, -1, -1, 0, 0, -1, 0, 0, -0.5});
  594. Mat_<double> ref({3, 3}, {
  595. 0.74276086, 0., 0.74276086,
  596. 0.18569022, 0.18569022, 0.,
  597. -0.37138043, 0., 0.
  598. });
  599. for (int method : std::vector<int>({0, RANSAC, LMEDS}))
  600. {
  601. Mat h = findHomography(p1, p2, method);
  602. for (auto it = h.begin<double>(); it != h.end<double>(); ++it) {
  603. ASSERT_FALSE(cvIsNaN(*it)) << cv::format("method %d\nResult:\n", method) << h;
  604. }
  605. if (h.at<double>(0, 0) * ref.at<double>(0, 0) < 0) {
  606. h *= -1;
  607. }
  608. ASSERT_LE(cv::norm(h, ref, NORM_INF), 1e-8) << cv::format("method %d\nResult:\n", method) << h;
  609. }
  610. }
  611. TEST(Calib3d_Homography, Refine)
  612. {
  613. Mat_<double> p1({10, 2}, {41, -86, -87, 99, 66, -96, -86, -8, -67, 24,
  614. -87, -76, -19, 89, 37, -4, -86, -86, -66, -53});
  615. Mat_<double> p2({10, 2}, {
  616. 0.007723226608700208, -1.177541410622515,
  617. -0.1909072353027552, -0.4247610181930323,
  618. -0.134992319993638, -0.6469949816560389,
  619. -0.3570627451405215, 0.1811469436293486,
  620. -0.3005671881038939, -0.02325733734262935,
  621. -0.4404509481789249, 0.4851526464158342,
  622. 0.6343346428859541, -3.396187657072353,
  623. -0.3539383967092603, 0.1469447227353143,
  624. -0.4526924606856586, 0.5296757109061794,
  625. -0.4309974583614644, 0.4522732662733471
  626. });
  627. hconcat(p1, Mat::ones(p1.rows, 1, CV_64F), p1);
  628. hconcat(p2, Mat::ones(p2.rows, 1, CV_64F), p2);
  629. for(int method : std::vector<int>({0, RANSAC, LMEDS}))
  630. {
  631. Mat h = findHomography(p1, p2, method);
  632. EXPECT_NEAR(h.at<double>(2, 2), 1.0, 1e-7);
  633. Mat proj = p1 * h.t();
  634. proj.col(0) /= proj.col(2);
  635. proj.col(1) /= proj.col(2);
  636. Mat error;
  637. cv::pow(p2.colRange(0, 2) - proj.colRange(0, 2), 2, error);
  638. cv::reduce(error, error, 1, REDUCE_SUM);
  639. cv::reduce(error, error, 0, REDUCE_AVG);
  640. EXPECT_LE(sqrt(error.at<double>(0, 0)), method == LMEDS ? 7e-4 : 7e-5);
  641. }
  642. }
  643. }} // namespace