Combined with OpenCV4.5.2 to use WeChat open source library to identify QR codes

First, the reference link is as follows: https://mp.weixin.qq.com/s/2GZAJAUPHTXFOKDRv-D21Q

Have the following questions:

(1) Wechat_qrcode is only available in the modeule of opencv_contrib4.5.2 and above. Note that there is no version 4.5.1 in the above link!

(2) When using cmake to compile the contribu library, you need to pay attention not to check python, otherwise an error will be reported when the project is generated later.

My version is: opencv4.5.2, opencv_contrib4.5.2, VS2017; (debug+x64 bit)

Here is my compiled file, please take it yourself:

https://download.csdn.net/download/Helloorld_1/87446941

Core module file download address:

https://github.com/WeChatCV/opencv_3rdparty

Here is the code used to test:

#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/wechat_qrcode.hpp>

using namespace std;
using namespace cv;

int main()
{
	//加载图片解码
	Ptr<wechat_qrcode::WeChatQRCode> detector;
	string detect_prototxt = "./model/detect.prototxt";//修改下载的核心文件位置
	string detect_caffe_model = "./model/detect.caffemodel";
	string sr_prototxt = "./model/sr.prototxt";
	string sr_caffe_model = "./model/sr.caffemodel";

	Mat img = imread("QR.jpg");//选择识别含有二维码的图片
	try//用来抛出异常的代码,检查是否能使用核心文件。
	{
		detector = makePtr<wechat_qrcode::WeChatQRCode>(detect_prototxt, detect_caffe_model,
			sr_prototxt, sr_caffe_model);
	}
	catch (const std::exception& e)
	{
		cout <<
			"\n---------------------------------------------------------------\n"
			"Failed to initialize WeChatQRCode.\n"
			"Please, download 'detector.*' and 'sr.*' from\n"
			"https://github.com/WeChatCV/opencv_3rdparty/tree/wechat_qrcode\n"
			"and put them into the current directory.\n"
			"---------------------------------------------------------------\n";
		cout << e.what() << endl;
		return 0;
	}

	vector<Mat> vPoints;
	vector<String> strDecoded;
	strDecoded = detector->detectAndDecode(img, vPoints);//检测二维码,并返回结果,用Vpoints来保存边框信息。
	for (int i = 0; i < strDecoded.size(); i++)
	{
		cout << "decode-" << i + 1 << ": " << strDecoded[i] << endl;
		Point pt1 = Point((int)vPoints[i].at<float>(0, 0), (int)vPoints[i].at<float>(0, 1));
		Point pt2 = Point((int)vPoints[i].at<float>(1, 0), (int)vPoints[i].at<float>(1, 1));
		Point pt3 = Point((int)vPoints[i].at<float>(2, 0), (int)vPoints[i].at<float>(2, 1));
		Point pt4 = Point((int)vPoints[i].at<float>(3, 0), (int)vPoints[i].at<float>(3, 1));
		line(img, pt1, pt2, Scalar(0, 255, 0), 2);
		line(img, pt2, pt3, Scalar(0, 255, 0), 2);
		line(img, pt3, pt4, Scalar(0, 255, 0), 2);
		line(img, pt4, pt1, Scalar(0, 255, 0), 2);
		putText(img, strDecoded[i], pt1, 0, 0.5, Scalar(255, 0, 0), 2);
	}
	imshow("wechat_qrcode", img);
	waitKey();
	//imwrite("result.png", img);
}

Guess you like

Origin blog.csdn.net/Helloorld_1/article/details/129007207