如何在 Linux 上使用 opencv 库解码 QRCODE C++

How to decode QRCODE using C++ opencv library on Linux?

本文关键字:opencv 解码 QRCODE C++ Linux      更新时间:2023-10-16

我正在使用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。复制输出并将其粘贴到第二个链接中显示的位置。比添加标题引用到库(-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

完整命令如下所示:

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 
             ..

上面的命令对我有用;可以调整其他选项以满足您的需求。