test_qrcode.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. #include "test_precomp.hpp"
  5. namespace opencv_test { namespace {
  6. std::string qrcode_images_name[] = {
  7. "version_1_down.jpg", "version_1_left.jpg", "version_1_right.jpg", "version_1_up.jpg", "version_1_top.jpg",
  8. "version_2_down.jpg", "version_2_left.jpg", "version_2_right.jpg", "version_2_up.jpg", "version_2_top.jpg",
  9. "version_3_down.jpg", "version_3_left.jpg", "version_3_right.jpg", "version_3_up.jpg", "version_3_top.jpg",
  10. "version_4_down.jpg", "version_4_left.jpg", "version_4_right.jpg", "version_4_up.jpg", "version_4_top.jpg",
  11. "version_5_down.jpg", "version_5_left.jpg", "version_5_right.jpg", "version_5_up.jpg", "version_5_top.jpg",
  12. "russian.jpg", "kanji.jpg", "link_github_ocv.jpg", "link_ocv.jpg", "link_wiki_cv.jpg"
  13. };
  14. std::string qrcode_images_close[] = {
  15. "close_1.png", "close_2.png", "close_3.png", "close_4.png", "close_5.png"
  16. };
  17. std::string qrcode_images_monitor[] = {
  18. "monitor_1.png", "monitor_2.png", "monitor_3.png", "monitor_4.png", "monitor_5.png"
  19. };
  20. std::string qrcode_images_curved[] = {
  21. "curved_1.jpg", "curved_2.jpg", "curved_3.jpg", "curved_4.jpg", "curved_5.jpg", "curved_6.jpg", "curved_7.jpg", "curved_8.jpg"
  22. };
  23. std::string qrcode_images_multiple[] = {
  24. "2_qrcodes.png", "3_close_qrcodes.png", "3_qrcodes.png", "4_qrcodes.png",
  25. "5_qrcodes.png", "6_qrcodes.png", "7_qrcodes.png", "8_close_qrcodes.png"
  26. };
  27. //#define UPDATE_QRCODE_TEST_DATA
  28. #ifdef UPDATE_QRCODE_TEST_DATA
  29. TEST(Objdetect_QRCode, generate_test_data)
  30. {
  31. const std::string root = "qrcode/";
  32. const std::string dataset_config = findDataFile(root + "dataset_config.json");
  33. FileStorage file_config(dataset_config, FileStorage::WRITE);
  34. file_config << "test_images" << "[";
  35. size_t images_count = sizeof(qrcode_images_name) / sizeof(qrcode_images_name[0]);
  36. for (size_t i = 0; i < images_count; i++)
  37. {
  38. file_config << "{:" << "image_name" << qrcode_images_name[i];
  39. std::string image_path = findDataFile(root + qrcode_images_name[i]);
  40. std::vector<Point> corners;
  41. Mat src = imread(image_path, IMREAD_GRAYSCALE), straight_barcode;
  42. std::string decoded_info;
  43. ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
  44. EXPECT_TRUE(detectQRCode(src, corners));
  45. #ifdef HAVE_QUIRC
  46. EXPECT_TRUE(decodeQRCode(src, corners, decoded_info, straight_barcode));
  47. #endif
  48. file_config << "x" << "[:";
  49. for (size_t j = 0; j < corners.size(); j++) { file_config << corners[j].x; }
  50. file_config << "]";
  51. file_config << "y" << "[:";
  52. for (size_t j = 0; j < corners.size(); j++) { file_config << corners[j].y; }
  53. file_config << "]";
  54. file_config << "info" << decoded_info;
  55. file_config << "}";
  56. }
  57. file_config << "]";
  58. file_config.release();
  59. }
  60. TEST(Objdetect_QRCode_Close, generate_test_data)
  61. {
  62. const std::string root = "qrcode/close/";
  63. const std::string dataset_config = findDataFile(root + "dataset_config.json");
  64. FileStorage file_config(dataset_config, FileStorage::WRITE);
  65. file_config << "close_images" << "[";
  66. size_t close_count = sizeof(qrcode_images_close) / sizeof(qrcode_images_close[0]);
  67. for (size_t i = 0; i < close_count; i++)
  68. {
  69. file_config << "{:" << "image_name" << qrcode_images_close[i];
  70. std::string image_path = findDataFile(root + qrcode_images_close[i]);
  71. std::vector<Point> corners;
  72. Mat src = imread(image_path, IMREAD_GRAYSCALE), barcode, straight_barcode;
  73. std::string decoded_info;
  74. ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
  75. const double min_side = std::min(src.size().width, src.size().height);
  76. double coeff_expansion = 1024.0 / min_side;
  77. const int width = cvRound(src.size().width * coeff_expansion);
  78. const int height = cvRound(src.size().height * coeff_expansion);
  79. Size new_size(width, height);
  80. resize(src, barcode, new_size, 0, 0, INTER_LINEAR);
  81. EXPECT_TRUE(detectQRCode(barcode, corners));
  82. #ifdef HAVE_QUIRC
  83. EXPECT_TRUE(decodeQRCode(barcode, corners, decoded_info, straight_barcode));
  84. #endif
  85. file_config << "x" << "[:";
  86. for (size_t j = 0; j < corners.size(); j++) { file_config << corners[j].x; }
  87. file_config << "]";
  88. file_config << "y" << "[:";
  89. for (size_t j = 0; j < corners.size(); j++) { file_config << corners[j].y; }
  90. file_config << "]";
  91. file_config << "info" << decoded_info;
  92. file_config << "}";
  93. }
  94. file_config << "]";
  95. file_config.release();
  96. }
  97. TEST(Objdetect_QRCode_Monitor, generate_test_data)
  98. {
  99. const std::string root = "qrcode/monitor/";
  100. const std::string dataset_config = findDataFile(root + "dataset_config.json");
  101. FileStorage file_config(dataset_config, FileStorage::WRITE);
  102. file_config << "monitor_images" << "[";
  103. size_t monitor_count = sizeof(qrcode_images_monitor) / sizeof(qrcode_images_monitor[0]);
  104. for (size_t i = 0; i < monitor_count; i++)
  105. {
  106. file_config << "{:" << "image_name" << qrcode_images_monitor[i];
  107. std::string image_path = findDataFile(root + qrcode_images_monitor[i]);
  108. std::vector<Point> corners;
  109. Mat src = imread(image_path, IMREAD_GRAYSCALE), barcode, straight_barcode;
  110. std::string decoded_info;
  111. ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
  112. const double min_side = std::min(src.size().width, src.size().height);
  113. double coeff_expansion = 1024.0 / min_side;
  114. const int width = cvRound(src.size().width * coeff_expansion);
  115. const int height = cvRound(src.size().height * coeff_expansion);
  116. Size new_size(width, height);
  117. resize(src, barcode, new_size, 0, 0, INTER_LINEAR);
  118. EXPECT_TRUE(detectQRCode(barcode, corners));
  119. #ifdef HAVE_QUIRC
  120. EXPECT_TRUE(decodeQRCode(barcode, corners, decoded_info, straight_barcode));
  121. #endif
  122. file_config << "x" << "[:";
  123. for (size_t j = 0; j < corners.size(); j++) { file_config << corners[j].x; }
  124. file_config << "]";
  125. file_config << "y" << "[:";
  126. for (size_t j = 0; j < corners.size(); j++) { file_config << corners[j].y; }
  127. file_config << "]";
  128. file_config << "info" << decoded_info;
  129. file_config << "}";
  130. }
  131. file_config << "]";
  132. file_config.release();
  133. }
  134. TEST(Objdetect_QRCode_Curved, generate_test_data)
  135. {
  136. const std::string root = "qrcode/curved/";
  137. const std::string dataset_config = findDataFile(root + "dataset_config.json");
  138. FileStorage file_config(dataset_config, FileStorage::WRITE);
  139. file_config << "test_images" << "[";
  140. size_t images_count = sizeof(qrcode_images_curved) / sizeof(qrcode_images_curved[0]);
  141. for (size_t i = 0; i < images_count; i++)
  142. {
  143. file_config << "{:" << "image_name" << qrcode_images_curved[i];
  144. std::string image_path = findDataFile(root + qrcode_images_curved[i]);
  145. std::vector<Point> corners;
  146. Mat src = imread(image_path, IMREAD_GRAYSCALE), straight_barcode;
  147. std::string decoded_info;
  148. ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
  149. EXPECT_TRUE(detectQRCode(src, corners));
  150. #ifdef HAVE_QUIRC
  151. EXPECT_TRUE(decodeCurvedQRCode(src, corners, decoded_info, straight_barcode));
  152. #endif
  153. file_config << "x" << "[:";
  154. for (size_t j = 0; j < corners.size(); j++) { file_config << corners[j].x; }
  155. file_config << "]";
  156. file_config << "y" << "[:";
  157. for (size_t j = 0; j < corners.size(); j++) { file_config << corners[j].y; }
  158. file_config << "]";
  159. file_config << "info" << decoded_info;
  160. file_config << "}";
  161. }
  162. file_config << "]";
  163. file_config.release();
  164. }
  165. TEST(Objdetect_QRCode_Multi, generate_test_data)
  166. {
  167. const std::string root = "qrcode/multiple/";
  168. const std::string dataset_config = findDataFile(root + "dataset_config.json");
  169. FileStorage file_config(dataset_config, FileStorage::WRITE);
  170. file_config << "multiple_images" << "[:";
  171. size_t multiple_count = sizeof(qrcode_images_multiple) / sizeof(qrcode_images_multiple[0]);
  172. for (size_t i = 0; i < multiple_count; i++)
  173. {
  174. file_config << "{:" << "image_name" << qrcode_images_multiple[i];
  175. std::string image_path = findDataFile(root + qrcode_images_multiple[i]);
  176. Mat src = imread(image_path);
  177. ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
  178. std::vector<Point> corners;
  179. QRCodeDetector qrcode;
  180. EXPECT_TRUE(qrcode.detectMulti(src, corners));
  181. #ifdef HAVE_QUIRC
  182. std::vector<cv::String> decoded_info;
  183. std::vector<Mat> straight_barcode;
  184. EXPECT_TRUE(qrcode.decodeMulti(src, corners, decoded_info, straight_barcode));
  185. #endif
  186. file_config << "x" << "[:";
  187. for(size_t j = 0; j < corners.size(); j += 4)
  188. {
  189. file_config << "[:";
  190. for (size_t k = 0; k < 4; k++)
  191. {
  192. file_config << corners[j + k].x;
  193. }
  194. file_config << "]";
  195. }
  196. file_config << "]";
  197. file_config << "y" << "[:";
  198. for(size_t j = 0; j < corners.size(); j += 4)
  199. {
  200. file_config << "[:";
  201. for (size_t k = 0; k < 4; k++)
  202. {
  203. file_config << corners[j + k].y;
  204. }
  205. file_config << "]";
  206. }
  207. file_config << "]";
  208. file_config << "info";
  209. file_config << "[:";
  210. for(size_t j = 0; j < decoded_info.size(); j++)
  211. {
  212. file_config << decoded_info[j];
  213. }
  214. file_config << "]";
  215. file_config << "}";
  216. }
  217. file_config << "]";
  218. file_config.release();
  219. }
  220. #else
  221. typedef testing::TestWithParam< std::string > Objdetect_QRCode;
  222. TEST_P(Objdetect_QRCode, regression)
  223. {
  224. const std::string name_current_image = GetParam();
  225. const std::string root = "qrcode/";
  226. const int pixels_error = 3;
  227. std::string image_path = findDataFile(root + name_current_image);
  228. Mat src = imread(image_path, IMREAD_GRAYSCALE), straight_barcode;
  229. ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
  230. std::vector<Point> corners;
  231. std::string decoded_info;
  232. QRCodeDetector qrcode;
  233. #ifdef HAVE_QUIRC
  234. decoded_info = qrcode.detectAndDecode(src, corners, straight_barcode);
  235. ASSERT_FALSE(corners.empty());
  236. ASSERT_FALSE(decoded_info.empty());
  237. int expected_barcode_type = CV_8UC1;
  238. EXPECT_EQ(expected_barcode_type, straight_barcode.type());
  239. #else
  240. ASSERT_TRUE(qrcode.detect(src, corners));
  241. #endif
  242. const std::string dataset_config = findDataFile(root + "dataset_config.json");
  243. FileStorage file_config(dataset_config, FileStorage::READ);
  244. ASSERT_TRUE(file_config.isOpened()) << "Can't read validation data: " << dataset_config;
  245. {
  246. FileNode images_list = file_config["test_images"];
  247. size_t images_count = static_cast<size_t>(images_list.size());
  248. ASSERT_GT(images_count, 0u) << "Can't find validation data entries in 'test_images': " << dataset_config;
  249. for (size_t index = 0; index < images_count; index++)
  250. {
  251. FileNode config = images_list[(int)index];
  252. std::string name_test_image = config["image_name"];
  253. if (name_test_image == name_current_image)
  254. {
  255. for (int i = 0; i < 4; i++)
  256. {
  257. int x = config["x"][i];
  258. int y = config["y"][i];
  259. EXPECT_NEAR(x, corners[i].x, pixels_error);
  260. EXPECT_NEAR(y, corners[i].y, pixels_error);
  261. }
  262. #ifdef HAVE_QUIRC
  263. std::string original_info = config["info"];
  264. EXPECT_EQ(decoded_info, original_info);
  265. #endif
  266. return; // done
  267. }
  268. }
  269. std::cerr
  270. << "Not found results for '" << name_current_image
  271. << "' image in config file:" << dataset_config << std::endl
  272. << "Re-run tests with enabled UPDATE_QRCODE_TEST_DATA macro to update test data."
  273. << std::endl;
  274. }
  275. }
  276. typedef testing::TestWithParam< std::string > Objdetect_QRCode_Close;
  277. TEST_P(Objdetect_QRCode_Close, regression)
  278. {
  279. const std::string name_current_image = GetParam();
  280. const std::string root = "qrcode/close/";
  281. const int pixels_error = 3;
  282. std::string image_path = findDataFile(root + name_current_image);
  283. Mat src = imread(image_path, IMREAD_GRAYSCALE), barcode, straight_barcode;
  284. ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
  285. const double min_side = std::min(src.size().width, src.size().height);
  286. double coeff_expansion = 1024.0 / min_side;
  287. const int width = cvRound(src.size().width * coeff_expansion);
  288. const int height = cvRound(src.size().height * coeff_expansion);
  289. Size new_size(width, height);
  290. resize(src, barcode, new_size, 0, 0, INTER_LINEAR);
  291. std::vector<Point> corners;
  292. std::string decoded_info;
  293. QRCodeDetector qrcode;
  294. #ifdef HAVE_QUIRC
  295. decoded_info = qrcode.detectAndDecode(barcode, corners, straight_barcode);
  296. ASSERT_FALSE(corners.empty());
  297. ASSERT_FALSE(decoded_info.empty());
  298. int expected_barcode_type = CV_8UC1;
  299. EXPECT_EQ(expected_barcode_type, straight_barcode.type());
  300. #else
  301. ASSERT_TRUE(qrcode.detect(barcode, corners));
  302. #endif
  303. const std::string dataset_config = findDataFile(root + "dataset_config.json");
  304. FileStorage file_config(dataset_config, FileStorage::READ);
  305. ASSERT_TRUE(file_config.isOpened()) << "Can't read validation data: " << dataset_config;
  306. {
  307. FileNode images_list = file_config["close_images"];
  308. size_t images_count = static_cast<size_t>(images_list.size());
  309. ASSERT_GT(images_count, 0u) << "Can't find validation data entries in 'test_images': " << dataset_config;
  310. for (size_t index = 0; index < images_count; index++)
  311. {
  312. FileNode config = images_list[(int)index];
  313. std::string name_test_image = config["image_name"];
  314. if (name_test_image == name_current_image)
  315. {
  316. for (int i = 0; i < 4; i++)
  317. {
  318. int x = config["x"][i];
  319. int y = config["y"][i];
  320. EXPECT_NEAR(x, corners[i].x, pixels_error);
  321. EXPECT_NEAR(y, corners[i].y, pixels_error);
  322. }
  323. #ifdef HAVE_QUIRC
  324. std::string original_info = config["info"];
  325. EXPECT_EQ(decoded_info, original_info);
  326. #endif
  327. return; // done
  328. }
  329. }
  330. std::cerr
  331. << "Not found results for '" << name_current_image
  332. << "' image in config file:" << dataset_config << std::endl
  333. << "Re-run tests with enabled UPDATE_QRCODE_TEST_DATA macro to update test data."
  334. << std::endl;
  335. }
  336. }
  337. typedef testing::TestWithParam< std::string > Objdetect_QRCode_Monitor;
  338. TEST_P(Objdetect_QRCode_Monitor, regression)
  339. {
  340. const std::string name_current_image = GetParam();
  341. const std::string root = "qrcode/monitor/";
  342. const int pixels_error = 3;
  343. std::string image_path = findDataFile(root + name_current_image);
  344. Mat src = imread(image_path, IMREAD_GRAYSCALE), barcode, straight_barcode;
  345. ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
  346. const double min_side = std::min(src.size().width, src.size().height);
  347. double coeff_expansion = 1024.0 / min_side;
  348. const int width = cvRound(src.size().width * coeff_expansion);
  349. const int height = cvRound(src.size().height * coeff_expansion);
  350. Size new_size(width, height);
  351. resize(src, barcode, new_size, 0, 0, INTER_LINEAR);
  352. std::vector<Point> corners;
  353. std::string decoded_info;
  354. QRCodeDetector qrcode;
  355. #ifdef HAVE_QUIRC
  356. decoded_info = qrcode.detectAndDecode(barcode, corners, straight_barcode);
  357. ASSERT_FALSE(corners.empty());
  358. ASSERT_FALSE(decoded_info.empty());
  359. int expected_barcode_type = CV_8UC1;
  360. EXPECT_EQ(expected_barcode_type, straight_barcode.type());
  361. #else
  362. ASSERT_TRUE(qrcode.detect(barcode, corners));
  363. #endif
  364. const std::string dataset_config = findDataFile(root + "dataset_config.json");
  365. FileStorage file_config(dataset_config, FileStorage::READ);
  366. ASSERT_TRUE(file_config.isOpened()) << "Can't read validation data: " << dataset_config;
  367. {
  368. FileNode images_list = file_config["monitor_images"];
  369. size_t images_count = static_cast<size_t>(images_list.size());
  370. ASSERT_GT(images_count, 0u) << "Can't find validation data entries in 'test_images': " << dataset_config;
  371. for (size_t index = 0; index < images_count; index++)
  372. {
  373. FileNode config = images_list[(int)index];
  374. std::string name_test_image = config["image_name"];
  375. if (name_test_image == name_current_image)
  376. {
  377. for (int i = 0; i < 4; i++)
  378. {
  379. int x = config["x"][i];
  380. int y = config["y"][i];
  381. EXPECT_NEAR(x, corners[i].x, pixels_error);
  382. EXPECT_NEAR(y, corners[i].y, pixels_error);
  383. }
  384. #ifdef HAVE_QUIRC
  385. std::string original_info = config["info"];
  386. EXPECT_EQ(decoded_info, original_info);
  387. #endif
  388. return; // done
  389. }
  390. }
  391. std::cerr
  392. << "Not found results for '" << name_current_image
  393. << "' image in config file:" << dataset_config << std::endl
  394. << "Re-run tests with enabled UPDATE_QRCODE_TEST_DATA macro to update test data."
  395. << std::endl;
  396. }
  397. }
  398. typedef testing::TestWithParam< std::string > Objdetect_QRCode_Curved;
  399. TEST_P(Objdetect_QRCode_Curved, regression)
  400. {
  401. const std::string name_current_image = GetParam();
  402. const std::string root = "qrcode/curved/";
  403. const int pixels_error = 3;
  404. std::string image_path = findDataFile(root + name_current_image);
  405. Mat src = imread(image_path, IMREAD_GRAYSCALE), straight_barcode;
  406. ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
  407. std::vector<Point> corners;
  408. std::string decoded_info;
  409. QRCodeDetector qrcode;
  410. #ifdef HAVE_QUIRC
  411. decoded_info = qrcode.detectAndDecodeCurved(src, corners, straight_barcode);
  412. ASSERT_FALSE(corners.empty());
  413. ASSERT_FALSE(decoded_info.empty());
  414. int expected_barcode_type = CV_8UC1;
  415. EXPECT_EQ(expected_barcode_type, straight_barcode.type());
  416. #else
  417. ASSERT_TRUE(qrcode.detect(src, corners));
  418. #endif
  419. const std::string dataset_config = findDataFile(root + "dataset_config.json");
  420. FileStorage file_config(dataset_config, FileStorage::READ);
  421. ASSERT_TRUE(file_config.isOpened()) << "Can't read validation data: " << dataset_config;
  422. {
  423. FileNode images_list = file_config["test_images"];
  424. size_t images_count = static_cast<size_t>(images_list.size());
  425. ASSERT_GT(images_count, 0u) << "Can't find validation data entries in 'test_images': " << dataset_config;
  426. for (size_t index = 0; index < images_count; index++)
  427. {
  428. FileNode config = images_list[(int)index];
  429. std::string name_test_image = config["image_name"];
  430. if (name_test_image == name_current_image)
  431. {
  432. for (int i = 0; i < 4; i++)
  433. {
  434. int x = config["x"][i];
  435. int y = config["y"][i];
  436. EXPECT_NEAR(x, corners[i].x, pixels_error);
  437. EXPECT_NEAR(y, corners[i].y, pixels_error);
  438. }
  439. #ifdef HAVE_QUIRC
  440. std::string original_info = config["info"];
  441. EXPECT_EQ(decoded_info, original_info);
  442. #endif
  443. return; // done
  444. }
  445. }
  446. std::cerr
  447. << "Not found results for '" << name_current_image
  448. << "' image in config file:" << dataset_config << std::endl
  449. << "Re-run tests with enabled UPDATE_QRCODE_TEST_DATA macro to update test data."
  450. << std::endl;
  451. }
  452. }
  453. typedef testing::TestWithParam < std::string > Objdetect_QRCode_Multi;
  454. TEST_P(Objdetect_QRCode_Multi, regression)
  455. {
  456. const std::string name_current_image = GetParam();
  457. const std::string root = "qrcode/multiple/";
  458. const int pixels_error = 3;
  459. std::string image_path = findDataFile(root + name_current_image);
  460. Mat src = imread(image_path);
  461. ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
  462. QRCodeDetector qrcode;
  463. std::vector<Point> corners;
  464. #ifdef HAVE_QUIRC
  465. std::vector<cv::String> decoded_info;
  466. std::vector<Mat> straight_barcode;
  467. EXPECT_TRUE(qrcode.detectAndDecodeMulti(src, decoded_info, corners, straight_barcode));
  468. ASSERT_FALSE(corners.empty());
  469. ASSERT_FALSE(decoded_info.empty());
  470. int expected_barcode_type = CV_8UC1;
  471. for(size_t i = 0; i < straight_barcode.size(); i++)
  472. EXPECT_EQ(expected_barcode_type, straight_barcode[i].type());
  473. #else
  474. ASSERT_TRUE(qrcode.detectMulti(src, corners));
  475. #endif
  476. const std::string dataset_config = findDataFile(root + "dataset_config.json");
  477. FileStorage file_config(dataset_config, FileStorage::READ);
  478. ASSERT_TRUE(file_config.isOpened()) << "Can't read validation data: " << dataset_config;
  479. {
  480. FileNode images_list = file_config["multiple_images"];
  481. size_t images_count = static_cast<size_t>(images_list.size());
  482. ASSERT_GT(images_count, 0u) << "Can't find validation data entries in 'test_images': " << dataset_config;
  483. for (size_t index = 0; index < images_count; index++)
  484. {
  485. FileNode config = images_list[(int)index];
  486. std::string name_test_image = config["image_name"];
  487. if (name_test_image == name_current_image)
  488. {
  489. for(int j = 0; j < int(corners.size()); j += 4)
  490. {
  491. bool ok = false;
  492. for (int k = 0; k < int(corners.size() / 4); k++)
  493. {
  494. int count_eq_points = 0;
  495. for (int i = 0; i < 4; i++)
  496. {
  497. int x = config["x"][k][i];
  498. int y = config["y"][k][i];
  499. if(((abs(corners[j + i].x - x)) <= pixels_error) && ((abs(corners[j + i].y - y)) <= pixels_error))
  500. count_eq_points++;
  501. }
  502. if (count_eq_points == 4)
  503. {
  504. ok = true;
  505. break;
  506. }
  507. }
  508. EXPECT_TRUE(ok);
  509. }
  510. #ifdef HAVE_QUIRC
  511. size_t count_eq_info = 0;
  512. for(int i = 0; i < int(decoded_info.size()); i++)
  513. {
  514. for(int j = 0; j < int(decoded_info.size()); j++)
  515. {
  516. std::string original_info = config["info"][j];
  517. if(original_info == decoded_info[i])
  518. {
  519. count_eq_info++;
  520. break;
  521. }
  522. }
  523. }
  524. EXPECT_EQ(decoded_info.size(), count_eq_info);
  525. #endif
  526. return; // done
  527. }
  528. }
  529. std::cerr
  530. << "Not found results for '" << name_current_image
  531. << "' image in config file:" << dataset_config << std::endl
  532. << "Re-run tests with enabled UPDATE_QRCODE_TEST_DATA macro to update test data."
  533. << std::endl;
  534. }
  535. }
  536. INSTANTIATE_TEST_CASE_P(/**/, Objdetect_QRCode, testing::ValuesIn(qrcode_images_name));
  537. INSTANTIATE_TEST_CASE_P(/**/, Objdetect_QRCode_Close, testing::ValuesIn(qrcode_images_close));
  538. INSTANTIATE_TEST_CASE_P(/**/, Objdetect_QRCode_Monitor, testing::ValuesIn(qrcode_images_monitor));
  539. INSTANTIATE_TEST_CASE_P(/**/, Objdetect_QRCode_Curved, testing::ValuesIn(qrcode_images_curved));
  540. INSTANTIATE_TEST_CASE_P(/**/, Objdetect_QRCode_Multi, testing::ValuesIn(qrcode_images_multiple));
  541. TEST(Objdetect_QRCode_decodeMulti, decode_regression_16491)
  542. {
  543. #ifdef HAVE_QUIRC
  544. Mat zero_image = Mat::zeros(256, 256, CV_8UC1);
  545. Point corners_[] = {Point(16, 16), Point(128, 16), Point(128, 128), Point(16, 128),
  546. Point(16, 16), Point(128, 16), Point(128, 128), Point(16, 128)};
  547. std::vector<Point> vec_corners;
  548. int array_size = 8;
  549. vec_corners.assign(corners_, corners_ + array_size);
  550. std::vector<cv::String> decoded_info;
  551. std::vector<Mat> straight_barcode;
  552. QRCodeDetector vec_qrcode;
  553. EXPECT_NO_THROW(vec_qrcode.decodeMulti(zero_image, vec_corners, decoded_info, straight_barcode));
  554. Mat mat_corners(2, 4, CV_32SC2, (void*)&vec_corners[0]);
  555. QRCodeDetector mat_qrcode;
  556. EXPECT_NO_THROW(mat_qrcode.decodeMulti(zero_image, mat_corners, decoded_info, straight_barcode));
  557. #endif
  558. }
  559. TEST(Objdetect_QRCode_detectMulti, detect_regression_16961)
  560. {
  561. const std::string name_current_image = "9_qrcodes.jpg";
  562. const std::string root = "qrcode/multiple/";
  563. std::string image_path = findDataFile(root + name_current_image);
  564. Mat src = imread(image_path);
  565. ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
  566. QRCodeDetector qrcode;
  567. std::vector<Point> corners;
  568. EXPECT_TRUE(qrcode.detectMulti(src, corners));
  569. ASSERT_FALSE(corners.empty());
  570. size_t expect_corners_size = 36;
  571. EXPECT_EQ(corners.size(), expect_corners_size);
  572. }
  573. TEST(Objdetect_QRCode_decodeMulti, check_output_parameters_type_19363)
  574. {
  575. const std::string name_current_image = "9_qrcodes.jpg";
  576. const std::string root = "qrcode/multiple/";
  577. std::string image_path = findDataFile(root + name_current_image);
  578. Mat src = imread(image_path);
  579. ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
  580. #ifdef HAVE_QUIRC
  581. QRCodeDetector qrcode;
  582. std::vector<Point> corners;
  583. std::vector<cv::String> decoded_info;
  584. #if 0 // FIXIT: OutputArray::create() type check
  585. std::vector<Mat2b> straight_barcode_nchannels;
  586. EXPECT_ANY_THROW(qrcode.detectAndDecodeMulti(src, decoded_info, corners, straight_barcode_nchannels));
  587. #endif
  588. int expected_barcode_type = CV_8UC1;
  589. std::vector<Mat1b> straight_barcode;
  590. EXPECT_TRUE(qrcode.detectAndDecodeMulti(src, decoded_info, corners, straight_barcode));
  591. ASSERT_FALSE(corners.empty());
  592. for(size_t i = 0; i < straight_barcode.size(); i++)
  593. EXPECT_EQ(expected_barcode_type, straight_barcode[i].type());
  594. #endif
  595. }
  596. TEST(Objdetect_QRCode_detect, detect_regression_20882)
  597. {
  598. const std::string name_current_image = "qrcode_near_the_end.jpg";
  599. const std::string root = "qrcode/";
  600. std::string image_path = findDataFile(root + name_current_image);
  601. Mat src = imread(image_path);
  602. ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
  603. QRCodeDetector qrcode;
  604. std::vector<Point> corners;
  605. Mat straight_barcode;
  606. cv::String decoded_info;
  607. EXPECT_TRUE(qrcode.detect(src, corners));
  608. EXPECT_TRUE(!corners.empty());
  609. #ifdef HAVE_QUIRC
  610. EXPECT_NO_THROW(qrcode.decode(src, corners, straight_barcode));
  611. #endif
  612. }
  613. TEST(Objdetect_QRCode_basic, not_found_qrcode)
  614. {
  615. std::vector<Point> corners;
  616. Mat straight_barcode;
  617. std::string decoded_info;
  618. Mat zero_image = Mat::zeros(256, 256, CV_8UC1);
  619. QRCodeDetector qrcode;
  620. EXPECT_FALSE(qrcode.detect(zero_image, corners));
  621. #ifdef HAVE_QUIRC
  622. corners = std::vector<Point>(4);
  623. EXPECT_ANY_THROW(qrcode.decode(zero_image, corners, straight_barcode));
  624. #endif
  625. }
  626. #endif // UPDATE_QRCODE_TEST_DATA
  627. }} // namespace