'Detect QRCode with OpenCV
Im trying to use OpenCV to detect a QR-Code in a Image. As an Example, i downloaded a basic QR-Code from here: https://de.wikipedia.org/wiki/Datei:QRCodeWikipedia.png, or the direct link is this: https://upload.wikimedia.org/wikipedia/commons/c/cb/QRCodeWikipedia.png
What i did is downloading OpenCV 4.5.5. for Windows directly here: https://opencv.org/releases/, and add the include/lib dirs into my project.
Now, my code wont detect the QR-Code, and then wont decode it. But if i hold my phone over it, it detect it without problems, so i guess the QRCode itself is ok.
The imshow at the end also works fine, so the file get correctly loaded and displayed.
Thats my code:
#include <opencv2/objdetect.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace cv;
int main(int argc, char* argv[]) {
Mat getImage = imread(R"(QRCodeWikipedia.png)");
QRCodeDetector qrDet = QRCodeDetector::QRCodeDetector();
Mat corners;
bool detection_result = qrDet.detect(getImage, corners);
std::cout << "detection_result: " << detection_result << std::endl;
Mat points, rectImage;
std::string data = qrDet.detectAndDecode(getImage, points, rectImage);
if (data.length() > 0) {
std::cout << "Data after decoding: " << data << std::endl;
}
else {
std::cout << "QR Code not detected" << std::endl;
}
imshow("file", getImage);
waitKey(0);
}
Output from that code is
detection_result: 0
QR Code not detected
Is there something im doing wrong? Or should be changed, so it can detect that QR-Code?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
