transforming perspective view to a top view

拥有回忆 提交于 2020-01-06 04:53:07

问题


I am using a combination of Opencv and python to turn the perspective view of this image to a birds-eye view (top view)

to remove the perspective view I wrote this code

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)

I feel the points that choose for pts2 are not that corrects and does not give me a nice top view. Can anyone find the correct transformation? I want the width of the white lane to be constant along the image.


回答1:


If you transform it to a square (and change the original points a little bit) it gets a little bit better:

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()

You can force it to have the same width along the image by selecting 4 points on the lane markers themselves.



来源:https://stackoverflow.com/questions/57439977/transforming-perspective-view-to-a-top-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!