Qt-OpenCV学习笔记--二维码(QR)识别

一、概述

QR码(Quick Response Code) 是二维码的一种,在正方形二位矩阵内通过黑白标识编码二进制位从而编码数据,最早发明用于日本汽车制造业追踪零部件。

二、函数

detect()

功能:

检测图像中的 QR 码并获得包含该代码的四边形。

如果检测失败,或者图像中有多个二维码,则返回值为false。

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

img

载入图像,灰度或者彩色(BGR)

points

获得二维码最小四边形的顶点坐标

decode()

功能:

通过 detect() 检测二维码,然后调用 decode() 解码。

如果无法解码,则返回 UTF8 编码的输出字符串或空字符串。

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

img

包含 QR 码的灰度或彩色 (BGR) 图像。

points

通过 decode() 方法(或其他算法)找到的四边形顶点。

straight_qrcode

获得校正和二值化的二维码图像。

detectAndDecode()

功能:

检测和解码二维码。

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

img

包含 QR 码的灰度或彩色 (BGR) 图像。

points

获得二维码最小四边形的顶点坐标;如果未找到,则为空。

straight_qrcode

获得校正和二值化的二维码图像。

三、测试代码

#include "widget.h"
#include "ui_widget.h"

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/objdetect.hpp>

#include <iostream>
#include <QDebug>

using namespace cv;
using namespace std;

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //载入图像
    Mat src = imread("./photo/ma3.jpg",0);
    //显示图像
    imshow("src",src);

    //创建检测器
    QRCodeDetector detector;

    //检测标识
    bool sign;
    //二维码最小四边形的顶点坐标
    vector<Point> points;
    //检测二维码
    sign = detector.detect(src,points);
    //判断检测是否成功,如果成功,打印顶点坐标
    if(sign)
    {
        for (unsigned int i = 0;i<points.size();i++)
        {
            qDebug() << points[i].x;
            qDebug() << points[i].y;
        }
    }
    else
    {
        qDebug() << "检测失败!";
    }

    //解码
    string infomation1;
    Mat dst;
    infomation1 = detector.decode(src,points,dst);
    imshow("dst",dst);
    //打印解码信息
    QString str1 = QString::fromStdString(infomation1);
    qDebug() << str1;


    //第二种方法:
    string infomation2;
    vector<Point> pt;
    infomation2 = detector.detectAndDecode(src,pt);
    //打印解码信息
    QString str2 = QString::fromStdString(infomation2);
    qDebug() << str2;

}

Widget::~Widget()
{
    delete ui;
}

四、测试结果

五、参考

Opencv之二维码识别---QRCodeDetector

二维码(QR 码)

QR二维码检测

猜你喜欢

转载自blog.csdn.net/ssismm/article/details/128715951