检测并解析二维码
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);elseline(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;
}
總結
- 上一篇: 初识马尔科夫模型(Markov Mode
- 下一篇: 第二季度 QQ 平台生态治理公告:处置违