mirror of https://github.com/opencv/opencv.git
Open Source Computer Vision Library
https://opencv.org/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
187 lines
5.4 KiB
187 lines
5.4 KiB
6 years ago
|
#include "opencv2/objdetect.hpp"
|
||
|
#include "opencv2/imgproc.hpp"
|
||
|
#include "opencv2/highgui.hpp"
|
||
|
#include <string>
|
||
|
#include <iostream>
|
||
|
|
||
|
using namespace std;
|
||
|
using namespace cv;
|
||
|
|
||
6 years ago
|
static void drawQRCodeContour(Mat &color_image, vector<Point> transform);
|
||
|
static void drawFPS(Mat &color_image, double fps);
|
||
|
static int liveQRCodeDetect(const string& out_file);
|
||
|
static int imageQRCodeDetect(const string& in_file, const string& out_file);
|
||
6 years ago
|
|
||
|
int main(int argc, char *argv[])
|
||
|
{
|
||
|
const string keys =
|
||
|
"{h help ? | | print help messages }"
|
||
|
"{i in | | input path to file for detect (with parameter - show image, otherwise - camera)}"
|
||
|
"{o out | | output path to file (save image, work with -i parameter) }";
|
||
|
CommandLineParser cmd_parser(argc, argv, keys);
|
||
|
|
||
|
cmd_parser.about("This program detects the QR-codes from camera or images using the OpenCV library.");
|
||
|
if (cmd_parser.has("help"))
|
||
|
{
|
||
|
cmd_parser.printMessage();
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
string in_file_name = cmd_parser.get<string>("in"); // input path to image
|
||
6 years ago
|
string out_file_name;
|
||
|
if (cmd_parser.has("out"))
|
||
|
out_file_name = cmd_parser.get<string>("out"); // output path to image
|
||
6 years ago
|
|
||
|
if (!cmd_parser.check())
|
||
|
{
|
||
|
cmd_parser.printErrors();
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
int return_code = 0;
|
||
|
if (in_file_name.empty())
|
||
|
{
|
||
6 years ago
|
return_code = liveQRCodeDetect(out_file_name);
|
||
6 years ago
|
}
|
||
|
else
|
||
|
{
|
||
6 years ago
|
return_code = imageQRCodeDetect(samples::findFile(in_file_name), out_file_name);
|
||
6 years ago
|
}
|
||
|
return return_code;
|
||
|
}
|
||
|
|
||
6 years ago
|
void drawQRCodeContour(Mat &color_image, vector<Point> transform)
|
||
6 years ago
|
{
|
||
|
if (!transform.empty())
|
||
|
{
|
||
|
double show_radius = (color_image.rows > color_image.cols)
|
||
|
? (2.813 * color_image.rows) / color_image.cols
|
||
|
: (2.813 * color_image.cols) / color_image.rows;
|
||
|
double contour_radius = show_radius * 0.4;
|
||
|
|
||
|
vector< vector<Point> > contours;
|
||
|
contours.push_back(transform);
|
||
|
drawContours(color_image, contours, 0, Scalar(211, 0, 148), cvRound(contour_radius));
|
||
|
|
||
|
RNG rng(1000);
|
||
|
for (size_t i = 0; i < 4; i++)
|
||
|
{
|
||
|
Scalar color = Scalar(rng.uniform(0,255), rng.uniform(0, 255), rng.uniform(0, 255));
|
||
|
circle(color_image, transform[i], cvRound(show_radius), color, -1);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
void drawFPS(Mat &color_image, double fps)
|
||
6 years ago
|
{
|
||
|
ostringstream convert;
|
||
6 years ago
|
convert << cvRound(fps) << " FPS (QR detection)";
|
||
6 years ago
|
putText(color_image, convert.str(), Point(25, 25), FONT_HERSHEY_DUPLEX, 1, Scalar(0, 0, 255), 2);
|
||
|
}
|
||
|
|
||
6 years ago
|
int liveQRCodeDetect(const string& out_file)
|
||
6 years ago
|
{
|
||
|
VideoCapture cap(0);
|
||
|
if(!cap.isOpened())
|
||
|
{
|
||
6 years ago
|
cout << "Cannot open a camera" << endl;
|
||
6 years ago
|
return -4;
|
||
|
}
|
||
|
|
||
6 years ago
|
QRCodeDetector qrcode;
|
||
6 years ago
|
TickMeter total;
|
||
|
for(;;)
|
||
|
{
|
||
6 years ago
|
Mat frame, src, straight_barcode;
|
||
|
string decode_info;
|
||
6 years ago
|
vector<Point> transform;
|
||
|
cap >> frame;
|
||
6 years ago
|
if (frame.empty())
|
||
|
{
|
||
|
cout << "End of video stream" << endl;
|
||
|
break;
|
||
|
}
|
||
6 years ago
|
cvtColor(frame, src, COLOR_BGR2GRAY);
|
||
|
|
||
|
total.start();
|
||
6 years ago
|
bool result_detection = qrcode.detect(src, transform);
|
||
6 years ago
|
if (result_detection)
|
||
|
{
|
||
6 years ago
|
decode_info = qrcode.decode(src, transform, straight_barcode);
|
||
6 years ago
|
if (!decode_info.empty()) { cout << decode_info << endl; }
|
||
6 years ago
|
}
|
||
6 years ago
|
total.stop();
|
||
|
double fps = 1 / total.getTimeSec();
|
||
|
total.reset();
|
||
|
|
||
6 years ago
|
if (result_detection) { drawQRCodeContour(frame, transform); }
|
||
|
drawFPS(frame, fps);
|
||
6 years ago
|
|
||
6 years ago
|
imshow("Live QR code detector", frame);
|
||
6 years ago
|
char c = (char)waitKey(30);
|
||
|
if (c == 27)
|
||
|
break;
|
||
|
if (c == ' ' && !out_file.empty())
|
||
|
imwrite(out_file, frame); // TODO write original frame too
|
||
6 years ago
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
6 years ago
|
int imageQRCodeDetect(const string& in_file, const string& out_file)
|
||
6 years ago
|
{
|
||
6 years ago
|
Mat color_src = imread(in_file, IMREAD_COLOR), src;
|
||
|
cvtColor(color_src, src, COLOR_BGR2GRAY);
|
||
|
Mat straight_barcode;
|
||
6 years ago
|
string decoded_info;
|
||
6 years ago
|
vector<Point> transform;
|
||
|
const int count_experiments = 10;
|
||
|
double transform_time = 0.0;
|
||
6 years ago
|
bool result_detection = false;
|
||
6 years ago
|
TickMeter total;
|
||
6 years ago
|
QRCodeDetector qrcode;
|
||
6 years ago
|
for (size_t i = 0; i < count_experiments; i++)
|
||
|
{
|
||
|
total.start();
|
||
|
transform.clear();
|
||
6 years ago
|
result_detection = qrcode.detect(src, transform);
|
||
6 years ago
|
total.stop();
|
||
|
transform_time += total.getTimeSec();
|
||
6 years ago
|
total.reset();
|
||
6 years ago
|
if (!result_detection)
|
||
|
continue;
|
||
6 years ago
|
|
||
|
total.start();
|
||
6 years ago
|
decoded_info = qrcode.decode(src, transform, straight_barcode);
|
||
6 years ago
|
total.stop();
|
||
|
transform_time += total.getTimeSec();
|
||
6 years ago
|
total.reset();
|
||
|
}
|
||
|
double fps = count_experiments / transform_time;
|
||
6 years ago
|
if (!result_detection)
|
||
|
cout << "QR code not found" << endl;
|
||
|
if (decoded_info.empty())
|
||
|
cout << "QR code cannot be decoded" << endl;
|
||
6 years ago
|
|
||
6 years ago
|
drawQRCodeContour(color_src, transform);
|
||
|
drawFPS(color_src, fps);
|
||
6 years ago
|
|
||
6 years ago
|
cout << "Input image file path: " << in_file << endl;
|
||
|
cout << "Output image file path: " << out_file << endl;
|
||
|
cout << "Size: " << color_src.size() << endl;
|
||
|
cout << "FPS: " << fps << endl;
|
||
|
cout << "Decoded info: " << decoded_info << endl;
|
||
|
|
||
|
if (!out_file.empty())
|
||
6 years ago
|
{
|
||
6 years ago
|
imwrite(out_file, color_src);
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
for(;;)
|
||
6 years ago
|
{
|
||
6 years ago
|
imshow("Detect QR code on image", color_src);
|
||
|
if (waitKey(0) == 27)
|
||
|
break;
|
||
6 years ago
|
}
|
||
|
return 0;
|
||
|
}
|