opencv case 03 - two-dimensional code generation, discovery, positioning and recognition based on OpenCV

1. Generation of QR code

Not much nonsense, just go to the code

# 生成二维码
import qrcode

# 二维码包含的示例数据
data = "B0018"
# 生成的二维码图片名称
filename = "qrcode.png"
# 生成二维码
img = qrcode.make(data)
# 保存成图片输出
img.save(filename)

img.show()

running result:

will generate an image at the current
insert image description here

Recognize the generated QR code

Opencv has launched a two-dimensional code recognition interface since the 4th generation. The calling method is like this. The code is as follows:

import cv2

img = cv2.imread('qrcode.png')
qrcode = cv2.QRCodeDetector()
result, points, code = qrcode.detectAndDecode(img)

print(result)

operation result:

B0018

There are three return values,

  • The first result is the decoded content. For example, the result of my QR code is "B0018". Of course, it can also be a pure number.

  • The second points are the four corners of the QR code outline, clockwise from the upper left corner.

  • The third code is the original arrangement of the two-dimensional code, that is, a matrix of whether each point is 0 or 255. White is 255, and black is 0. It is very convenient to call, and if you don’t need to decode, you can call it if you just want to locate The detect function returns only four corner points.

What if it is a QR code recognition in a large picture? For example, the picture below
insert image description here

If you continue to use the above identification QR code, it will not be recognized.

Let's take a look at the principle and positioning principle of the two-dimensional code

The structure and basic principle of QR code

The standard QR code structure is as follows:

insert image description here
Special attention should be paid to the three black square areas in the figure, which are the three most important areas used to locate a QR code. The first thing we need to do when scanning and detecting a QR code is to find these three areas. If After finding these three areas, we have successfully discovered a QR code, and we can locate and identify it.

The descriptions of other parts of the QR code are as follows:

insert image description here
The square area on the three corners is from left to right, and from top to bottom, the ratio of black and white is 1:1:3:1:1.

insert image description here
No matter how the angle changes, this is the most significant feature. Through this feature, we can realize the detection and positioning of QR code scanning.

In addition to the above qrcode package that can recognize QR codes, there is also a pyzbar package that can also recognize QR codes. Compared with the pyzbar package, it is more efficient than the qrcode package. the code below

import cv2
import numpy as np
import time
import pyzbar.pyzbar as pyzbar

# 显示条码和二维码位置
def display(im, decodedObjects):
    # 遍历所有已解码的对象
    for decodedObject in decodedObjects:
        points = decodedObject.polygon

        # 如果点不形成四边形,请找到凸包
        if len(points) > 4:
            hull = cv2.convexHull(np.array([point for point in points], dtype=np.float32))
            hull = list(map(tuple, np.squeeze(hull)))
        else:
            hull = points;
        # 凸包中的点数
        n = len(hull)
        # 绘制凸包
        for j in range(0, n):
            cv2.line(im, hull[j], hull[(j + 1) % n], (255, 0, 0), 3)
# 创建一个 qrCodeDetector 对象
qrDecoder = cv2.QRCodeDetector()

# 检测和解码二维码
t = time.time()
inputImage = cv2.imread("66.jpg")

decodedObjects = pyzbar.decode(inputImage)
if len(decodedObjects):
    zbarData = decodedObjects[0].data
else:
    zbarData = ''


if zbarData:
    cv2.putText(inputImage, "result : {}".format(zbarData.decode()), (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 1,
                (0, 255, 0), 2, cv2.LINE_AA)
else:
    cv2.putText(inputImage, "ZBAR : QR Code NOT Detected", (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2,
                cv2.LINE_AA)

display(inputImage, decodedObjects)

print("Time Taken for Detect and Decode : {:.3f} seconds".format(time.time() - t))
cv2.imshow("Result", inputImage)
cv2.waitKey(0)
cv2.destroyAllWindows()





running result:

insert image description here

From the results, it can be seen that the position of the QR code can be located and the recognition result is displayed in the upper left corner.

Guess you like

Origin blog.csdn.net/hai411741962/article/details/132496222