相機(jī)校準(zhǔn)—外參矩陣
點(diǎn)擊上方“小白學(xué)視覺(jué)”,選擇加"星標(biāo)"或“置頂”
重磅干貨,第一時(shí)間送達(dá)

相機(jī)外參

有助于確定攝影機(jī)方向的旋轉(zhuǎn)變換。 有助于移動(dòng)相機(jī)的平移變換。
旋轉(zhuǎn)
通過(guò)旋轉(zhuǎn)改變坐標(biāo)

從圖來(lái)看,
sinα = y/r , cosα = x/r ? [1]
? xsinα = ycosα ? [2]
同樣的, x′ = rcos(θ+α)
? x′ = (x/cosα) ? cos(θ+α) (from [1])
但, cos(θ+α) = cosθcosα ? sinθsinα
? x′ = (x/cosα) ? (cosθcosα ? sinθsinα)
? x′ = xcosθ ? xsinα ? (sinθ / cosα)
? x′ = xcosθ ? ycosα ? (sinθ / cosα) (from [2])
? x′ = xcosθ ? ysinθ
同樣地,
y′ = rsin(θ+α)
? y′ = (y/sinα) ? sin(θ+α) (from [1])
但, sin(θ+α) = sinθcosα + cosθsinα
? y′ = (y/sinα) ? (sinθcosα + cosθsinα)
? y′ = ycosθ + ycosα ? (sinθ / sinα)
? y′ = ycosθ + xsinα ? (sinθ / sinα) (from [2])
? y′ = ycosθ + xsinθ
? y′ = xsinθ + ycosθ
因此我們有,
x′ = xcosθ ? ysinθ
y′ = xsinθ + ycosθ

擴(kuò)展到R3



內(nèi)參旋轉(zhuǎn)與外參旋轉(zhuǎn)

基變換

從這個(gè)圖來(lái)看,
sinα = y′/r , cosα = x′/r ? [1]
? x′sinα = y′cosα ? [2]
同樣, x = rcos(θ+α)
? x = (x′/cosα) ? cos(θ+α) (from [1])
但, cos(θ+α) = cosθcosα ? sinθsinα
? x = (x′ / cosα) ? (cosθcosα ? sinθsinα)
? x = x′cosθ ? xsinα ? (sinθ / cosα)
? x = x′cosθ ? y′cosα ? (sinθ / cosα) (from [2])
? x = x′cosθ ? y′sinθ
同樣地,
y = rsin(θ+α)
? y = (y′/sinα) ? sin(θ+α) (from [1])
但, sin(θ+α) = sinθcosα + cosθsinα
? y = (y′/sinα) ? (sinθcosα + cosθsinα)
? y = y′cosθ + y′cosα ? (sinθ / sinα)
? y = y′cosθ + x′sinα ? (sinθ / sinα) (from [2])
? y = y′cosθ + x′sinθ
? y = x′sinθ + y′cosθ
因此我們有,
x = x′cosθ ? y′sinθ
y = x′sinθ + y′cosθ


線(xiàn)性變換與基變換的關(guān)系
平移
通過(guò)平移改變坐標(biāo)

從這個(gè)圖來(lái)看,
x′ = x - a
y′ = y + b

[x, y, 1] ? [x/1, y/1] = [x, y]
通過(guò)平移改變基

從圖上看,
x′ = x - a
y′ = y - b

攝像機(jī)外參矩陣


自由度
簡(jiǎn)化矩陣
實(shí)例
設(shè)置
# create a virtual environment in anaconda
conda create -n camera-calibration-python python=3.6 anaconda
conda activate camera-calibration-python
# clone the repository and install dependencies
git clone https://github.com/wingedrasengan927/Image-formation-and-camera-calibration.git
cd Image-formation-and-camera-calibration
pip install -r requirements.txt
pytransform3d:這個(gè)庫(kù)進(jìn)行三維空間中的可視化和轉(zhuǎn)換。 ipympl:它使matplotlib繪圖具有交互性。
實(shí)例直覺(jué)
%matplotlib widget
import matplotlib.pyplot as plt
from utils import *
# rotate an angle of pi/4 along the standard Y axis
angles = [np.pi/4]
order = 'y'
# transalte by the given offset
offset = np.array([0, -8, 0])
# define parameters for the image plane
f = 2
img_size = (7, 7)
# create rotation transformation matrix
R = create_rotation_transformation_matrix(angles, order)
R_ = np.identity(4)
R_[:3, :3] = R
# create translation transformation matrix
T_ = create_translation_matrix(offset)
R_, T_
(array([[ 0.70710678, 0. , -0.70710678, 0. ],
[ 0. , 1. , 0. , 0. ],
[ 0.70710678, 0. , 0.70710678, 0. ],
[ 0. , 0. , 0. , 1. ]]),
array([[ 1., 0., 0., 0.],
[ 0., 1., 0., -8.],
[ 0., 0., 1., 0.],
[ 0., 0., 0., 1.]]))
# create an image grid
xx, yy, Z = create_image_grid(f, img_size)
# convert the image grid to homogeneous coordinates
pt_h = convert_grid_to_homogeneous(xx, yy, Z, img_size)
# transform the homogeneous coordinates
pt_h_transformed = R_ @ T_ @ pt_h
# convert the transformed homogeneous coordinates back to the image grid
xxt, yyt, Zt = convert_homogeneous_to_grid(pt_h_transformed, img_size)
# define axis and figure
fig = plt.figure(figsize=(6, 4))
ax = fig.add_subplot(111,projection='3d')
# set limits
ax.set(xlim=(-10, 5), ylim=(-15, 5), zlim=(0, 10))
# plot the global basis and the transformed camera basis
ax = pr.plot_basis(ax)
ax = pr.plot_basis(ax, R, offset)
# plot the original and transformed image plane
ax.plot_surface(xx, yy, Z, alpha=0.75)
ax.plot_surface(xxt, yyt, Zt, alpha=0.75)
ax.set_title("camera transformation")
ax.set_xlabel("X-axis")
ax.set_ylabel("Y-axis")
ax.set_zlabel("Z-axis")
Text(0.5, 0, 'Z-axis')

