How to Detect and Decode QR Code from an image in C++ with OpenCV

Hello learners, In this tutorial, we will learn how to decode QR Codes in C++ with OpenCV.
Before moving to the concept directly, let’s go through some basic terminologies for better understanding.

  • QR Code
  • OpenCV

What is a QR Code?

QR stands for quick response. A QR code is a type of barcode that stores information such as website links, texts, etc. These QR Codes are mostly used in cashless payments. QR Codes are scanned using a QR scanner or a smartphone camera.

What is OpenCV?

OpenCV stands for Open Source Computer Vision Library. OpenCV is an open-source computer vision and machine learning software library. It is used to develop real-time computer vision applications such as face recognition etc.
We will use OpenCV 4.0 to detect and decode the QR Code. Therefore, you will need OpenCV 4.0.0 or above to run the code.

Now, let’s start to learn how to detect and decode a QR Code.

Detect and Decode QR Code from an image in C++ with OpenCV

Step 1: Include all the required header files and namespaces into the code.

#include <opencv2/objdetect.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>

using namespace std;
using namespace cv;

Namespaces are used to avoid naming collisions (same names to more than one identifier). It defines a scope to the identifiers.
For example, the “using namespace std” statement indicates that whatever names we use in the program are from the std library and there is no need to explicitly mention from which library they belong. so, we can eliminate the use of the scope resolution operator whenever we use the names from the std library.

Step 2: Creation of a function to display a highlighted box around the QR Code.

void display(Mat &im, Mat &points)
{
  int i;
  int n = points.rows;
  for(i = 0 ; i < n ; i++)
  {
    line(im, Point2i(points.at<float>(i,0), points.at<float>(i,1)),
    Point2i(points.at<float>((i+1)%n,0), points.at<float>((i+1)%n,1)), Scalar(255,0,0), 3);
  }
  imshow("windowName", im);
}

This display() is a user-defined function that takes the image and the coordinates of the image as its arguments and returns nothing. The Mat class of the OpenCV: to store the values of an image. The line() function from the OpenCV C++ library is used to draw a line.

Syntax of the line() function:

line(image, startPointOfLineSegment, endPointOfLineSegment, color, thickness, lineType, shift)
This line() function returns an image.

imshow() function is used to display an image in a window. imshow() function takes windowName and image as its arguments and doesn’t return anything.

Step 3: The main function from which the execution of the program starts.

int main(int argc, char* argv[]) {
  Mat getImage;
  if(argc>1)
    getImage = imread(argv[1]);
  else
    getImage = imread("filename.jpg");
  QRCodeDetector qrDet = QRCodeDetector::QRCodeDetector();
  Mat points, rectImage;
  std::string data = qrDet.detectAndDecode(getImage, points, rectImage);
  if(data.length()>0)
  {
    cout << "Data after decoding: " << data << endl;
    display(getImage, points);
    rectImage.convertTo(rectImage, CV_8UC3);
    imshow("Rectified QRCode", rectImage);
    waitKey(0);
  }
  else {
    cout << "QR Code not detected" << endl;
  }
}

The argc, argv are passed as parameters to the main() function. argc is of integer type indicating the number of arguments and argv is an array of arguments.
The imread() function is used to read images which takes filename and flag as its arguments. The flag argument is optional that is used to determine the mode in which the image is to be read.
We create an object for the QRCodeDetector class from the cv library and instantiate it with its respective constructor QRCodeDetector(). Then, we’ll call the detectAndDecode() function with the help of the dot operator to detect and get the information from the QR Code.

Finally, we display the information and the rectified image of the QR Code as output.

Output: 
The data present in the QR Code is displayed along with the QR Code with a highlighted box around it.

Data after decoding: CodeSpeedy

Leave a Reply

Your email address will not be published. Required fields are marked *