首页 > 解决方案 > 如何在 Linux 上使用 C++ opencv 库解码 QRCODE?

问题描述

我正在使用 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`

标签: c++linuxopencvqr-code

解决方案


首先,代码与 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

此步骤将解决您的问题。


推荐阅读