test_fast_hough_transform.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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) 2015, Smart Engines Ltd, all rights reserved.
  14. // Copyright (C) 2015, Institute for Information Transmission Problems of the Russian Academy of Sciences (Kharkevich Institute), all rights reserved.
  15. // Copyright (C) 2015, Dmitry Nikolaev, Simon Karpenko, Michail Aliev, Elena Kuznetsova, 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. //----------------------utils---------------------------------------------------
  46. template <typename T> struct Eps
  47. {
  48. static T get() { return 1; }
  49. };
  50. template <> struct Eps<float> { static float get() { return float(1e-3); } };
  51. template <> struct Eps<double> { static double get() { return 1e-6; } };
  52. template <typename T> struct MinPos
  53. {
  54. static T get() { return Eps<T>::get(); }
  55. };
  56. template <typename T> struct Max { static T get()
  57. {
  58. return saturate_cast<T>(numeric_limits<T>::max()); }
  59. };
  60. template <typename T> struct Rand
  61. {
  62. static T get(T _min = MinPos<T>::get(), T _max = Max<T>::get())
  63. {
  64. RNG& rng = TS::ptr()->get_rng();
  65. return saturate_cast<T>(rng.uniform(int(std::max(MinPos<T>::get(),
  66. _min)),
  67. int(std::min(Max<T>::get(),
  68. _max))));
  69. }
  70. };
  71. template <> struct Rand <float>
  72. {
  73. static float get(float _min = MinPos<float>::get(),
  74. float _max = Max<float>::get())
  75. {
  76. RNG& rng = TS::ptr()->get_rng();
  77. return rng.uniform(std::max(MinPos<float>::get(), _min),
  78. std::min(Max<float>::get(), _max));
  79. }
  80. };
  81. template <> struct Rand <double>
  82. {
  83. static double get(double _min = MinPos<double>::get(),
  84. double _max = Max<double>::get())
  85. {
  86. RNG& rng = TS::ptr()->get_rng();
  87. return rng.uniform(std::max(MinPos<double>::get(), _min),
  88. std::min(Max<double>::get(), _max));
  89. }
  90. };
  91. template <typename T> struct Eq
  92. {
  93. static bool get(T a, T b)
  94. {
  95. return a < b ? b - a < Eps<T>::get() : a - b < Eps<T>::get();
  96. }
  97. };
  98. //----------------------TestFHT-------------------------------------------------
  99. class TestFHT
  100. {
  101. public:
  102. TestFHT() : ts(TS::ptr()) {}
  103. void run_n_tests(int depth,
  104. int channels,
  105. int pts_count,
  106. int n_per_test);
  107. private:
  108. template <typename T>
  109. int run_n_tests_t(int depth,
  110. int channels,
  111. int pts_count,
  112. int n_per_test);
  113. template <typename T>
  114. int run_test(int depth,
  115. int channels,
  116. int pts_count);
  117. template <typename T>
  118. int put_random_points(Mat &img,
  119. int count,
  120. vector<Point> &pts);
  121. int run_func(Mat const&src,
  122. Mat& fht);
  123. template <typename T>
  124. int validate_test_results(Mat const &fht,
  125. Mat const &src,
  126. vector<Point> const& pts);
  127. template <typename T> int validate_sum(Mat const& src, Mat const& fht);
  128. int validate_point(Mat const& fht, vector<Point> const &pts);
  129. int validate_line(Mat const& fht, Mat const& src, vector<Point> const& pts);
  130. private:
  131. TS *ts;
  132. };
  133. template <typename T>
  134. int TestFHT::put_random_points(Mat &img, int count, vector<Point> &pts)
  135. {
  136. int code = TS::OK;
  137. pts.resize(count, Point(-1, -1));
  138. for (int i = 0; i < count; ++i)
  139. {
  140. RNG rng = ts->get_rng();
  141. Point const pt(rng.uniform(0, img.cols),
  142. rng.uniform(0, img.rows));
  143. pts[i] = pt;
  144. for (int c = 0; c < img.channels(); ++c)
  145. {
  146. T color = Rand<T>::get(MinPos<T>::get(),
  147. T(Max<T>::get() / count));
  148. T *img_line = (T*)(img.data + img.step * pt.y);
  149. img_line[pt.x * img.channels() + c] = color;
  150. }
  151. }
  152. return code;
  153. }
  154. template <typename T>
  155. int TestFHT::validate_sum(Mat const& src, Mat const& fht)
  156. {
  157. int const channels = src.channels();
  158. if (fht.channels() != channels)
  159. return TS::FAIL_BAD_ARG_CHECK;
  160. vector<Mat> src_channels(channels);
  161. split(src, src_channels);
  162. vector<Mat> fht_channels(channels);
  163. split(fht, fht_channels);
  164. for (int c = 0; c < channels; ++c)
  165. {
  166. T const src_sum = saturate_cast<T>(sum(src_channels[c]).val[0]);
  167. for (int y = 0; y < fht.rows; ++y)
  168. {
  169. T const fht_sum = saturate_cast<T>(sum(fht_channels[c].row(y)).val[0]);
  170. if (!Eq<T>::get(src_sum, fht_sum))
  171. {
  172. ts->printf(TS::LOG,
  173. "The sum of column #%d of channel #%d of the fast "
  174. "hough transform result and the sum of source image"
  175. " mismatch (=%g, should be =%g)\n",
  176. y, c, (float)fht_sum, (float)src_sum);
  177. return TS::FAIL_BAD_ACCURACY;
  178. }
  179. }
  180. }
  181. return TS::OK;
  182. }
  183. int TestFHT::validate_point(Mat const& fht,
  184. vector<Point> const &pts)
  185. {
  186. if (pts.empty())
  187. return TS::OK;
  188. for (size_t i = 1; i < pts.size(); ++i)
  189. {
  190. if (pts[0] != pts[i])
  191. return TS::OK;
  192. }
  193. int const channels = fht.channels();
  194. vector<Mat> fht_channels(channels);
  195. split(fht, fht_channels);
  196. for (int c = 0; c < channels; ++c)
  197. {
  198. for (int y = 0; y < fht.rows; ++y)
  199. {
  200. int cnt = countNonZero(fht_channels[c].row(y));
  201. if (cnt != 1)
  202. {
  203. ts->printf(TS::LOG,
  204. "The incorrect count of non-zero values in column "
  205. "#%d, channel #%d of FastHoughTransform result "
  206. "image (=%d, should be %d)\n",
  207. y, c, cnt, 1);
  208. return TS::FAIL_BAD_ACCURACY;
  209. }
  210. }
  211. }
  212. return TS::OK;
  213. }
  214. static const double MAX_LDIST = 2.0;
  215. int TestFHT::validate_line(Mat const& fht,
  216. Mat const& src,
  217. vector<Point> const& pts)
  218. {
  219. size_t const size = (int)pts.size();
  220. if (size < 2)
  221. return TS::OK;
  222. size_t first_pt_i = 0, second_pt_i = 1;
  223. for (size_t i = first_pt_i + 1; i < size; ++i)
  224. {
  225. if (pts[i] != pts[first_pt_i])
  226. {
  227. second_pt_i = first_pt_i;
  228. break;
  229. }
  230. }
  231. if (pts[second_pt_i] == pts[first_pt_i])
  232. return TS::OK;
  233. for (size_t i = second_pt_i + 1; i < size; ++i)
  234. {
  235. if (pts[i] != pts[second_pt_i])
  236. return TS::OK;
  237. }
  238. const Point &f = pts[first_pt_i];
  239. const Point &s = pts[second_pt_i];
  240. int const channels = fht.channels();
  241. vector<Mat> fht_channels(channels);
  242. split(fht, fht_channels);
  243. for (int ch = 0; ch < channels; ++ch)
  244. {
  245. Point fht_max(-1, -1);
  246. minMaxLoc(fht_channels[ch], 0, 0, 0, &fht_max);
  247. Vec4i src_line = HoughPoint2Line(fht_max, src,
  248. ARO_315_135, HDO_DESKEW, RO_STRICT);
  249. double const a = src_line[1] - src_line[3];
  250. double const b = src_line[2] - src_line[0];
  251. double const c = - (a * src_line[0] + b * src_line[1]);
  252. double const fd = abs(f.x * a + f.y * b + c) / sqrt(a * a + b * b);
  253. double const sd = abs(s.x * a + s.y * b + c) / sqrt(a * a + b * b);
  254. double const dist = std::max(fd, sd);
  255. if (dist > MAX_LDIST)
  256. {
  257. ts->printf(TS::LOG,
  258. "Failed to detect max line in channels %d (distance "
  259. "between point and line correspoinding of maximum in "
  260. "FastHoughTransform space is #%g)\n", ch, dist);
  261. return TS::FAIL_BAD_ACCURACY;
  262. }
  263. }
  264. return TS::OK;
  265. }
  266. template <typename T>
  267. int TestFHT::validate_test_results(Mat const &fht,
  268. Mat const &src,
  269. vector<Point> const& pts)
  270. {
  271. int code = validate_sum<T>(src, fht);
  272. if (code == TS::OK)
  273. code = validate_point(fht, pts);
  274. if (code == TS::OK)
  275. code = validate_line(fht, src, pts);
  276. return code;
  277. }
  278. int TestFHT::run_func(Mat const&src,
  279. Mat& fht)
  280. {
  281. int code = TS::OK;
  282. FastHoughTransform(src, fht, src.depth());
  283. return code;
  284. }
  285. static Size random_size(int const max_size_log,
  286. int const elem_size)
  287. {
  288. RNG& rng = TS::ptr()->get_rng();
  289. return randomSize(rng, std::max(1,
  290. max_size_log - cvRound(log(double(elem_size)))));
  291. }
  292. static const int FHT_MAX_SIZE_LOG = 9;
  293. template <typename T>
  294. int TestFHT::run_test(int depth,
  295. int channels,
  296. int pts_count)
  297. {
  298. int code = TS::OK;
  299. Size size = random_size(FHT_MAX_SIZE_LOG,
  300. CV_ELEM_SIZE(CV_MAKE_TYPE(depth, channels)));
  301. Mat src = Mat::zeros(size, CV_MAKETYPE(depth, channels));
  302. vector<Point> pts;
  303. code = put_random_points<T>(src, pts_count, pts);
  304. if (code != TS::OK)
  305. return code;
  306. Mat fht;
  307. code = run_func(src, fht);
  308. if (code != TS::OK)
  309. return code;
  310. code = validate_test_results<T>(fht, src, pts);
  311. return code;
  312. }
  313. void TestFHT::run_n_tests(int depth,
  314. int channels,
  315. int pts_count,
  316. int n)
  317. {
  318. try
  319. {
  320. int code = TS::OK;
  321. switch (depth)
  322. {
  323. case CV_8U:
  324. code = run_n_tests_t<uchar>(depth, channels, pts_count, n);
  325. break;
  326. case CV_8S:
  327. code = run_n_tests_t<schar>(depth, channels, pts_count, n);
  328. break;
  329. case CV_16U:
  330. code = run_n_tests_t<ushort>(depth, channels, pts_count, n);
  331. break;
  332. case CV_16S:
  333. code = run_n_tests_t<short>(depth, channels, pts_count, n);
  334. break;
  335. case CV_32S:
  336. code = run_n_tests_t<int>(depth, channels, pts_count, n);
  337. break;
  338. case CV_32F:
  339. code = run_n_tests_t<float>(depth, channels, pts_count, n);
  340. break;
  341. case CV_64F:
  342. code = run_n_tests_t<double>(depth, channels, pts_count, n);
  343. break;
  344. default:
  345. code = TS::FAIL_BAD_ARG_CHECK;
  346. ts->printf(TS::LOG, "Unknown depth %d\n", depth);
  347. break;
  348. }
  349. if (code != TS::OK)
  350. throw TS::FailureCode(code);
  351. }
  352. catch (const TS::FailureCode& fc)
  353. {
  354. std::string errorStr = TS::str_from_code(fc);
  355. ts->printf(TS::LOG,
  356. "General failure:\n\t%s (%d)\n", errorStr.c_str(), fc);
  357. ts->set_failed_test_info(fc);
  358. }
  359. catch(...)
  360. {
  361. ts->printf(TS::LOG, "Unknown failure\n");
  362. ts->set_failed_test_info(TS::FAIL_EXCEPTION);
  363. }
  364. }
  365. template <typename T>
  366. int TestFHT::run_n_tests_t(int depth,
  367. int channels,
  368. int pts_count,
  369. int n)
  370. {
  371. int code = TS::OK;
  372. for (int iTest = 0; iTest < n; ++iTest)
  373. {
  374. code = run_test<T>(depth, channels, pts_count);
  375. if (code != TS::OK)
  376. {
  377. ts->printf(TS::LOG, "Test %d failed with code %d\n", iTest, code);
  378. break;
  379. }
  380. }
  381. return code;
  382. }
  383. //----------------------TEST_P--------------------------------------------------
  384. typedef tuple<int, int, int, int> Depth_Channels_PtsC_nPerTest;
  385. typedef TestWithParam<Depth_Channels_PtsC_nPerTest> FastHoughTransformTest;
  386. TEST_P(FastHoughTransformTest, accuracy)
  387. {
  388. int const depth = get<0>(GetParam());
  389. int const channels = get<1>(GetParam());
  390. int const pts_count = get<2>(GetParam());
  391. int const n_per_test = get<3>(GetParam());
  392. TestFHT testFht;
  393. testFht.run_n_tests(depth, channels, pts_count, n_per_test);
  394. }
  395. #define FHT_ALL_DEPTHS CV_8U, CV_16U, CV_32S, CV_32F, CV_64F
  396. #define FHT_ALL_CHANNELS 1, 3, 4
  397. INSTANTIATE_TEST_CASE_P(FullSet, FastHoughTransformTest,
  398. Combine(Values(FHT_ALL_DEPTHS),
  399. Values(FHT_ALL_CHANNELS),
  400. Values(1, 2),
  401. Values(5)));
  402. #undef FHT_ALL_DEPTHS
  403. #undef FHT_ALL_CHANNELS
  404. }} // namespace