gpt4 book ai didi

python - 如何确定透视变换后点在新图像平面中的位置?

转载 作者:行者123 更新时间:2023-12-02 16:06:13 24 4
gpt4 key购买 nike

我正在使用 OpenCV+Python+Numpy,我在图像中有三个点,我知道这些点的确切位置。

(P1, P2);
N1

我要将图像转换为另一个 View (例如,我将透视图转换为侧 View )。如果这样做,我将无法获得图像平面中这三个点的确切位置。
我应该以可以获得这些点的新坐标的方式编写代码。
   pts1=np.float32([[867,652],[1020,580],[1206,666],[1057,757]]) 

pts2=np.float32([[700,732],[869,754],[906,916],[712,906]])


matrix=cv2.getPerspectiveTransform(pts1,pts2)


result=cv2.warpPerspective(Image1,matrix,(1920,1080))

cv2.imshow('Image',Image1) cv2.imshow('Tran',result)

我的问题是:如何确定这 3 个点的新位置?

最佳答案

很简单,你可以在文档中查看 warpPerspective 作品。要转换点的位置,您可以使用以下转换:

enter image description here

在哪里 [x, y]是原点,M是你的透视矩阵

在 python 中实现这一点,您可以使用以下代码:

p = (50,100) # your original point
px = (matrix[0][0]*p[0] + matrix[0][1]*p[1] + matrix[0][2]) / ((matrix[2][0]*p[0] + matrix[2][1]*p[1] + matrix[2][2]))
py = (matrix[1][0]*p[0] + matrix[1][1]*p[1] + matrix[1][2]) / ((matrix[2][0]*p[0] + matrix[2][1]*p[1] + matrix[2][2]))
p_after = (int(px), int(py)) # after transformation

您可以在下面的代码中看到结果。红点是你的原点。第二个图显示了透视变换后的去向。蓝色圆圈是您在上面的公式中计算的点。

blue

你可以看看我的 Jupyter Notebook herehere .

编码:
import numpy as np
import cv2
import matplotlib.pyplot as plt

# load the image, clone it for output, and then convert it to grayscale
image = cv2.imread('sample.png')
pts1=np.float32([[867,652],[1020,580],[1206,666],[1057,757]])
pts2=np.float32([[700,732],[869,754],[906,916],[712,906]])
matrix=cv2.getPerspectiveTransform(pts1,pts2)

# Draw the point
p = (50,100)
cv2.circle(image,p, 20, (255,0,0), -1)

# Put in perspective
result=cv2.warpPerspective(image,matrix,(1500,800))

# Show images
plt.imshow(image)
plt.title('Original')
plt.show()

plt.imshow(result)
plt.title('Distorced')
plt.show()

# Here you can transform your point
p = (50,100)
px = (matrix[0][0]*p[0] + matrix[0][1]*p[1] + matrix[0][2]) / ((matrix[2][0]*p[0] + matrix[2][1]*p[1] + matrix[2][2]))
py = (matrix[1][0]*p[0] + matrix[1][1]*p[1] + matrix[1][2]) / ((matrix[2][0]*p[0] + matrix[2][1]*p[1] + matrix[2][2]))
p_after = (int(px), int(py))

# Draw the new point
cv2.circle(result,p_after, 20, (0,0,255), 12)

# Show the result
plt.imshow(result)
plt.title('Predicted position of your point in blue')
plt.show()

关于python - 如何确定透视变换后点在新图像平面中的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57399915/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com