基于二维码对图像应用变换

问题描述 投票:0回答:1

我想修正图像的透视和比例。该图像包含一个 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

############################

############################

我拍了一张新照片:

  • 原始图像是:page_1_ori。 在此图像中,代码位于 (362,1322) [![原图][1]][1]
  • 我对图像进行了一些转换,得到:page_1 [![变换后的图像][2]][2]

如果我确实使用以下代码(与上面相同的代码,添加

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]

python opencv computer-vision augmented-reality qr-code
1个回答
0
投票

############################

############################

编辑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
似乎给出了更好的结果,尽管代码似乎有点扭曲。

transformed_image_1.jpg

© www.soinside.com 2019 - 2024. All rights reserved.