Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/samples/cpp/live_detect_qrcode.cpp
16337 views
1
#include "opencv2/objdetect.hpp"
2
#include "opencv2/imgproc.hpp"
3
#include "opencv2/highgui.hpp"
4
#include <string>
5
#include <iostream>
6
7
using namespace std;
8
using namespace cv;
9
10
void getMatWithQRCodeContour(Mat &color_image, vector<Point> transform);
11
void getMatWithFPS(Mat &color_image, double fps);
12
int liveQRCodeDetect();
13
int showImageQRCodeDetect(string in, string out);
14
15
int main(int argc, char *argv[])
16
{
17
const string keys =
18
"{h help ? | | print help messages }"
19
"{i in | | input path to file for detect (with parameter - show image, otherwise - camera)}"
20
"{o out | | output path to file (save image, work with -i parameter) }";
21
CommandLineParser cmd_parser(argc, argv, keys);
22
23
cmd_parser.about("This program detects the QR-codes from camera or images using the OpenCV library.");
24
if (cmd_parser.has("help"))
25
{
26
cmd_parser.printMessage();
27
return 0;
28
}
29
30
string in_file_name = cmd_parser.get<string>("in"); // input path to image
31
string out_file_name = cmd_parser.get<string>("out"); // output path to image
32
33
if (!cmd_parser.check())
34
{
35
cmd_parser.printErrors();
36
return -1;
37
}
38
39
int return_code = 0;
40
if (in_file_name.empty())
41
{
42
return_code = liveQRCodeDetect();
43
}
44
else
45
{
46
return_code = showImageQRCodeDetect(in_file_name, out_file_name);
47
}
48
return return_code;
49
}
50
51
void getMatWithQRCodeContour(Mat &color_image, vector<Point> transform)
52
{
53
if (!transform.empty())
54
{
55
double show_radius = (color_image.rows > color_image.cols)
56
? (2.813 * color_image.rows) / color_image.cols
57
: (2.813 * color_image.cols) / color_image.rows;
58
double contour_radius = show_radius * 0.4;
59
60
vector< vector<Point> > contours;
61
contours.push_back(transform);
62
drawContours(color_image, contours, 0, Scalar(211, 0, 148), cvRound(contour_radius));
63
64
RNG rng(1000);
65
for (size_t i = 0; i < 4; i++)
66
{
67
Scalar color = Scalar(rng.uniform(0,255), rng.uniform(0, 255), rng.uniform(0, 255));
68
circle(color_image, transform[i], cvRound(show_radius), color, -1);
69
}
70
}
71
}
72
73
void getMatWithFPS(Mat &color_image, double fps)
74
{
75
ostringstream convert;
76
convert << cvRound(fps) << " FPS.";
77
putText(color_image, convert.str(), Point(25, 25), FONT_HERSHEY_DUPLEX, 1, Scalar(0, 0, 255), 2);
78
}
79
80
int liveQRCodeDetect()
81
{
82
VideoCapture cap(0);
83
if(!cap.isOpened())
84
{
85
cout << "Cannot open a camera" << '\n';
86
return -4;
87
}
88
89
TickMeter total;
90
for(;;)
91
{
92
Mat frame, src, straight_barcode;
93
string decode_info;
94
vector<Point> transform;
95
cap >> frame;
96
if(frame.empty()) { break; }
97
cvtColor(frame, src, COLOR_BGR2GRAY);
98
99
total.start();
100
bool result_detection = detectQRCode(src, transform);
101
if (result_detection)
102
{
103
bool result_decode = decodeQRCode(src, transform, decode_info, straight_barcode);
104
if (result_decode) { cout << decode_info << '\n'; }
105
}
106
total.stop();
107
double fps = 1 / total.getTimeSec();
108
total.reset();
109
110
if (result_detection) { getMatWithQRCodeContour(frame, transform); }
111
getMatWithFPS(frame, fps);
112
113
imshow("Live detect QR code", frame);
114
if( waitKey(30) > 0 ) { break; }
115
}
116
return 0;
117
}
118
119
int showImageQRCodeDetect(string in, string out)
120
{
121
Mat src = imread(in, IMREAD_GRAYSCALE), straight_barcode;
122
string decode_info;
123
vector<Point> transform;
124
const int count_experiments = 10;
125
double transform_time = 0.0;
126
bool result_detection = false, result_decode = false;
127
TickMeter total;
128
for (size_t i = 0; i < count_experiments; i++)
129
{
130
total.start();
131
transform.clear();
132
result_detection = detectQRCode(src, transform);
133
total.stop();
134
transform_time += total.getTimeSec();
135
total.reset();
136
if (!result_detection) { break; }
137
138
total.start();
139
result_decode = decodeQRCode(src, transform, decode_info, straight_barcode);
140
total.stop();
141
transform_time += total.getTimeSec();
142
total.reset();
143
if (!result_decode) { break; }
144
145
}
146
double fps = count_experiments / transform_time;
147
if (!result_detection) { cout << "Not find QR-code." << '\n'; return -2; }
148
if (!result_decode) { cout << "Not decode QR-code." << '\n'; return -3; }
149
150
Mat color_src = imread(in);
151
getMatWithQRCodeContour(color_src, transform);
152
getMatWithFPS(color_src, fps);
153
154
for(;;)
155
{
156
imshow("Detect QR code on image", color_src);
157
if( waitKey(30) > 0 ) { break; }
158
}
159
160
if (!out.empty())
161
{
162
getMatWithQRCodeContour(color_src, transform);
163
getMatWithFPS(color_src, fps);
164
165
cout << "Input image file path: " << in << '\n';
166
cout << "Output image file path: " << out << '\n';
167
cout << "Size: " << color_src.size() << '\n';
168
cout << "FPS: " << fps << '\n';
169
cout << "Decode info: " << decode_info << '\n';
170
171
vector<int> compression_params;
172
compression_params.push_back(IMWRITE_PNG_COMPRESSION);
173
compression_params.push_back(9);
174
try
175
{
176
imwrite(out, color_src, compression_params);
177
}
178
catch (cv::Exception& ex)
179
{
180
cout << "Exception converting image to PNG format: ";
181
cout << ex.what() << '\n';
182
return -3;
183
}
184
}
185
return 0;
186
}
187
188