QR二维码检测

本文详细介绍了如何使用OpenCV库在C++中检测和解码QR二维码。通过`detect()`函数定位二维码,`decode()`函数解码信息,以及`detectAndDecode()`函数一站式完成这两步操作。示例代码展示了从读取图像到识别并显示二维码信息的完整流程,包括在图像上绘制二维码边界和显示解码内容。
摘要由CSDN通过智能技术生成

引言

QR二维码在我们的日常生活中非常多见,比如微信和支付宝,火车票和商品标识等。二维码的出现极大地方便了我们的日常·生活,同时也能将信息较为隐蔽地进行传输。

1、定位结果的detect()函数

bool cv::QRCodeDetector::detect(InputArray img, OutputArray points)

img:待检测是否含有QR二维码的灰度图像或者彩色图像

points:包含QR二维码的最小区域四边形的4个顶点坐标,即二维码的4个顶点

2、 定位结果解码的decode()函数

std::string cv::QRCodeDetector::decode(InputArray img, InputArray points, OutputArray straight_qrcode = noArray())

img:含有QR二维码的图像

points:包含QR二维码的最小区域的四边形的四个顶点

straight_qrcode:经过校正和二值化的QR二维码

 3、识别并解码的detectAndDecode()函数

std::string cv::QRCodeDetector::decode(InputArray img, OutputArray points = noArray(), OutputArray straight_qrcode = noArray())

img:含有QR二维码的图像

points:包含QR二维码的最小区域的四边形的四个顶点

straight_qrcode:经过校正和二值化的QR二维码

 4、代码实现

#include <iostream>
#include <opencv2/highgui.hpp>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp> 
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;

int main()
{
	Mat img = imread("F:\\图像处理\\图片\\QR.jpg");
	if (img.empty()) {
		cout << "请确认文件名称是否正确!" << endl;
		return -1;
	}

	Mat gray, qrcode_bin;
	cvtColor(img, gray, COLOR_BGR2GRAY);
	QRCodeDetector qrcodedetector;
	vector<Point> points;
	string information;
	bool isQRcode;
	isQRcode = qrcodedetector.detect(gray, points);	//识别二维码
	if (isQRcode) {
		//解码二维码
		information = qrcodedetector.decode(gray, points, qrcode_bin);
		cout << points << endl;	//输出4个坐标的顶点
	}
	else {
		cout << "无法识别二维码,请确认图像是否含有二维码!" << endl;
		return -1;
	}

	//绘制二维码边框
	for (int i = 0; i < points.size(); i++) {
		if (i == points.size() - 1) {
			line(img, points[i], points[0], Scalar(0, 0, 255), 2, 8);
			break;
		}
		line(img, points[i], points[i + 1], Scalar(0, 0, 255), 2, 8);
	}

	//将解码内容输出到图片上
	putText(img, information.c_str(), Point(20, 30), 0, 1.0, Scalar(0, 0, 255), 2, 8);
	
	//利用函数直接定位二维码并解码
	string information2;
	vector<Point> points2;
	information2 = qrcodedetector.detectAndDecode(gray, points2);
	cout << points2 << endl;
	putText(img, information2.c_str(), Point(20, 55), 0, 1.0, Scalar(0, 0, 0), 2, 8);
	
	//输出结果
	namedWindow("result", WINDOW_NORMAL);
	imshow("result", img);
	namedWindow("qrcode_bin", WINDOW_NORMAL);
	imshow("qrcode_bin", qrcode_bin);
	waitKey(0);

	return 0;
}

5、结果

 QR二维码检测的流程到这里就全部完成了,如果您觉得以上内容对您有所帮助,不妨一键三连,感想您的观看!

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
YOLOv8是一种目标检测模型,可以用于检测图像或视频中的不同目标物体。通过引用中提供的命令"CLI yolo detect predict model=yolov8n.pt source=0 show=True",我们可以使用YOLOv8模型对指定的图像或视频进行目标检测操作。根据引用的描述,即使在低质量的视频和光照条件不佳的情况下,YOLOv8仍然能够准确地捕捉到对象,并且还能够检测到背景中的一些物体。同时,引用提到YOLOv8提供了5种不同大小的模型供选择,开发者可以根据性能和精度之间的平衡来选择适合的模型。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [​目标检测:如何有效实现YOLOv8](https://blog.csdn.net/weixin_38739735/article/details/130676155)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *3* [YOLOv8目标检测快速上手!](https://blog.csdn.net/wjinjie/article/details/130013031)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值