2017-01-02 61 views
9

我正在研究depth map with OpenCV。我可以獲得它,但它是從左側攝像機原點重建的,並且後者有一點傾斜,如圖所示,深度「偏移」(深度應該接近且沒有水平梯度):Python - 從旋轉角度對OpenCV進行透視變換

enter image description here

我想表達它作爲一個零角度,我嘗試用經線角度的功能,你可以看到下面,但我得到零場...

P = np.dot(cam,np.dot(Transl,np.dot(Rot,A1))) 

dst = cv2.warpPerspective(depth, P, (2048, 2048)) 

有:

#Projection 2D -> 3D matrix 
A1 = np.zeros((4,3)) 
A1[0,0] = 1 
A1[0,2] = -1024 
A1[1,1] = 1 
A1[1,2] = -1024 
A1[3,2] = 1 

#Rotation matrice around the Y axis 
theta = np.deg2rad(5) 
Rot = np.zeros((4,4)) 
Rot[0,0] = np.cos(theta) 
Rot[0,2] = -np.sin(theta) 
Rot[1,1] = 1 
Rot[2,0] = np.sin(theta) 
Rot[2,2] = np.cos(theta) 
Rot[3,3] = 1 

#Translation matrix on the X axis 
dist = 0 
Transl = np.zeros((4,4)) 
Transl[0,0] = 1 
Transl[0,2] = dist 
Transl[1,1] = 1 
Transl[2,2] = 1 
Transl[3,3] = 1 

#Camera Intrisecs matrix 3D -> 2D 
cam = np.concatenate((C1,np.zeros((3,1))),axis=1) 
cam[2,2] = 1 
P = np.dot(cam,np.dot(Transl,np.dot(Rot,A1))) 

dst = cv2.warpPerspective(Z0_0, P, (2048*3, 2048*3)) 

編輯後來:

您也可以下載32MB數據集場在這裏:https://filex.ec-lille.fr/get?k=cCBoyoV4tbmkzSV5bi6。然後,加載並查看圖像:

from matplotlib import pyplot as plt 
import numpy as np 

img = np.load('testZ0.npy') 
plt.imshow(img) 
plt.show() 
+0

爲什麼你刪除了你的答案valentin? – user3601754

+0

[This answer](http://stackoverflow.com/q/33497736/1510289)可能對你有幫助。 –

+0

謝謝,我檢查了;) – user3601754

回答

4

我已經得到了一個粗略的解決方案。您可以稍後修改它。

我使用OpenCV中的鼠標處理操作裁剪給定熱圖中的感興趣區域。

(我只是說我用鼠標來裁剪區域?)是的,我做了。在OpenCV SEE THIS中瞭解更多關於鼠標功能的信息。此外,還有許多其他的做題,可以幫助你在這方面:)

使用我能這些功能,得到如下:

enter image description here

現在你移走傾斜的問題。我使用了單應矩陣,通過獲取上面圖像的角點並將其用於一個確定大小的「白色」圖像。我使用了cv2.findHomography()函數。

現在使用的OpenCV的cv2.warpPerspective()功能,我能得到如下:

enter image description here

現在你可以將所需的規模,以這個形象,你想要的。

CODE:

我還附上一些代碼片段,請過目:

#First I created an image of white color of a definite size 
back = np.ones((435, 379, 3)) # size 
back[:] = (255, 255, 255)  # white color 

接下來,我所獲得的角點pts_src傾斜圖像下面:

pts_src = np.array([[25.0, 2.0],[403.0,22.0],[375.0,436.0],[6.0,433.0]]) 

我想將上面的點映射到下面給出的點'pts_dst':

pts_dst = np.array([[2.0, 2.0], [379.0, 2.0], [379.0, 435.0],[2.0, 435.0]]) 

現在我使用單應性的主要:

h, status = cv2.findHomography(pts_src, pts_dst) 

最後,我使用透視變換映射到的原始圖像的白色圖像。

fin = cv2.warpPerspective(img, h, (back.shape[1],back.shape[0])) 
# img -> original tilted image. 
# back -> image of white color. 

希望這會有所幫助!我也從這個問題中學到了很多東西。

注意:提供給'cv2.findHomography()'的點必須在float。 欲瞭解更多有關Homography的信息,請訪問THIS PAGE