简体   繁体   English

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

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

I am working with opencv c++ library to decode Qrcode.Here I given sample test code which is from this website: https://www.learnopencv.com/opencv-qr-code-scanner-c-and-python/我正在使用 opencv c++ 库来解码 Qrcode。这里我给出了来自这个网站的示例测试代码: https://www.learnopencv.com/opencv-qr-code-scanner-c-and-python/

When I am compiling this test program I am getting following error:当我编译这个测试程序时,出现以下错误:

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)

How to resolve this error?如何解决这个错误?

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`

First of all, code is compatible with OpenCV 4.0, so be sure that you are using OpenCV 4.0.首先,代码与 OpenCV 4.0 兼容,因此请确保您使用的是 OpenCV 4.0。 If you are using OpenCV 4.0, you may have refered different version of OpenCV path in eclipse.如果您使用的是 OpenCV 4.0,您可能在 Eclipse 中引用了不同版本的 OpenCV 路径。

For solution, there is two steps.对于解决方案,有两个步骤。

Step1第1步

From Terminal, type pkg-config --cflags opencv4 .从终端输入pkg-config --cflags opencv4 Output will be like I/usr/local/include/opencv4/opencv .输出将类似于I/usr/local/include/opencv4/opencv Copy output and paste it place which is shown in first link.复制输出并将其粘贴到第一个链接中显示的位置。

https://drive.google.com/open?id=1WSBEOaSF6JJvOiUSI_kop8wnRaK8TOIt https://drive.google.com/open?id=1WSBEOaSF6JJvOiUSI_kop8wnRaK8TOIt

Steps2步骤2

Again from terminal, type pkg-config --libs opencv4 .再次从终端输入pkg-config --libs opencv4 Output will be like L/usr/local/lib .输出将类似于L/usr/local/lib Copy output and paste it place which is shown in second link.复制输出并将其粘贴到第二个链接中显示的位置。 Than add headers referances to Libraries(-l) section like shown in link.比链接中所示添加对 Libraries(-l) 部分的标题引用。

https://drive.google.com/open?id=1VYJHNV10P8oj_pwaUh3GZJwWu8vDmUxA https://drive.google.com/open?id=1VYJHNV10P8oj_pwaUh3GZJwWu8vDmUxA

This steps will be solution your problem.此步骤将解决您的问题。

Referring thread below(same problem but has all details): https://forum.opencv.org/t/quirc-error-in-opencv-4/6804/3 On a Ubuntu 22.04.1参考下面的线程(同样的问题,但有所有细节): https://forum.opencv.org/t/quirc-error-in-opencv-4/6804/3在 Ubuntu 22.04.1

On linux OpenCV4.* build detect presence of 3rd party libs, to make it use bundled QUIRC use for cmake: -DBUILD_QUIRC=ON -DQUIRC=ON在 linux 上 OpenCV4.* 构建检测第三方库的存在,使其使用捆绑的 QUIRC 用于 cmake:-DBUILD_QUIRC=ON -DQUIRC=ON

Full cmd be like:完整的 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 \
             ..

Above cmd worked for me, other options can be adjusted to suit your needs.以上 cmd 为我工作,其他选项可以根据您的需要进行调整。 --batcilla enter code here --batcilla 在此处输入代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM