如何理解SLAM三維重建中的DLT算法求解單應矩陣Python實踐代碼

# -*- coding: utf-8 -*-
"""
Created on Thu Dec  5 20:22:04 2019

@author: 知乎@司南牧
"""

import numpy as np

points1 = np.array([
                [1,1],
                [2,3],
                [4,8],
                [3,2],
                ])
points1 = np.column_stack((points1,np.ones(points1.shape[0]))).T

H = np.array([
        [3,4,1],
        [5,6,2],
        [0,0,1]
        ])


points2 = H@points1

p1 = points1[:-1,:].T
p2 = points2[:-1,:].T

H = H.reshape((-1,1))

A_up = np.column_stack((p1,np.ones(p1.shape[0]),np.zeros((p1.shape[0],3)),-p1[:,0]*p2[:,0],-p1[:,1]*p2[:,0],-p2[:,0]))
A_below = np.column_stack((np.zeros((p1.shape[0],3)),p1,np.ones(p1.shape[0]),-p1[:,0]*p2[:,1],-p1[:,1]*p2[:,1],-p2[:,1]))

A = np.vstack((A_up,A_below))

result = np.linalg.svd(A)[-1][-1]
result = result/result[-1]
result = result.reshape((p1.shape[1]+1,-1))
print("DLT算法計算出的單應矩陣爲:\n",result)
print("真實值爲:\n",H.reshape((p1.shape[1]+1,-1)))

作者:知乎@司南牧

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章