【问题标题】:Compute homography for a virtual camera with opencv使用 opencv 计算虚拟相机的单应性
【发布时间】:2014-07-18 05:08:46
【问题描述】:

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

所以,给定一个图像 I1,我想计算一个图像 I2,它代表从虚拟相机看到的图像 I1。

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

在给定虚拟相机的相机姿态以及它的内部参数矩阵的情况下,我如何计算这个单应性?

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

提前致谢。

【问题讨论】:

    标签: opencv 3d camera virtual homography


    【解决方案1】:

    好的,找到了这篇文章 (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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-17
      • 2013-01-04
      • 1970-01-01
      • 1970-01-01
      • 2021-06-20
      • 1970-01-01
      相关资源
      最近更新 更多