【发布时间】:2020-09-04 18:56:59
【问题描述】:
我正在尝试扭曲下面的图像,以便获得更大的墙的正面平行视图,即图像左侧的墙。但是,我无法决定应该在函数 cv2.getPerspectiveTransform() 中给出哪些点,以便获得矩阵,如果应用该矩阵会产生所需的结果。
我使用的代码是:
import cv2
import numpy as np
circles = np.zeros((4,2),np.int)
counter = 0
def mousePoints(event,x,y,flags,params):
global counter
if event == cv2.EVENT_LBUTTONDOWN:
circles[counter] = x,y
counter = counter + 1
print(circles)
img = cv2.imread("DSC_0273.JPG")
img = cv2.resize(img,(1500,1000))
q = 0
while True:
if counter == 4:
q = q+1
height1,width1 = 1080,1920
pts1 = np.float32([circles[0],circles[1],circles[2],circles[3]])
width = np.sqrt((circles[1][0] - circles[0][0])**2 + (circles[1][1] - circles[0][1])**2)
height = np.sqrt((circles[2][1] - circles[0][1])**2 + (circles[2][0] - circles[0][0])**2)
width = int(np.round(width))
height = int(np.round(height))
x1,y1 = circles[0]
pts2 = np.float32([[x1,y1],[(x1+width),y1],[(x1+width),(y1+height)],[x1,(y1+height)]])
matrix = cv2.getPerspectiveTransform(pts1,pts2)
if q == 1:
print(matrix.shape)
print(matrix)
imgOutput = cv2.warpPerspective(img,matrix,(width1,height1))
cv2.imshow("Output Image ", imgOutput)
for x in range (0,4):
cv2.circle(img,(circles[x][0],circles[x][1]),3,(0,255,0),cv2.FILLED)
cv2.imshow("Original Image ", img)
cv2.setMouseCallback("Original Image ", mousePoints)
cv2.waitKey(1)
所以基本上,我点击 4 个点,我的代码将找到扭曲矩阵,以便将这 4 个点内的区域映射到一个矩形,这样我给出的第一个点就会映射到相同的像素位置,并且调整其他4个点,使包围变成一个矩形。为了推断这一点,我在整个图像上应用了相同的矩阵。 我试过的一组点(通过鼠标点击给出的4个点)是: [[349, 445], [396, 415],[388, 596], [338, 610]] 我得到的结果是:
【问题讨论】:
-
请将您的代码编辑到您的问题中,并显示您尝试过的要点,以及您得到的结果。
-
给出了典型的描述和匹配的关键点,如 sift 或 surf 或 orb,因为它们被设计为在不同的图像中匹配。
-
@barny 根据你的要求,我已经添加了我的代码、我使用的一组点以及我得到的输出。
-
@Micka 恐怕我不清楚你的意思。你能详细说明一下吗?
-
请修正你的代码缩进 - 它现在不会执行。想象一下,我想亲自尝试您的代码,我需要能够将其粘贴到文件中并运行它 - 所以我需要一个 minimal reproducible example。
标签: python opencv image-processing perspective