OpenCV (38): QR code detection

1. Principle of QR code recognition

Function graphics:

  1. Position detection graphics: Usually, there are three position detection graphics in the QR code, which are L-shaped or high-angle cross-shaped, distributed on the three corners of the QR code, and are used to help the scanning device locate the position and direction of the QR code.

  2. Location detection pattern separator: helps the scanning device distinguish the location detection pattern and the data area of ​​the QR code.

  3. Calculation pattern: Typically a small square pattern used to calibrate a scanning device to capture and decode an image of a QR code.

  4. Alignment Marks: Typically a series of small square patterns used to help scanning devices better align and decode QR codes at different distances and angles.

Coding area format:

  1. Format information: Format information is used to specify the encoding format and error correction level of the QR code.

  2. Version information: The version information specifies the size and data capacity of the QR code. .

  3. Data and error correction codes: The encoding area also contains the actual data and error correction codes.

2. QR code positioning function and recognition function

QR code positioning function detect()

bool detect(InputArray img, OutputArray points) const;
  • img: Whether the grayscale image or color image containing the QR code is to be detected.
  • points: The four vertex coordinates of the minimum area quadrilateral containing the QR code, that is, the four vertex coordinates of the QR code.

QR code recognition function decode()

 std::string decode(InputArray img, InputArray points, OutputArray straight_code = noArray()) const;

  • img: Image containing QR code.
  • points: The four vertex coordinates of the minimum area quadrilateral containing the QR code.
  • straight qrcode: Corrected and binarized OR QR code.

Sample code:

void qrcode(Mat image){
    Mat gray,qrcode_bin;
    cvtColor(image,gray,COLOR_BGR2GRAY);
    QRCodeDetector qrCodeDetector;
    vector<Point> points;
    string information;
    bool isQRcode;
    isQRcode=qrCodeDetector.detect(gray,points);//识别二维码
    if(isQRcode){
        //解码二维码
        information=qrCodeDetector.decode(gray,points,qrcode_bin);
    }else{
        LOGD("无法识别二维码");
    }
    //绘制二维码的边框
    for(int i=0;i<points.size();i++){
        if(i==points.size()-1) {
            line(image, points[i], points[0], Scalar(0, 0, 255, 255), 2, 8);
            break;
        }
        line(image,points[i],points[i+1],Scalar(0,0,255,255),2,8);
    }
    //将解码内容输出到图片上
    putText(image,information.c_str(),Point(20,30),2,1,Scalar(0,0,255,255),8);
    //显示图像
    imwrite("/sdcard/DCIM/image.png",image);
    imwrite("/sdcard/DCIM/qrcode_bin.png",qrcode_bin);
}

Output picture:

Corrected and binarized OR QR code:

3. QR code direct positioning and recognition function detectAndDecode()

std::string cv::QRCodeDetector::detectAndDecode ( InputArray   img,

OutputArray points = noArray(),

OutputArray straight qrcode = noArray()

)

  • img: image containing QR code
  • points: The four vertex coordinates of the minimum area quadrilateral containing the QR code
  • straight_qrcode: Corrected and binarized OR QR code

Sample code:

//利用函数直接定位二维码并解码
void qrcode2(Mat image){
    Mat gray;
    cvtColor(image,gray,COLOR_BGR2GRAY);
    QRCodeDetector qrCodeDetector;
    vector<Point> points;
    string information;
    information=qrCodeDetector.detectAndDecode(gray,points);
    //将解码内容输出到图片上
    putText(image,information.c_str(),Point(20,30),2,1,Scalar(0,0,255,255),8);
    //显示图像
    imwrite("/sdcard/DCIM/image2.png",image);
}

Output picture:

Guess you like

Origin blog.csdn.net/weixin_63357306/article/details/132830563