test_descriptors.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. // Intel License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000, Intel Corporation, all rights reserved.
  14. // Third party copyrights are property of their respective owners.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification,
  17. // are permitted provided that the following conditions are met:
  18. //
  19. // * Redistribution's of source code must retain the above copyright notice,
  20. // this list of conditions and the following disclaimer.
  21. //
  22. // * Redistribution's in binary form must reproduce the above copyright notice,
  23. // this list of conditions and the following disclaimer in the documentation
  24. // and/or other materials provided with the distribution.
  25. //
  26. // * The name of Intel Corporation may not be used to endorse or promote products
  27. // derived from this software without specific prior written permission.
  28. //
  29. // This software is provided by the copyright holders and contributors "as is" and
  30. // any express or implied warranties, including, but not limited to, the implied
  31. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. // In no event shall the Intel Corporation or contributors be liable for any direct,
  33. // indirect, incidental, special, exemplary, or consequential damages
  34. // (including, but not limited to, procurement of substitute goods or services;
  35. // loss of use, data, or profits; or business interruption) however caused
  36. // and on any theory of liability, whether in contract, strict liability,
  37. // or tort (including negligence or otherwise) arising in any way out of
  38. // the use of this software, even if advised of the possibility of such damage.
  39. //
  40. //M*/
  41. #include "test_precomp.hpp"
  42. namespace opencv_test { namespace {
  43. class CV_DescriptorBaseTest : public cvtest::BaseTest
  44. {
  45. public:
  46. CV_DescriptorBaseTest();
  47. ~CV_DescriptorBaseTest();
  48. protected:
  49. virtual void imageTransformation(const Mat &img1, const Mat &img2, Mat &out1, Mat &out2) = 0;
  50. virtual void imageTransformation(const Mat &img1, Mat &out1) = 0;
  51. void testROI(const Mat &img);
  52. void testMonotonicity(const Mat &img, Mat &out);
  53. void run(int );
  54. Mat censusImage[2];
  55. Mat censusImageSingle[2];
  56. Mat left;
  57. Mat right;
  58. int kernel_size, descriptor_type;
  59. };
  60. //we test to see if the descriptor applied on a roi
  61. //has the same value with the descriptor from the original image
  62. //tested at the roi boundaries
  63. void CV_DescriptorBaseTest::testROI(const Mat &img)
  64. {
  65. int pt, pb,w,h;
  66. //initialize random values for the roi top and bottom
  67. pt = rand() % 100;
  68. pb = rand() % 100;
  69. //calculate the new width and height
  70. w = img.cols;
  71. h = img.rows - pt - pb;
  72. int start = pt + kernel_size / 2 + 1;
  73. int stop = h - kernel_size/2 - 1;
  74. //set the region of interest according to above values
  75. Rect region_of_interest = Rect(0, pt, w, h);
  76. Mat image_roi1 = img(region_of_interest);
  77. Mat p1,p2;
  78. //create 2 images where to put our output
  79. p1.create(image_roi1.rows, image_roi1.cols, CV_32SC4);
  80. p2.create(img.rows, img.cols, CV_32SC4);
  81. imageTransformation(image_roi1,p1);
  82. imageTransformation(img,p2);
  83. int *roi_data = (int *)p1.data;
  84. int *img_data = (int *)p2.data;
  85. //verify result
  86. for(int i = start; i < stop; i++)
  87. {
  88. for(int j = 0; j < w ; j++)
  89. {
  90. if(roi_data[(i - pt) * w + j] != img_data[(i) * w + j])
  91. {
  92. ts->printf(cvtest::TS::LOG, "Something wrong with ROI \n");
  93. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);
  94. return;
  95. }
  96. }
  97. }
  98. }
  99. CV_DescriptorBaseTest::~CV_DescriptorBaseTest()
  100. {
  101. left.release();
  102. right.release();
  103. censusImage[0].release();
  104. censusImage[1].release();
  105. censusImageSingle[0].release();
  106. censusImageSingle[1].release();
  107. }
  108. CV_DescriptorBaseTest::CV_DescriptorBaseTest()
  109. {
  110. //read 2 images from file
  111. left = imread(ts->get_data_path() + "stereomatching/datasets/tsukuba/im2.png", IMREAD_GRAYSCALE);
  112. right = imread(ts->get_data_path() + "stereomatching/datasets/tsukuba/im6.png", IMREAD_GRAYSCALE);
  113. if(left.empty() || right.empty())
  114. {
  115. ts->printf(cvtest::TS::LOG, "Wrong input data \n");
  116. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
  117. return;
  118. }
  119. ts->printf(cvtest::TS::LOG, "Data loaded \n");
  120. }
  121. //verify if we don't have an image with all pixels the same( except when all input pixels are equal)
  122. void CV_DescriptorBaseTest::testMonotonicity(const Mat &img, Mat &out)
  123. {
  124. //verify if input data is correct
  125. if(img.rows != out.rows || img.cols != out.cols || img.empty() || out.empty())
  126. {
  127. ts->printf(cvtest::TS::LOG, "Wrong input / output dimension \n");
  128. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
  129. return;
  130. }
  131. //verify that for an input image with different pxels the values of the
  132. //output pixels are not the same
  133. int same = 0;
  134. uint8_t *data = img.data;
  135. uint8_t val = data[1];
  136. int stride = (int)img.step;
  137. for(int i = 0 ; i < img.rows && !same; i++)
  138. {
  139. for(int j = 0; j < img.cols; j++)
  140. {
  141. if(val != data[i * stride + j])
  142. {
  143. same = 1;
  144. break;
  145. }
  146. }
  147. }
  148. int value_descript = out.data[1];
  149. int accept = 0;
  150. uint8_t *outData = out.data;
  151. for(int i = 0 ; i < img.rows && !accept; i++)
  152. {
  153. for(int j = 0; j < img.cols; j++)
  154. {
  155. //we verify for the output image if the iage pixels are not all the same of an input
  156. //image with different pixels
  157. if(value_descript != outData[i * stride + j] && same)
  158. {
  159. //if we found a value that is different we accept
  160. accept = 1;
  161. break;
  162. }
  163. }
  164. }
  165. if(accept == 1 && same == 0)
  166. {
  167. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);
  168. ts->printf(cvtest::TS::LOG, "The image has all values the same \n");
  169. return;
  170. }
  171. if(accept == 0 && same == 1)
  172. {
  173. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);
  174. ts->printf(cvtest::TS::LOG, "For correct image we get all descriptor values the same \n");
  175. return;
  176. }
  177. ts->set_failed_test_info(cvtest::TS::OK);
  178. }
  179. ///////////////////////////////////
  180. //census transform
  181. class CV_CensusTransformTest: public CV_DescriptorBaseTest
  182. {
  183. public:
  184. CV_CensusTransformTest();
  185. protected:
  186. void imageTransformation(const Mat &img1, const Mat &img2, Mat &out1, Mat &out2);
  187. void imageTransformation(const Mat &img1, Mat &out1);
  188. };
  189. CV_CensusTransformTest::CV_CensusTransformTest()
  190. {
  191. kernel_size = 11;
  192. descriptor_type = CV_SPARSE_CENSUS;
  193. }
  194. void CV_CensusTransformTest::imageTransformation(const Mat &img1, const Mat &img2, Mat &out1, Mat &out2)
  195. {
  196. //verify if input data is correct
  197. if(img1.rows != out1.rows || img1.cols != out1.cols || img1.empty() || out1.empty()
  198. || img2.rows != out2.rows || img2.cols != out2.cols || img2.empty() || out2.empty())
  199. {
  200. ts->printf(cvtest::TS::LOG, "Wrong input / output data \n");
  201. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
  202. return;
  203. }
  204. if(kernel_size % 2 == 0)
  205. {
  206. ts->printf(cvtest::TS::LOG, "Wrong kernel size;Kernel should be odd \n");
  207. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
  208. return;
  209. }
  210. censusTransform(img1,img2,kernel_size,out1,out2,descriptor_type);
  211. }
  212. void CV_CensusTransformTest::imageTransformation(const Mat &img1, Mat &out1)
  213. {
  214. //verify if input data is correct
  215. if(img1.rows != out1.rows || img1.cols != out1.cols || img1.empty() || out1.empty())
  216. {
  217. ts->printf(cvtest::TS::LOG, "Wrong input / output data \n");
  218. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
  219. return;
  220. }
  221. if(kernel_size % 2 == 0)
  222. {
  223. ts->printf(cvtest::TS::LOG, "Wrong kernel size;Kernel should be odd \n");
  224. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
  225. return;
  226. }
  227. censusTransform(img1,kernel_size,out1,descriptor_type);
  228. }
  229. //////////////////////////////////
  230. //symetric census
  231. class CV_SymetricCensusTest: public CV_DescriptorBaseTest
  232. {
  233. public:
  234. CV_SymetricCensusTest();
  235. protected:
  236. void imageTransformation(const Mat &img1, const Mat &img2, Mat &out1, Mat &out2);
  237. void imageTransformation(const Mat &img1, Mat &out1);
  238. };
  239. CV_SymetricCensusTest::CV_SymetricCensusTest()
  240. {
  241. kernel_size = 7;
  242. descriptor_type = CV_CS_CENSUS;
  243. }
  244. void CV_SymetricCensusTest::imageTransformation(const Mat &img1, const Mat &img2, Mat &out1, Mat &out2)
  245. {
  246. //verify if input data is correct
  247. if(img1.rows != out1.rows || img1.cols != out1.cols || img1.empty() || out1.empty()
  248. || img2.rows != out2.rows || img2.cols != out2.cols || img2.empty() || out2.empty())
  249. {
  250. ts->printf(cvtest::TS::LOG, "Wrong input / output data \n");
  251. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
  252. return;
  253. }
  254. if(kernel_size % 2 == 0)
  255. {
  256. ts->printf(cvtest::TS::LOG, "Wrong kernel size;Kernel should be odd \n");
  257. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
  258. return;
  259. }
  260. symetricCensusTransform(img1,img2,kernel_size,out1,out2,descriptor_type);
  261. }
  262. void CV_SymetricCensusTest::imageTransformation(const Mat &img1, Mat &out1)
  263. {
  264. //verify if input data is correct
  265. if(img1.rows != out1.rows || img1.cols != out1.cols || img1.empty() || out1.empty())
  266. {
  267. ts->printf(cvtest::TS::LOG, "Wrong input / output data \n");
  268. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
  269. return;
  270. }
  271. if(kernel_size % 2 == 0)
  272. {
  273. ts->printf(cvtest::TS::LOG, "Wrong kernel size;Kernel should be odd \n");
  274. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
  275. return;
  276. }
  277. symetricCensusTransform(img1,kernel_size,out1,descriptor_type);
  278. }
  279. //////////////////////////////////
  280. //modified census transform
  281. class CV_ModifiedCensusTransformTest: public CV_DescriptorBaseTest
  282. {
  283. public:
  284. CV_ModifiedCensusTransformTest();
  285. protected:
  286. void imageTransformation(const Mat &img1, const Mat &img2, Mat &out1, Mat &out2);
  287. void imageTransformation(const Mat &img1, Mat &out1);
  288. };
  289. CV_ModifiedCensusTransformTest::CV_ModifiedCensusTransformTest()
  290. {
  291. kernel_size = 9;
  292. descriptor_type = CV_MODIFIED_CENSUS_TRANSFORM;
  293. }
  294. void CV_ModifiedCensusTransformTest::imageTransformation(const Mat &img1, const Mat &img2, Mat &out1, Mat &out2)
  295. {
  296. //verify if input data is correct
  297. if(img1.rows != out1.rows || img1.cols != out1.cols || img1.empty() || out1.empty()
  298. || img2.rows != out2.rows || img2.cols != out2.cols || img2.empty() || out2.empty())
  299. {
  300. ts->printf(cvtest::TS::LOG, "Wrong input / output data \n");
  301. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
  302. return;
  303. }
  304. if(kernel_size % 2 == 0)
  305. {
  306. ts->printf(cvtest::TS::LOG, "Wrong kernel size;Kernel should be odd \n");
  307. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
  308. return;
  309. }
  310. modifiedCensusTransform(img1,img2,kernel_size,out1,out2,descriptor_type);
  311. }
  312. void CV_ModifiedCensusTransformTest::imageTransformation(const Mat &img1, Mat &out1)
  313. {
  314. if(img1.rows != out1.rows || img1.cols != out1.cols || img1.empty() || out1.empty())
  315. {
  316. ts->printf(cvtest::TS::LOG, "Wrong input / output data \n");
  317. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
  318. return;
  319. }
  320. if(kernel_size % 2 == 0)
  321. {
  322. ts->printf(cvtest::TS::LOG, "Wrong kernel size;Kernel should be odd \n");
  323. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
  324. return;
  325. }
  326. modifiedCensusTransform(img1,kernel_size,out1,descriptor_type);
  327. }
  328. //////////////////////////////////
  329. //star kernel census
  330. class CV_StarKernelCensusTest: public CV_DescriptorBaseTest
  331. {
  332. public:
  333. CV_StarKernelCensusTest();
  334. protected:
  335. void imageTransformation(const Mat &img1, const Mat &img2, Mat &out1, Mat &out2);
  336. void imageTransformation(const Mat &img1, Mat &out1);
  337. };
  338. CV_StarKernelCensusTest :: CV_StarKernelCensusTest()
  339. {
  340. kernel_size = 9;
  341. descriptor_type = CV_STAR_KERNEL;
  342. }
  343. void CV_StarKernelCensusTest :: imageTransformation(const Mat &img1, const Mat &img2, Mat &out1, Mat &out2)
  344. {
  345. //verify if input data is correct
  346. if(img1.rows != out1.rows || img1.cols != out1.cols || img1.empty() || out1.empty()
  347. || img2.rows != out2.rows || img2.cols != out2.cols || img2.empty() || out2.empty())
  348. {
  349. ts->printf(cvtest::TS::LOG, "Wrong input / output data \n");
  350. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
  351. return;
  352. }
  353. if(kernel_size % 2 == 0)
  354. {
  355. ts->printf(cvtest::TS::LOG, "Wrong kernel size;Kernel should be odd \n");
  356. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
  357. return;
  358. }
  359. starCensusTransform(img1,img2,kernel_size,out1,out2);
  360. }
  361. void CV_StarKernelCensusTest::imageTransformation(const Mat &img1, Mat &out1)
  362. {
  363. if(img1.rows != out1.rows || img1.cols != out1.cols || img1.empty() || out1.empty())
  364. {
  365. ts->printf(cvtest::TS::LOG, "Wrong input / output data \n");
  366. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
  367. return;
  368. }
  369. if(kernel_size % 2 == 0)
  370. {
  371. ts->printf(cvtest::TS::LOG, "Wrong kernel size;Kernel should be odd \n");
  372. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
  373. return;
  374. }
  375. starCensusTransform(img1,kernel_size,out1);
  376. }
  377. void CV_DescriptorBaseTest::run(int )
  378. {
  379. if (left.empty() || right.empty())
  380. {
  381. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
  382. ts->printf(cvtest::TS::LOG, "No input images detected\n");
  383. return;
  384. }
  385. testROI(left);
  386. censusImage[0].create(left.rows, left.cols, CV_32SC4);
  387. censusImage[1].create(left.rows, left.cols, CV_32SC4);
  388. censusImageSingle[0].create(left.rows, left.cols, CV_32SC4);
  389. censusImageSingle[1].create(left.rows, left.cols, CV_32SC4);
  390. censusImage[0].setTo(0);
  391. censusImage[1].setTo(0);
  392. censusImageSingle[0].setTo(0);
  393. censusImageSingle[1].setTo(0);
  394. imageTransformation(left, right, censusImage[0], censusImage[1]);
  395. imageTransformation(left, censusImageSingle[0]);
  396. imageTransformation(right, censusImageSingle[1]);
  397. testMonotonicity(left,censusImage[0]);
  398. testMonotonicity(right,censusImage[1]);
  399. testMonotonicity(left,censusImageSingle[0]);
  400. testMonotonicity(right,censusImageSingle[1]);
  401. if (censusImage[0].empty() || censusImage[1].empty() || censusImageSingle[0].empty() || censusImageSingle[1].empty())
  402. {
  403. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);
  404. ts->printf(cvtest::TS::LOG, "The descriptor images are empty \n");
  405. return;
  406. }
  407. int *datl1 = (int *)censusImage[0].data;
  408. int *datr1 = (int *)censusImage[1].data;
  409. int *datl2 = (int *)censusImageSingle[0].data;
  410. int *datr2 = (int *)censusImageSingle[1].data;
  411. for(int i = 0; i < censusImage[0].rows - kernel_size/ 2; i++)
  412. {
  413. for(int j = 0; j < censusImage[0].cols; j++)
  414. {
  415. if(datl1[i * censusImage[0].cols + j] != datl2[i * censusImage[0].cols + j])
  416. {
  417. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);
  418. ts->printf(cvtest::TS::LOG, "Mismatch for left images %d \n",descriptor_type);
  419. return;
  420. }
  421. if(datr1[i * censusImage[0].cols + j] != datr2[i * censusImage[0].cols + j])
  422. {
  423. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);
  424. ts->printf(cvtest::TS::LOG, "Mismatch for right images %d \n",descriptor_type);
  425. return;
  426. }
  427. }
  428. }
  429. int min = std::numeric_limits<int>::min();
  430. int max = std::numeric_limits<int>::max();
  431. //check if all values are between int min and int max and not NAN
  432. if (0 != cvtest::check(censusImage[0], min, max, 0))
  433. {
  434. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
  435. return;
  436. }
  437. //check if all values are between int min and int max and not NAN
  438. if (0 != cvtest::check(censusImage[1], min, max, 0))
  439. {
  440. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
  441. return ;
  442. }
  443. }
  444. TEST(DISABLED_census_transform_testing, accuracy) { CV_CensusTransformTest test; test.safe_run(); }
  445. TEST(DISABLED_symetric_census_testing, accuracy) { CV_SymetricCensusTest test; test.safe_run(); }
  446. TEST(DISABLED_Dmodified_census_testing, accuracy) { CV_ModifiedCensusTransformTest test; test.safe_run(); }
  447. TEST(DISABLED_Dstar_kernel_testing, accuracy) { CV_StarKernelCensusTest test; test.safe_run(); }
  448. }} // namespace