联合OpenCV4.5.2利用微信开源的库识别二维码

首先参考链接如下:https://mp.weixin.qq.com/s/2GZAJAUPHTXFOKDRv-D21Q

有以下问题:

(1)只有opencv_contrib4.5.2及其以上版本的modeule中才有wechat_qrcode,注意按照上述链接中4.5.1的版本是没有的哟!

(2)利用cmake去编译contribu库时,需要注意,不要勾选python,不然后面生成项目时,会报错的。

我的使用版本是:opencv4.5.2,opencv_contrib4.5.2,VS2017;(debug+x64位)

这里是我编译好的文件,请自取:

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

核心模块文件下载地址:

https://github.com/WeChatCV/opencv_3rdparty

以下是用来测试的代码:

#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);
}

猜你喜欢

转载自blog.csdn.net/Helloorld_1/article/details/129007207