前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >OpenCV4.5.1新增微信QRCode解码功能使用步骤与测评 附源码和效果视频

OpenCV4.5.1新增微信QRCode解码功能使用步骤与测评 附源码和效果视频

作者头像
Color Space
发布2021-02-07 10:11:17
6K8
发布2021-02-07 10:11:17
举报
导读:

微信开源了QRCode解码功能,并可以在OpenCV中使用,本期将介绍使用步骤和效果演示。

使用步骤:

WeChatQRCode模块为OpenCV4.5.1新增功能,需要在github下载最新opencv源码master和contrib部分编译后使用。

源码编译使用CMake,相关编译教程很多博客写的很详细,这里只做关键步骤说明:

(1) 编译是勾选BUILD_opencv_wechat_qrcode选项(默认勾选)

(2) 勾选BUILD_opencv_world 这样只生成一个dll方便使用

(3) 先Configure,后Generate,直至不报错(警告可忽略)

(4) 打开OpenCV.Sln,先生成ALLBuild,再仅用于项目生成Install,生成成功(无错误,无失败)会得到如下文件:

在include/opencv2文件夹下会多一个wechat_qrcode.hpp

bin文件夹下只生成opencv_world451.dll


接下来就是配置步骤:包含目录、库目录、附加依赖项和环境变量,这个大家用这么久OpenCV应该都会了,这里跳过。

最后上代码:

代码语言:javascript
复制
#include "pch.h"
#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/T33/result.bmp");
  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);
  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);

核心函数strDecoded = detector->detectAndDecode(img, vPoints);可以得到QRCode位置和解码内容,使用到的模型下载地址如下: https://github.com/WeChatCV/opencv_3rdparty

加载两张图像测试效果:

视频实时测试效果:

使用体验:非常适用APP开发者手机端扫码使用,如果是工业应用还需要自己做预处理和增强等步骤。大家有兴趣可以自己尝试。

觉得有用,麻烦给个赞和在看↓↓↓

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-02-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 OpenCV与AI深度学习 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档