Opencv4.5.1 微信二维码识别功能 QRCode 编译与使用 C++

17 篇文章 0 订阅

 

日常生活中手机摄像头最常用的功能可能就是扫码了,而且大部分使用微信扫码,微信扫码的功能的确很强大,精度高、速度快。微信扫码近来加入了opencv开源项目了。现特地进行了尝试,并记录了整个过程,以上动图就是实际效果。可以看出在不同距离、视角、光线环境下都表现优异,普通笔记本电脑release模式下能够达到在70FPS左右。

以下则是记录步骤。

一、下载源码

wechat_qrcode包含在opencv_contrib中,因此,需要将该部分联合进行编译。

https://github.com/opencv

 

二、源码编译

① cmake配置

在cmake中添加源码编译路径和生成路径,第一次点击configure。

② 添加contrib路径

第一次configure后,找到OPENCV_EXTRA_MODULES_PATH,将contrib路径添加进来,注意要具体到modules/,继续configure。

如果没有产生错误,则,点击Generate。

稍等,完成后,点击Open Project,或build文件夹下打开.sln。

③ 生成解决方案

1)VS中,先选择编译目标模式,再选中ALL_BUILD,生成。

如果发生错误,则需要查看具体错误原因,进行修改。

2)再仅生成INSTALL。

则,在生成的install文件夹下生成以下项目。

 

三、建立WeChat_QR工程

1)在VS中新建微信二维码识别工程

2)将上个步骤的include路径和.lib配置到项目属性中,

3)添加代码,如下

#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 = "C:/D/opencv451/WeChat_lib/detect.prototxt";
	string detect_caffe_model = "C:/D/opencv451/WeChat_lib/detect.caffemodel";
	string sr_prototxt = "C:/D/opencv451/WeChat_lib/sr.prototxt";
	string sr_caffe_model = "C:/D/opencv451/WeChat_lib/sr.caffemodel";

	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;

	VideoCapture capture;
	capture.open(0, CAP_DSHOW);
	if (!capture.isOpened())
	{
		cout << "can't open camera" << endl;
		exit(-1);
	}
	Mat img;
	
	while (capture.read(img))
	{
		double start = getTickCount();

		strDecoded = detector->detectAndDecode(img, 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);
		}

		double end = getTickCount();
		double run_time = (end - start) / getTickFrequency();
		double fps = 1 / run_time;
		putText(img, format("FPS: %0.2f", fps), Point(20, 20), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 255), 1, 8);
		imshow("wechat_qrcode", img);

		char ch = waitKey(1);
		if (ch == 27)break;
	}
	waitKey();

	return 0;
}

4)视频效果如前文

:部分参考:   https://mp.weixin.qq.com/s/2GZAJAUPHTXFOKDRv-D21Q

  • 10
    点赞
  • 52
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值