源码下载:
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)
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压缩的图像会更加不好识别