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