我想修正图像的透视和比例。该图像包含一个 QR 码(已知大小
EXPECTED_QR_SIZE
),我正在使用 OpenCV
函数来检测 QR 码并对整个图像应用透视和缩放校正。
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
############################
############################
我拍了一张新照片:
如果我确实使用以下代码(与上面相同的代码,添加
dest_pts += [362, 1322]
):
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()
我得到: [![结果图像][3]][3]
这很好地纠正了代码,但没有纠正图像的其余部分。
############################
############################
编辑2
############################
############################
删除
points = points.astype(int)
有帮助(显然)。
[![无点转换为 int][4]][4]
############################
############################
编辑3
############################
############################
在这里,我比较了两种可能性:
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])
# 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')
translation = np.array([362, 1322], dtype='float32')
# Compute the homography matrix
M = cv2.getPerspectiveTransform(points, dest_pts)
# Add the translation directly to the homography matrix
M[0, 2] += translation[0] # Add translation on the x-axis
M[1, 2] += translation[1] # Add translation on the y-axis
# Transformed the image and display it
transformed_image_1 = cv2.warpPerspective(image, M, (image.shape[1], image.shape[0]))
# save the image
cv2.imwrite('transformed_image_1.jpg', transformed_image_1)
# OR
dest_pts += translation
M = cv2.getPerspectiveTransform(points.astype('float32'), dest_pts)
# Transformed the image and display it
transformed_image_2 = cv2.warpPerspective(image, M, (image.shape[1], image.shape[0]))
cv2.imwrite('transformed_image_2.jpg', transformed_image_2)
transformed_image_1.jpg
似乎给出了更好的结果,尽管代码似乎有点扭曲。