E = np.linalg.inv(R_ @ T_)
# remove last row of E
E = E[:-1, :]
cw = np.array([-1/np.sqrt(2), -8, 1/np.sqrt(2), 1]) # homogeneous coordinates of the point wrt the world
cc = E @ cw.reshape(4, 1) # coordinates of the point wrt the camera
cc = cc.flatten()
cc
array([0., 0., 1.])
首先,我們導(dǎo)入必要的庫(kù)。utils.py文件包含所有必要的幫助函數(shù)。%matplotlib widget啟用了ipympl后端,使我們能夠使用繪圖。 接下來(lái),我們定義必要的參數(shù),如角度、旋轉(zhuǎn)順序、平移偏移、焦距和圖像平面的大小。焦距和圖像平面僅用于演示目的,我們將在下一篇文章中詳細(xì)討論它們。 在這里,我們保持簡(jiǎn)單,關(guān)于標(biāo)準(zhǔn)Y軸旋轉(zhuǎn)??/4。然而,我們可以圍繞任何軸進(jìn)行任意數(shù)量的旋轉(zhuǎn)。注意旋轉(zhuǎn)的順序。我們的平移偏移量是[0,-8,0],沿Y軸8個(gè)單位。 使用這些參數(shù),我們?yōu)樾D(zhuǎn)和平移創(chuàng)建變換矩陣。 接下來(lái),我們使用變換矩陣轉(zhuǎn)換最初位于原點(diǎn)的相機(jī)并繪制它。多虧了ipympl,圖表是互動(dòng)的。試著擺弄一下圖表,用不同的視角來(lái)觀看。

接下來(lái),我們通過(guò)對(duì)變換矩陣求逆來(lái)創(chuàng)建基矩陣的變化,即相機(jī)外參矩陣。 最后,我們?nèi)∫粋€(gè)世界坐標(biāo)[-1/√2, -8, 1/√2],然后應(yīng)用基變換的變化,得到相機(jī)的坐標(biāo)為[0, 0, 1]。這是有意義的,因?yàn)樵擖c(diǎn)位于相機(jī)Z軸的正上方。
好消息!
小白學(xué)視覺(jué)知識(shí)星球
開(kāi)始面向外開(kāi)放啦??????
下載1:OpenCV-Contrib擴(kuò)展模塊中文版教程 在「小白學(xué)視覺(jué)」公眾號(hào)后臺(tái)回復(fù):擴(kuò)展模塊中文教程,即可下載全網(wǎng)第一份OpenCV擴(kuò)展模塊教程中文版,涵蓋擴(kuò)展模塊安裝、SFM算法、立體視覺(jué)、目標(biāo)跟蹤、生物視覺(jué)、超分辨率處理等二十多章內(nèi)容。 下載2:Python視覺(jué)實(shí)戰(zhàn)項(xiàng)目52講 在「小白學(xué)視覺(jué)」公眾號(hào)后臺(tái)回復(fù):Python視覺(jué)實(shí)戰(zhàn)項(xiàng)目,即可下載包括圖像分割、口罩檢測(cè)、車(chē)道線(xiàn)檢測(cè)、車(chē)輛計(jì)數(shù)、添加眼線(xiàn)、車(chē)牌識(shí)別、字符識(shí)別、情緒檢測(cè)、文本內(nèi)容提取、面部識(shí)別等31個(gè)視覺(jué)實(shí)戰(zhàn)項(xiàng)目,助力快速學(xué)校計(jì)算機(jī)視覺(jué)。 下載3:OpenCV實(shí)戰(zhàn)項(xiàng)目20講 在「小白學(xué)視覺(jué)」公眾號(hào)后臺(tái)回復(fù):OpenCV實(shí)戰(zhàn)項(xiàng)目20講,即可下載含有20個(gè)基于OpenCV實(shí)現(xiàn)20個(gè)實(shí)戰(zhàn)項(xiàng)目,實(shí)現(xiàn)OpenCV學(xué)習(xí)進(jìn)階。 交流群
歡迎加入公眾號(hào)讀者群一起和同行交流,目前有SLAM、三維視覺(jué)、傳感器、自動(dòng)駕駛、計(jì)算攝影、檢測(cè)、分割、識(shí)別、醫(yī)學(xué)影像、GAN、算法競(jìng)賽等微信群(以后會(huì)逐漸細(xì)分),請(qǐng)掃描下面微信號(hào)加群,備注:”昵稱(chēng)+學(xué)校/公司+研究方向“,例如:”張三 + 上海交大 + 視覺(jué)SLAM“。請(qǐng)按照格式備注,否則不予通過(guò)。添加成功后會(huì)根據(jù)研究方向邀請(qǐng)進(jìn)入相關(guān)微信群。請(qǐng)勿在群內(nèi)發(fā)送廣告,否則會(huì)請(qǐng)出群,謝謝理解~
評(píng)論
圖片
表情

