Код: Выделить всё
import cv2
import numpy as np
EXPECTED_QR_SIZE = 148
image = cv2.imread('page_1.jpg')
# Detect the QR code in the image
qrCodeDetector = cv2.QRCodeDetector()
retval, decoded_info, points, straight_qrcode = qrCodeDetector.detectAndDecodeMulti(image)
points = points.astype(int)
# Define the destination points for the perspective transformation
dest_pts = np.array([[0, 0], [EXPECTED_QR_SIZE-1, 0], [EXPECTED_QR_SIZE-1, EXPECTED_QR_SIZE-1], [0, EXPECTED_QR_SIZE-1]], dtype='float32')
M = cv2.getPerspectiveTransform(points.astype('float32'), dest_pts)
# Transformed the image and display it
transformed_image = cv2.warpPerspective(image, M, (image.shape[1], image.shape[0]))
cv2.imshow('Image', transformed_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
#########################
########### ###############
ИЗМЕНИТЬ 1
######################## #
#########################
Я сделали новое изображение:
- исходное изображение: page_1_ori.
На этом изображении код расположен по адресу (362,1322)

< /li>
Я применил к изображению некоторые преобразования, которые дают: page_1

Код: Выделить всё
import cv2
import numpy as np
EXPECTED_QR_SIZE = 148
image = cv2.imread('page_1.jpg')
# Detect the QR code in the image
qrCodeDetector = cv2.QRCodeDetector()
retval, decoded_info, points, straight_qrcode = qrCodeDetector.detectAndDecodeMulti(image)
print('QR width, height:', points[0][1][0] - points[0][0][0], points[0][2][1] - points[0][1][1])
points = points.astype(int)
# Define the destination points for the perspective transformation
dest_pts = np.array([[0, 0], [EXPECTED_QR_SIZE-1, 0], [EXPECTED_QR_SIZE-1, EXPECTED_QR_SIZE-1], [0, EXPECTED_QR_SIZE-1]], dtype='float32')
dest_pts += [362, 1322]
M = cv2.getPerspectiveTransform(points.astype('float32'), dest_pts)
# Transformed the image and display it
transformed_image = cv2.warpPerspective(image, M, (image.shape[1], image.shape[0]))
cv2.imshow('Image', transformed_image)
# save the image
cv2.imwrite('transformed_image.jpg', transformed_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
################### ##############
######################## #
РЕДАКТИРОВАТЬ 2
################## ########
#########################Удаление точек =points.astype(int) помогает (очевидно).
Подробнее здесь: https://stackoverflow.com/questions/791 ... -a-qr-code
Мобильная версия