Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/objdetect/test/test_qrcode.cpp
16337 views
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
// #define UPDATE_QRCODE_TEST_DATA
19
#ifdef UPDATE_QRCODE_TEST_DATA
20
21
TEST(Objdetect_QRCode, generate_test_data)
22
{
23
const std::string root = "qrcode/";
24
const std::string dataset_config = findDataFile(root + "dataset_config.json");
25
FileStorage file_config(dataset_config, FileStorage::WRITE);
26
27
file_config << "test_images" << "[";
28
size_t images_count = sizeof(qrcode_images_name) / sizeof(qrcode_images_name[0]);
29
for (size_t i = 0; i < images_count; i++)
30
{
31
file_config << "{:" << "image_name" << qrcode_images_name[i];
32
std::string image_path = findDataFile(root + qrcode_images_name[i]);
33
std::vector<Point> corners;
34
Mat src = imread(image_path, IMREAD_GRAYSCALE), straight_barcode;
35
std::string decoded_info;
36
ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
37
EXPECT_TRUE(detectQRCode(src, corners));
38
#ifdef HAVE_QUIRC
39
EXPECT_TRUE(decodeQRCode(src, corners, decoded_info, straight_barcode));
40
#endif
41
file_config << "x" << "[:";
42
for (size_t j = 0; j < corners.size(); j++) { file_config << corners[j].x; }
43
file_config << "]";
44
file_config << "y" << "[:";
45
for (size_t j = 0; j < corners.size(); j++) { file_config << corners[j].y; }
46
file_config << "]";
47
file_config << "info" << decoded_info;
48
file_config << "}";
49
}
50
file_config << "]";
51
file_config.release();
52
}
53
54
#else
55
56
typedef testing::TestWithParam< std::string > Objdetect_QRCode;
57
TEST_P(Objdetect_QRCode, regression)
58
{
59
const std::string name_current_image = GetParam();
60
const std::string root = "qrcode/";
61
const int pixels_error = 3;
62
63
std::string image_path = findDataFile(root + name_current_image);
64
Mat src = imread(image_path, IMREAD_GRAYSCALE), straight_barcode;
65
ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
66
67
std::vector<Point> corners;
68
std::string decoded_info;
69
ASSERT_TRUE(detectQRCode(src, corners));
70
#ifdef HAVE_QUIRC
71
ASSERT_TRUE(decodeQRCode(src, corners, decoded_info, straight_barcode));
72
#endif
73
74
const std::string dataset_config = findDataFile(root + "dataset_config.json", false);
75
FileStorage file_config(dataset_config, FileStorage::READ);
76
ASSERT_TRUE(file_config.isOpened()) << "Can't read validation data: " << dataset_config;
77
{
78
FileNode images_list = file_config["test_images"];
79
size_t images_count = static_cast<size_t>(images_list.size());
80
ASSERT_GT(images_count, 0u) << "Can't find validation data entries in 'test_images': " << dataset_config;
81
82
for (size_t index = 0; index < images_count; index++)
83
{
84
FileNode config = images_list[(int)index];
85
std::string name_test_image = config["image_name"];
86
if (name_test_image == name_current_image)
87
{
88
for (int i = 0; i < 4; i++)
89
{
90
int x = config["x"][i];
91
int y = config["y"][i];
92
EXPECT_NEAR(x, corners[i].x, pixels_error);
93
EXPECT_NEAR(y, corners[i].y, pixels_error);
94
}
95
96
#ifdef HAVE_QUIRC
97
std::string original_info = config["info"];
98
EXPECT_EQ(decoded_info, original_info);
99
#endif
100
101
return; // done
102
}
103
}
104
std::cerr
105
<< "Not found results for '" << name_current_image
106
<< "' image in config file:" << dataset_config << std::endl
107
<< "Re-run tests with enabled UPDATE_QRCODE_TEST_DATA macro to update test data."
108
<< std::endl;
109
}
110
}
111
112
INSTANTIATE_TEST_CASE_P(/**/, Objdetect_QRCode, testing::ValuesIn(qrcode_images_name));
113
114
115
116
TEST(Objdetect_QRCode_basic, not_found_qrcode)
117
{
118
std::vector<Point> corners;
119
Mat straight_barcode;
120
std::string decoded_info;
121
Mat zero_image = Mat::zeros(256, 256, CV_8UC1);
122
EXPECT_FALSE(detectQRCode(zero_image, corners));
123
#ifdef HAVE_QUIRC
124
corners = std::vector<Point>(4);
125
EXPECT_ANY_THROW(decodeQRCode(zero_image, corners, decoded_info, straight_barcode));
126
#endif
127
}
128
129
130
131
#endif // UPDATE_QRCODE_TEST_DATA
132
133
}} // namespace
134
135