将透视图转换为俯视图
transforming perspective view to a top view
我正在使用 Opencv 和 python 的组合将此图像的透视图转换为鸟瞰图(顶视图)
去掉我写这段代码的透视图
Image1 = cv2.imread('/path to image/im.jpg')
cv2.circle(Image1,(867,652),5,(0,0,255),-1)
cv2.circle(Image1,(1020,580),5,(0,0,255),-1)
cv2.circle(Image1,(1206,666),5,(0,0,255),-1)
cv2.circle(Image1,(1057,757),5,(0,0,255),-1)
pts1=np.float32([[867,652],[1020,580],[1206,666],[1057,757]])
pts2=np.float32([[448,609],[580,607],[582,724],[445,730]])
matrix=cv2.getPerspectiveTransform(pts1,pts2)
result=cv2.warpPerspective(Image1,matrix,(1920,1080))
cv2.imshow('frame',Image1)
cv2.imshow('result',result)
path = 'path'
cv2.imwrite(os.path.join(path , 'Dots.jpg'), Image1)
cv2.imwrite(os.path.join(path , 'TransImage.jpg'), result)
cv2.waitKey(0)
我觉得为pts2选择的点不是很正确,也没有给我一个很好的俯视图。谁能找到正确的转换?我希望白色车道的宽度沿图像保持不变。
如果你把它变成一个正方形(并稍微改变一下原来的点),它会变得更好一点:
import cv2
import numpy as np
img = cv2.imread('road.jpg')
pts = np.array([[864, 651], [1016, 581], [1205, 667], [1058, 759]], dtype=np.float32)
for pt in pts:
cv2.circle(img, tuple(pt.astype(np.int)), 1, (0,0,255), -1)
# compute IPM matrix and apply it
ipm_pts = np.array([[448,609], [580,609], [580,741], [448,741]], dtype=np.float32)
ipm_matrix = cv2.getPerspectiveTransform(pts, ipm_pts)
ipm = cv2.warpPerspective(img, ipm_matrix, img.shape[:2][::-1])
# display (or save) images
cv2.imshow('img', img)
cv2.imshow('ipm', ipm)
cv2.waitKey()
您可以通过在车道标记本身上选择 4 个点来强制它沿图像具有相同的宽度。
我正在使用 Opencv 和 python 的组合将此图像的透视图转换为鸟瞰图(顶视图)
去掉我写这段代码的透视图
Image1 = cv2.imread('/path to image/im.jpg')
cv2.circle(Image1,(867,652),5,(0,0,255),-1)
cv2.circle(Image1,(1020,580),5,(0,0,255),-1)
cv2.circle(Image1,(1206,666),5,(0,0,255),-1)
cv2.circle(Image1,(1057,757),5,(0,0,255),-1)
pts1=np.float32([[867,652],[1020,580],[1206,666],[1057,757]])
pts2=np.float32([[448,609],[580,607],[582,724],[445,730]])
matrix=cv2.getPerspectiveTransform(pts1,pts2)
result=cv2.warpPerspective(Image1,matrix,(1920,1080))
cv2.imshow('frame',Image1)
cv2.imshow('result',result)
path = 'path'
cv2.imwrite(os.path.join(path , 'Dots.jpg'), Image1)
cv2.imwrite(os.path.join(path , 'TransImage.jpg'), result)
cv2.waitKey(0)
我觉得为pts2选择的点不是很正确,也没有给我一个很好的俯视图。谁能找到正确的转换?我希望白色车道的宽度沿图像保持不变。
如果你把它变成一个正方形(并稍微改变一下原来的点),它会变得更好一点:
import cv2
import numpy as np
img = cv2.imread('road.jpg')
pts = np.array([[864, 651], [1016, 581], [1205, 667], [1058, 759]], dtype=np.float32)
for pt in pts:
cv2.circle(img, tuple(pt.astype(np.int)), 1, (0,0,255), -1)
# compute IPM matrix and apply it
ipm_pts = np.array([[448,609], [580,609], [580,741], [448,741]], dtype=np.float32)
ipm_matrix = cv2.getPerspectiveTransform(pts, ipm_pts)
ipm = cv2.warpPerspective(img, ipm_matrix, img.shape[:2][::-1])
# display (or save) images
cv2.imshow('img', img)
cv2.imshow('ipm', ipm)
cv2.waitKey()
您可以通过在车道标记本身上选择 4 个点来强制它沿图像具有相同的宽度。