[英]How to decode QRCODE using C++ opencv library on Linux?
我正在使用 opencv c++ 庫來解碼 Qrcode。這里我給出了來自這個網站的示例測試代碼: https://www.learnopencv.com/opencv-qr-code-scanner-c-and-python/
當我編譯這個測試程序時,出現以下錯誤:
test.cc: In function ‘int main(int, char**)’:
test.cc:29:3: error: ‘QRCodeDetector’ was not declared in this scope
QRCodeDetector qrDecoder = QRCodeDetector::QRCodeDetector();
^~~~~~~~~~~~~~
test.cc:33:22: error: ‘qrDecoder’ was not declared in this scope
std::string data = qrDecoder.detectAndDecode(inputImage, bbox, rectifiedImage)
如何解決這個錯誤?
test.cc:
//https://www.learnopencv.com/opencv-qr-code-scanner-c-and-python/
#include <opencv2/objdetect.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
void display(Mat &im, Mat &bbox)
{
int n = bbox.rows;
for(int i = 0 ; i < n ; i++)
{
line(im, Point2i(bbox.at<float>(i,0),bbox.at<float>(i,1)), Point2i(bbox.at<float>((i+1) % n,0), bbox.at<float>((i+1) % n,1)), Scalar(255,0,0), 3);
}
imshow("Result", im);
}
int main(int argc, char* argv[])
{
// Read image
Mat inputImage;
inputImage = imread(argv[1]);
QRCodeDetector qrDecoder = QRCodeDetector::QRCodeDetector();
Mat bbox, rectifiedImage;
std::string data = qrDecoder.detectAndDecode(inputImage, bbox, rectifiedImage);
if(data.length()>0)
{
cout << "Decoded Data : " << data << endl;
display(inputImage, bbox);
rectifiedImage.convertTo(rectifiedImage, CV_8UC3);
imshow("Rectified QRCode", rectifiedImage);
waitKey(0);
}
else
cout << "QR Code not detected" << endl;
}
//compile
g++ test.cc -o test `pkg-config opencv --cflags --libs`
首先,代碼與 OpenCV 4.0 兼容,因此請確保您使用的是 OpenCV 4.0。 如果您使用的是 OpenCV 4.0,您可能在 Eclipse 中引用了不同版本的 OpenCV 路徑。
對於解決方案,有兩個步驟。
第1步
從終端輸入pkg-config --cflags opencv4 。 輸出將類似於I/usr/local/include/opencv4/opencv 。 復制輸出並將其粘貼到第一個鏈接中顯示的位置。
https://drive.google.com/open?id=1WSBEOaSF6JJvOiUSI_kop8wnRaK8TOIt
步驟2
再次從終端輸入pkg-config --libs opencv4 。 輸出將類似於L/usr/local/lib 。 復制輸出並將其粘貼到第二個鏈接中顯示的位置。 比鏈接中所示添加對 Libraries(-l) 部分的標題引用。
https://drive.google.com/open?id=1VYJHNV10P8oj_pwaUh3GZJwWu8vDmUxA
此步驟將解決您的問題。
參考下面的線程(同樣的問題,但有所有細節): https://forum.opencv.org/t/quirc-error-in-opencv-4/6804/3在 Ubuntu 22.04.1
在 linux 上 OpenCV4.* 構建檢測第三方庫的存在,使其使用捆綁的 QUIRC 用於 cmake:-DBUILD_QUIRC=ON -DQUIRC=ON
完整的 cmd 如下:
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local/OpenCV47 \
-D INSTALL_C_EXAMPLES=ON \
-D WITH_TBB=ON \
-D WITH_V4L=ON \
-D WITH_OPENGL=ON \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
-D BUILD_EXAMPLES=ON \
-DBUILD_QUIRC=ON -DQUIRC=ON \
..
以上 cmd 為我工作,其他選項可以根據您的需要進行調整。 --batcilla 在此處輸入代碼
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.