gpt4 book ai didi

opencv - 使用 opencv 计算虚拟相机的单应性

转载 作者:太空宇宙 更新时间:2023-11-03 22:01:00 46 4
gpt4 key购买 nike

我有一个平面的图像,我想计算一个图像变形,它为我提供从位于 3d 空间中另一点的虚拟相机看到的同一平面的合成 View 。

因此,给定图像 I1,我想计算图像 I2,它表示从虚拟相机看到的图像 I1。

理论上,存在将这两个图像相关联的单应性。

如何在给定虚拟相机的相机姿势及其内部参数矩阵的情况下计算单应性?

我正在使用 opencv 的 warpPerspective() 函数来应用此单应性并生成扭曲的图像。

提前致谢。

最佳答案

好的,找到这篇文章 ( Opencv virtually camera rotating/translating for bird's eye view ),我在其中找到了一些代码来做我需要的。

但是,我注意到 Y 的旋转有一个符号错误(-sin 而不是 sin)。这是我适用于 python 的解决方案。我是 Python 的新手,如果我做错了事,请见谅。

import cv2
import numpy as np

rotXdeg = 90
rotYdeg = 90
rotZdeg = 90
f = 500
dist = 500

def onRotXChange(val):
global rotXdeg
rotXdeg = val
def onRotYChange(val):
global rotYdeg
rotYdeg = val
def onRotZChange(val):
global rotZdeg
rotZdeg = val
def onFchange(val):
global f
f=val
def onDistChange(val):
global dist
dist=val

if __name__ == '__main__':

#Read input image, and create output image
src = cv2.imread('/home/miquel/image.jpeg')
dst = np.ndarray(shape=src.shape,dtype=src.dtype)

#Create user interface with trackbars that will allow to modify the parameters of the transformation
wndname1 = "Source:"
wndname2 = "WarpPerspective: "
cv2.namedWindow(wndname1, 1)
cv2.namedWindow(wndname2, 1)
cv2.createTrackbar("Rotation X", wndname2, rotXdeg, 180, onRotXChange)
cv2.createTrackbar("Rotation Y", wndname2, rotYdeg, 180, onRotYChange)
cv2.createTrackbar("Rotation Z", wndname2, rotZdeg, 180, onRotZChange)
cv2.createTrackbar("f", wndname2, f, 2000, onFchange)
cv2.createTrackbar("Distance", wndname2, dist, 2000, onDistChange)

#Show original image
cv2.imshow(wndname1, src)

h , w = src.shape[:2]

while True:

rotX = (rotXdeg - 90)*np.pi/180
rotY = (rotYdeg - 90)*np.pi/180
rotZ = (rotZdeg - 90)*np.pi/180

#Projection 2D -> 3D matrix
A1= np.matrix([[1, 0, -w/2],
[0, 1, -h/2],
[0, 0, 0 ],
[0, 0, 1 ]])

# Rotation matrices around the X,Y,Z axis
RX = np.matrix([[1, 0, 0, 0],
[0,np.cos(rotX),-np.sin(rotX), 0],
[0,np.sin(rotX),np.cos(rotX) , 0],
[0, 0, 0, 1]])

RY = np.matrix([[ np.cos(rotY), 0, np.sin(rotY), 0],
[ 0, 1, 0, 0],
[ -np.sin(rotY), 0, np.cos(rotY), 0],
[ 0, 0, 0, 1]])

RZ = np.matrix([[ np.cos(rotZ), -np.sin(rotZ), 0, 0],
[ np.sin(rotZ), np.cos(rotZ), 0, 0],
[ 0, 0, 1, 0],
[ 0, 0, 0, 1]])

#Composed rotation matrix with (RX,RY,RZ)
R = RX * RY * RZ

#Translation matrix on the Z axis change dist will change the height
T = np.matrix([[1,0,0,0],
[0,1,0,0],
[0,0,1,dist],
[0,0,0,1]])

#Camera Intrisecs matrix 3D -> 2D
A2= np.matrix([[f, 0, w/2,0],
[0, f, h/2,0],
[0, 0, 1,0]])

# Final and overall transformation matrix
H = A2 * (T * (R * A1))

# Apply matrix transformation
cv2.warpPerspective(src, H, (w, h), dst, cv2.INTER_CUBIC)

#Show the image
cv2.imshow(wndname2, dst)
cv2.waitKey(1)

关于opencv - 使用 opencv 计算虚拟相机的单应性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23920729/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com