检测并解析二维码

本文介绍使用OpenCV4中的QRCodeDetector类进行二维码检测与解析的方法。通过detectAndDecode函数,可一次性完成二维码区域定位及内容读取,并提供了一个实际运行的例子。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

OpenCV4中负责二维码检测与解析的类是QRCodeDetector
1.负责从图像中找到二维码区域,返回的是二维码四个顶点的坐标。

detect (InputArray img, OutputArray points) const
img参数是输入图像,支持灰度或者彩色
points是vector返回的四个点坐标数组

2.负责解析二维码,返回utf-8字符串作为解析结果,无法解析返回空

decode (InputArray img, InputArray points, OutputArray straight_qrcode=noArray())
img表示输入图像
point表示检测到四个点坐标
straight_qrcode表示解析的二维码ROI

3.一步搞定二维码检测与解析。

detectAndDecode(
InputArray img, //输入图像
OutputArray points=noArray(), // 顶点坐标
OutputArray straight_qrcode=noArray() // ROI

)

在这贴出自己写的一份利用detectAndDecode函数来检测并解析二维码的代码

#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main()
{
    Mat img;
    VideoCapture cap(2);
    double t = 0;
    double fps;
    while(1)
    {
        t = (double)cv::getTickCount();
        cap.read(img);
        cv::QRCodeDetector QRdetecter;
        std::vector<cv::Point> list;
        cv::Mat  res;
        string str = QRdetecter.detectAndDecode(img, list, res);
        std::cout <<  QR: << str << std::endl;
        for (int j = 0; j < list.size(); j++)
        {
            if (j == 3)
                line(img, list[j], list[0], Scalar(0, 255, 0), 2);
            else
                line(img, list[j], list[j + 1], Scalar(0, 255, 0), 2);
        }
        namedWindow(原图, 0);
        imshow(原图, img);
        if (res.data)
        {
            namedWindow(二维码ROI, 0);
            imshow(二维码ROI, res);
        }
        t = ((double)cv::getTickCount() - t) / cv::getTickFrequency();
        fps = 1.0 / t;
        cout<<fps<<endl;
        waitKey(1);
    }
        return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值