Path: blob/master/samples/cpp/live_detect_qrcode.cpp
16337 views
#include "opencv2/objdetect.hpp"1#include "opencv2/imgproc.hpp"2#include "opencv2/highgui.hpp"3#include <string>4#include <iostream>56using namespace std;7using namespace cv;89void getMatWithQRCodeContour(Mat &color_image, vector<Point> transform);10void getMatWithFPS(Mat &color_image, double fps);11int liveQRCodeDetect();12int showImageQRCodeDetect(string in, string out);1314int main(int argc, char *argv[])15{16const string keys =17"{h help ? | | print help messages }"18"{i in | | input path to file for detect (with parameter - show image, otherwise - camera)}"19"{o out | | output path to file (save image, work with -i parameter) }";20CommandLineParser cmd_parser(argc, argv, keys);2122cmd_parser.about("This program detects the QR-codes from camera or images using the OpenCV library.");23if (cmd_parser.has("help"))24{25cmd_parser.printMessage();26return 0;27}2829string in_file_name = cmd_parser.get<string>("in"); // input path to image30string out_file_name = cmd_parser.get<string>("out"); // output path to image3132if (!cmd_parser.check())33{34cmd_parser.printErrors();35return -1;36}3738int return_code = 0;39if (in_file_name.empty())40{41return_code = liveQRCodeDetect();42}43else44{45return_code = showImageQRCodeDetect(in_file_name, out_file_name);46}47return return_code;48}4950void getMatWithQRCodeContour(Mat &color_image, vector<Point> transform)51{52if (!transform.empty())53{54double show_radius = (color_image.rows > color_image.cols)55? (2.813 * color_image.rows) / color_image.cols56: (2.813 * color_image.cols) / color_image.rows;57double contour_radius = show_radius * 0.4;5859vector< vector<Point> > contours;60contours.push_back(transform);61drawContours(color_image, contours, 0, Scalar(211, 0, 148), cvRound(contour_radius));6263RNG rng(1000);64for (size_t i = 0; i < 4; i++)65{66Scalar color = Scalar(rng.uniform(0,255), rng.uniform(0, 255), rng.uniform(0, 255));67circle(color_image, transform[i], cvRound(show_radius), color, -1);68}69}70}7172void getMatWithFPS(Mat &color_image, double fps)73{74ostringstream convert;75convert << cvRound(fps) << " FPS.";76putText(color_image, convert.str(), Point(25, 25), FONT_HERSHEY_DUPLEX, 1, Scalar(0, 0, 255), 2);77}7879int liveQRCodeDetect()80{81VideoCapture cap(0);82if(!cap.isOpened())83{84cout << "Cannot open a camera" << '\n';85return -4;86}8788TickMeter total;89for(;;)90{91Mat frame, src, straight_barcode;92string decode_info;93vector<Point> transform;94cap >> frame;95if(frame.empty()) { break; }96cvtColor(frame, src, COLOR_BGR2GRAY);9798total.start();99bool result_detection = detectQRCode(src, transform);100if (result_detection)101{102bool result_decode = decodeQRCode(src, transform, decode_info, straight_barcode);103if (result_decode) { cout << decode_info << '\n'; }104}105total.stop();106double fps = 1 / total.getTimeSec();107total.reset();108109if (result_detection) { getMatWithQRCodeContour(frame, transform); }110getMatWithFPS(frame, fps);111112imshow("Live detect QR code", frame);113if( waitKey(30) > 0 ) { break; }114}115return 0;116}117118int showImageQRCodeDetect(string in, string out)119{120Mat src = imread(in, IMREAD_GRAYSCALE), straight_barcode;121string decode_info;122vector<Point> transform;123const int count_experiments = 10;124double transform_time = 0.0;125bool result_detection = false, result_decode = false;126TickMeter total;127for (size_t i = 0; i < count_experiments; i++)128{129total.start();130transform.clear();131result_detection = detectQRCode(src, transform);132total.stop();133transform_time += total.getTimeSec();134total.reset();135if (!result_detection) { break; }136137total.start();138result_decode = decodeQRCode(src, transform, decode_info, straight_barcode);139total.stop();140transform_time += total.getTimeSec();141total.reset();142if (!result_decode) { break; }143144}145double fps = count_experiments / transform_time;146if (!result_detection) { cout << "Not find QR-code." << '\n'; return -2; }147if (!result_decode) { cout << "Not decode QR-code." << '\n'; return -3; }148149Mat color_src = imread(in);150getMatWithQRCodeContour(color_src, transform);151getMatWithFPS(color_src, fps);152153for(;;)154{155imshow("Detect QR code on image", color_src);156if( waitKey(30) > 0 ) { break; }157}158159if (!out.empty())160{161getMatWithQRCodeContour(color_src, transform);162getMatWithFPS(color_src, fps);163164cout << "Input image file path: " << in << '\n';165cout << "Output image file path: " << out << '\n';166cout << "Size: " << color_src.size() << '\n';167cout << "FPS: " << fps << '\n';168cout << "Decode info: " << decode_info << '\n';169170vector<int> compression_params;171compression_params.push_back(IMWRITE_PNG_COMPRESSION);172compression_params.push_back(9);173try174{175imwrite(out, color_src, compression_params);176}177catch (cv::Exception& ex)178{179cout << "Exception converting image to PNG format: ";180cout << ex.what() << '\n';181return -3;182}183}184return 0;185}186187188