Python 使用QR码作为图像中的定位点进行透视扭曲 问题摘要
我有下图,其中有两个二维码始终位于固定位置,如下所示。它还具有从用户处获取的预定义感兴趣区域,如图1中的绿色矩形所示。现在我想做的是,当我看到一个以一定角度拍摄的图像(image2)时,我想使用QR码作为参考锚点进行透视扭曲,以便刻度盘中的读数适合红色边界框。下面给出了图像的示例。下面还给出了用于绘制用户定义的感兴趣区域的原始图像 我试过的 步骤1:用户选择ROI并保存输出Python 使用QR码作为图像中的定位点进行透视扭曲 问题摘要,python,image,opencv,image-processing,Python,Image,Opencv,Image Processing,我有下图,其中有两个二维码始终位于固定位置,如下所示。它还具有从用户处获取的预定义感兴趣区域,如图1中的绿色矩形所示。现在我想做的是,当我看到一个以一定角度拍摄的图像(image2)时,我想使用QR码作为参考锚点进行透视扭曲,以便刻度盘中的读数适合红色边界框。下面给出了图像的示例。下面还给出了用于绘制用户定义的感兴趣区域的原始图像 我试过的 步骤1:用户选择ROI并保存输出 import cv2 import numpy as np import pyzbar.pyzbar as p
import cv2
import numpy as np
import pyzbar.pyzbar as pyzbar
is_drawing = False
ix = -1
iy = -1
image_two=cv2.imread('cal_img.jpeg')
dim = (1536, 1024)
#resize image
resized = cv2.resize(image_two, dim, interpolation = cv2.INTER_AREA)
def draw_rectangle(event, x, y, flags, param):
global is_drawing, ix, iy
if event == cv2.EVENT_LBUTTONDOWN:
is_drawing = True
ix = x
iy = y
elif event == cv2.EVENT_LBUTTONUP:
is_drawing = False
cv2.rectangle(img=resized, pt1=(ix, iy), pt2=(x, y), color=(0, 255, 0), thickness=-1)
ix = -1
iy = -1
elif event == cv2.EVENT_MOUSEMOVE:
if is_drawing:
cv2.rectangle(img=resized, pt1=(ix, iy), pt2=(x, y), color=(0, 255, 0), thickness=-1)
cv2.namedWindow("image")
cv2.setMouseCallback("image", draw_rectangle)
while True:
cv2.imshow("image", resized)
# wait 5 seconds then wait for ESC key
if cv2.waitKey(5) & 0xFF == 27:
break
cv2.imwrite('image.jpg', resized)
cv2.destroyAllWindows()
步骤2:找到校准ROI的像素坐标
coordinates = [] # list of the green rectangle coordinates
green_color = [0, 255, 0]
last_x_green, last_y_green = 0, 0 # store the last x and y green positions
# reads the image in the color mode
image_four = cv2.imread('image.jpg', 1)
rows, cols, _ = image_four.shape
for x in range(rows):
for y in range(cols):
px = list(image_four[x, y])
if px == green_color:
# find the first coordinate of the green rectangle (top left corner)
if len(coordinates) == 0:
coordinates.append((y, x)) # top left corner of the green rectangle
last_x_green, last_y_green = x, y
coordinates.append((last_y_green, last_x_green))
top_left = coordinates[0] # (x1, y1)
bottom_left = (coordinates[0][0], coordinates[1][1]) # (x1, y2)
top_right = (coordinates[1][0], coordinates[0][1]) # (x2, y1)
bottom_right = coordinates[1]
print('The coordinates of the green rectangle, from left to right and from top to bottom, are:')
print(f'Top Left: {top_left}, Top Right: {top_right}, Bottom Left: {bottom_left}, Bottom Right: {bottom_right}')
第三步:阅读二维码
decodedObjects =pyzbar.decode(image_one)
print('The Decoded QR codeis:',decodedObjects[0].data)
print('The Location of the Top Left QR Code is:',decodedObjects[0].rect)
print('The Location of the Bottom Right QR Code is:',decodedObjects[1].rect)
问题
由于二维码在两幅图像中的位置保持不变,我无法确定如何在第二幅图像中获得ROI的四个角(绿色框),以进行透视变换。绘制线,定义二维码的角并延伸这些线。然后得到外部直线组的交点。这将定义一个旋转的矩形。请注意,如果您有4个QR码(或者只使用十字或更简单的符号也可以),这将更加容易,因为透视变换需要4组共轭坐标