调用微信二维码识别开源库

4 篇文章 0 订阅
该文提供了OpenCV及opencv_contrib的下载源,指出了编译时CMake路径使用斜杠的注意事项,以及在VS2019中编译生成动态库的过程。之后,讲述了Qt项目中如何配置OpenCV库,并给出了解码QRCode的代码示例,强调了运行时需要将dll文件与exe同目录,以及QImage处理可能影响识别效果的问题。
摘要由CSDN通过智能技术生成

源码下载:

opencv下载:3rdparty · 4.x · OpenCV / opencv · GitCode

opencv_contrib下载:OpenCV / opencv_contrib · GitCode

cnn模型下载:WeChatCV/opencv_3rdparty: OpenCV - 3rdparty (github.com)

编译教程

参考: OpenCV4.5.1新增微信QRCode解码功能使用步骤与测评 附源码和效果视频 (qq.com)

中途遇见错误参考: VS2019在Opencv4.5.3及其contrib安装过程中的错误_xxx52413的博客-CSDN博客_cmake error at cmake/opencvmodule.cmake:356 (_glob

cmake路径要使用斜杠’ / ‘不要使用反斜杠’ \ ’

编译完成使用vs生成动态连接库

Qt调用链接库

pro文件参考设置:

OPENCV_DIR = $$quote(C:\Tool\opencv4.1.2\opencv-4.x)
OPENCV_INCLUDE_PATH = $$OPENCV_DIR\build\install\include
OPENCV_LIBRARY_PATH = $$OPENCV_DIR\build\install\x64\vc16\lib
LIBS += -L$$OPENCV_LIBRARY_PATH
LIBS += -lopencv_world460

解码程序如下:

#include <opencv2/opencv.hpp>
#include <opencv2/wechat_qrcode.hpp>
//以下为识别过程
 	QImage qimg = ui->lb_image->pixmap()->toImage();

    //加载图片解码
    cv::Mat img = QImage2cvMat(qimg);
    //cv::Mat img = cv::imread("C:/Users/11509/Desktop/test.bmp");
    std::string detect_prototxt = "./model/detect.prototxt";
    std::string detect_caffe_model = "./model/detect.caffemodel";
    std::string sr_prototxt = "./model/sr.prototxt";
    std::string sr_caffe_model = "./model/sr.caffemodel";

    Ptr<wechat_qrcode::WeChatQRCode> detector;
    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 ;
    }
    std::cout << "import models success!" << std::endl;
    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();

编译好后的程序如果运行闪退需要将dll文件放在exe同目录下

经过Qimage压缩的图像会更加不好识别

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值