2013-02-28 207 views
4

我從前視圖獲取圖片。我想把它變成鳥瞰圖。 現在我想爲矩形(x,y)中的每個點計算什麼將在梯形中變換x,y。 enter image description here將矩形變換爲梯形以獲得透視

對於給定的x和y以及梯形(a)的角度,必須有一個公式。

我在C編程和使用opencv。

非常感謝。

回答

5

您是否考慮homography變換。你用它來創建或更正圖像中的視角,我認爲它正是你想要的。

使用OpenCV,您可以使用方法cv::findHomography()。參數是4個初始點(矩形的頂點)和4個最終點(飛人的頂點)。你得到一個變換矩陣,然後你可以使用cv::warpPerspective()cv::perspectiveTransform()

+0

是的,相信我我有谷歌這在任何方向。但我無法弄清楚。 – Khashayar 2013-02-28 16:56:28

1

我能夠找出解決問題的方法。

這裏是我用於相同的代碼:

導入所需的軟件包:
import cv2 
import numpy as np 
讀取圖像中使用:
filename = '1.jpg' 
img = cv2.imread(filename) 
cv2.imwrite('img.jpg',img) 

enter image description here

存儲的高度和寬度單獨變量中的圖像:
ih, iw, _ = img.shape 
創建黑窗口,其尺寸比所述圖像的更大的並存儲在單獨的變量其高度和寬度:
black = np.zeros((ih + 300, iw + 300, 3), np.uint8) 
cv2.imwrite('black.jpg',black) 
bh, bw, _ = black.shape 

enter image description here

在陣列中存儲的圖像的4個點:
pts_src = np.array([[0.0, 0.0],[float(iw), 0.0],[float(iw), float(ih)],[0.0,float(ih)]]) 
存儲梯形的4個點來獲得:
pts_dst = np.array([[bw * 0.25, 0],[bw * 0.75, 0.0],[float(bw), float(bh)],[0.0,float(bh)]]) 
使用 pts_srcpts_dst計算單應性矩陣:
h, status = cv2.findHomography(pts_src, pts_dst) 
翹曲指定矩形圖像轉換成梯形:
im_out = cv2.warpPerspective(img, h, (black.shape[1],black.shape[0])) 
cv2.imwrite("im_outImage.jpg", im_out) 

enter image description here

cv2.waitKey(0) 
cv2.destroyAllWindows() 

如果你改變了數組pts_dst中的值,你將能夠獲得各種不同的四邊形。

+0

希望它有助於:) – 2017-01-20 16:49:38