互补格雷码+相移码实现物体三维重建,整个重建过程可以分为6个步骤:
Github项目已建立,项目中提供了实验数据,供大家学习,欢迎大家star⭐️收藏!
求解绝对相位的具体原理和代码可以参考(超详细)互补格雷码+相移码求解包裹相位(Python实现),下面的内容主要介绍获得绝对相位后如何计算三维坐标点,重在理论推导。
获得绝对相位后,就可以建立相机-投影仪像素坐标之间的对应关系,假设相机像素坐标为 ( u c , v c ) (u_{c},v_{c}) (uc,vc),投影仪像素坐标为 ( u p , v p ) (u_{p},v_{p}) (up,vp),相移码一个周期所占的像素个数为 T T T,那么通过相位关系可以得到如下关系式:
Φ ( u p , v p ) = Φ ( u c , v c ) \Phi(u_{p},v_{p})=\Phi(u_{c},v_{c}) Φ(up,vp)=Φ(uc,vc)
Φ ( u p , v p ) = 2 π u p T \Phi(u_{p},v_{p})=\frac{2\pi u_{p}}{T} Φ(up,vp)=T2πup
可以得到如下关系式:
u p = Φ ( u p , v p ) ∗ T 2 π = Φ ( u c , v c ) ∗ T 2 π u_{p}=\frac{\Phi(u_{p},v_{p}) \ast T}{2\pi}=\frac{\Phi(u_{c},v_{c}) \ast T}{2\pi} up=2πΦ(up,vp)∗T=2πΦ(uc,vc)∗T
根据相机模型:
[ u c v c 1 ] = [ f c x 0 u c 0 0 0 f c y v c 0 0 0 0 1 0 ] 1 Z c [ R w → c t w → c 0 1 ] [ X w Y w Z w 1 ] = [ f c x 0 u c 0 0 f c y v c 0 0 0 1 ] [ X c / Z c Y c / Z c 1 ] = [ f c x 0 u c 0 0 f c y v c 0 0 0 1 ] [ x c y c 1 ] \begin{aligned} \begin{bmatrix} u_{c} \\ v_{c} \\ 1 \end{bmatrix} &= \begin{bmatrix} f_{cx} & 0 & u_{c0} & 0 \\ 0 & f_{cy} & v_{c0} & 0 \\ 0 & 0 & 1 & 0 \end{bmatrix} \frac{1}{Z_{c}} \begin{bmatrix} R_{w\rightarrow c} & t_{w\rightarrow c} \\ 0 & 1 \end{bmatrix} \begin{bmatrix} X_{w} \\ Y_{w} \\ Z_{w} \\ 1 \end{bmatrix} \\ &= \begin{bmatrix} f_{cx} & 0 & u_{c0} \\ 0 & f_{cy} & v_{c0} \\ 0 & 0 & 1 \end{bmatrix}\begin{bmatrix}X_{c}/Z_{c} \\ Y_{c}/Z_{c} \\ 1 \end{bmatrix} \\ &= \begin{bmatrix} f_{cx} & 0 & u_{c0} \\ 0 & f_{cy} & v_{c0} \\ 0 & 0 & 1 \end{bmatrix}\begin{bmatrix}x_{c} \\ y_{c} \\ 1 \end{bmatrix} \end{aligned} ⎣⎡ucvc1⎦⎤=⎣⎡fcx000fcy0uc0vc01000⎦⎤Zc1[Rw→c0tw→c1]⎣⎢⎢⎡XwYwZw1⎦⎥⎥⎤=⎣⎡fcx000fcy0uc0vc01⎦⎤⎣⎡Xc/ZcYc/Zc1⎦⎤=⎣⎡fcx000fcy0uc0vc01⎦⎤⎣⎡xcyc1⎦⎤
可以得到:
{ X c = x c ∗ Z c Y c = y c ∗ Z c Z c = Z c \left\{\begin{array}{ll}X_{c}=x_{c} \ast Z_{c} \\ Y_{c}=y_{c} \ast Z_{c} \\ Z_{c}=Z_{c} \end{array} \right. ⎩⎨⎧Xc=xc∗ZcYc=yc∗ZcZc=Zc
由相机内参数可得
{ u c = f c x ∗ x c + u c 0 v c = f c y ∗ y c + v c 0 ⇒ { x c = ( u c − u c 0 ) / f c x y c = ( v c − v c 0 ) / f c y \left\{\begin{array}{ll}u_{c}=f_{cx} \ast x_{c} + u_{c0} \\ v_{c}=f_{cy} \ast y_{c} + v_{c0}\end{array} \right. \Rightarrow \left\{\begin{array}{ll}x_{c}=(u_{c} - u_{c0})/f_{cx} \\ y_{c}= (v_{c} - v_{c0})/f_{cy}\end{array} \right. {uc=fcx∗xc+uc0vc=fcy∗yc+vc0⇒{xc=(uc−uc0)/fcxyc=(vc−vc0)/fcy
投影仪可以看作逆相机,因此投影仪模型与相机模型相同,同理可得:
{ X p = x p ∗ Z p Y p = y p ∗ Z p Z p = Z p \left\{\begin{array}{ll}X_{p}=x_{p} \ast Z_{p} \\ Y_{p}=y_{p} \ast Z_{p} \\ Z_{p}=Z_{p} \end{array} \right. ⎩⎨⎧Xp=xp∗ZpYp=yp∗ZpZp=Zp
{ u p = f p x ∗ x p + u p 0 v p = f p y ∗ y p + v p 0 ⇒ { x p = ( u p − u p 0 ) / f p x y p = ∗ ( v p − v p 0 ) / f p y \left\{\begin{array}{ll}u_{p}=f_{px} \ast x_{p} + u_{p0} \\ v_{p}=f_{py} \ast y_{p} + v_{p0}\end{array} \right. \Rightarrow \left\{\begin{array}{ll}x_{p}=(u_{p} - u_{p0})/f_{px} \\ y_{p}= \ast (v_{p} - v_{p0})/f_{py}\end{array} \right. {up=fpx∗xp+up0vp=fpy∗yp+vp0⇒{xp=(up−up0)/fpxyp=∗(vp−vp0)/fpy
设相机投影仪系统的外参数为 R c → p = [ r 11 r 12 r 13 r 21 r 22 r 23 r 31 r 32 r 33 ] 、 t c → p = [ t x t y t z ] R_{c \rightarrow p}=\begin{bmatrix}r_{11} & r_{12} & r_{13} \\ r_{21} & r_{22} & r_{23} \\ r_{31} & r_{32} & r_{33} \end{bmatrix}、t_{c \rightarrow p}=\begin{bmatrix}t_{x} & t_{y} & t_{z}\end{bmatrix} Rc→p=⎣⎡r11r21r31r12r22r32r13r23r33⎦⎤、tc→p=[txtytz]
由下面式子,
[ X p Y p Z p ] = R c → p [ X c Y c Z c ] + t c → p \begin{bmatrix}X_{p} \\ Y_{p} \\ Z_{p} \end{bmatrix} = R_{c \rightarrow p}\begin{bmatrix}X_{c} \\ Y_{c} \\ Z_{c} \end{bmatrix} + t_{c \rightarrow p} ⎣⎡XpYpZp⎦⎤=Rc→p⎣⎡XcYcZc⎦⎤+tc→p
展开得:
{ X p = r 11 X c + r 12 Y c + r 13 Z c + t x = ( r 11 x c + r 12 y c + r 13 ) Z c + t x Y p = r 21 X c + r 22 Y c + r 23 Z c + t y = ( r 21 x c + r 22 y c + r 23 ) Z c + t y Z p = r 31 X c + r 32 Y c + r 33 Z c + t z = ( r 31 x c + r 32 y c + r 33 ) Z c + t z \left\{\begin{array}{ll} X_{p} = r_{11}X_{c} + r_{12}Y_{c} + r_{13}Z_{c} + t_{x} = (r_{11}x_{c} + r_{12}y_{c} + r_{13})Z_{c} + t_{x} \\ Y_{p} = r_{21}X_{c} + r_{22}Y_{c} + r_{23}Z_{c} + t_{y} = (r_{21}x_{c} + r_{22}y_{c} + r_{23})Z_{c} + t_{y} \\ Z_{p} = r_{31}X_{c} + r_{32}Y_{c} + r_{33}Z_{c} + t_{z} = (r_{31}x_{c} + r_{32}y_{c} + r_{33})Z_{c} + t_{z} \end{array} \right. ⎩⎨⎧Xp=r11Xc+r12Yc+r13Zc+tx=(r11xc+r12yc+r13)Zc+txYp=r21Xc+r22Yc+r23Zc+ty=(r21xc+r22yc+r23)Zc+tyZp=r31Xc+r32Yc+r33Zc+tz=(r31xc+r32yc+r33)Zc+tz
又由相机投影仪像素坐标得关系有:
{ u p = f p x ∗ x p + u p 0 u p = Φ ( u c , v c ) ∗ T 2 π ⇒ f p x ∗ x p + u p 0 = Φ ( u c , v c ) ∗ T 2 π \left\{\begin{array}{ll}u_{p}=f_{px} \ast x_{p} + u_{p0} \\ u_{p} =\frac{\Phi(u_{c},v_{c}) \ast T}{2\pi}\end{array} \right. \Rightarrow f_{px} \ast x_{p} + u_{p0} = \frac{\Phi(u_{c},v_{c}) \ast T}{2\pi} {up=fpx∗xp+up0up=2πΦ(uc,vc)∗T⇒fpx∗xp+up0=2πΦ(uc,vc)∗T
最终得到连联立方程组:
{ x p ∗ Z p = ( r 11 x c + r 12 y c + r 13 ) Z c + t x Z p = ( r 31 x c + r 32 y c + r 33 ) Z c + t z f p x ∗ x p + u p 0 = Φ ( u c , v c ) ∗ T 2 π \left\{\begin{array}{ll} x_{p} \ast Z_{p} = (r_{11}x_{c} + r_{12}y_{c} + r_{13})Z_{c} + t_{x} \\ Z_{p} = (r_{31}x_{c} + r_{32}y_{c} + r_{33})Z_{c} + t_{z} \\ f_{px} \ast x_{p} + u_{p0} = \frac{\Phi(u_{c},v_{c}) \ast T}{2\pi} \end{array} \right. ⎩⎨⎧xp∗Zp=(r11xc+r12yc+r13)Zc+txZp=(r31xc+r32yc+r33)Zc+tzfpx∗xp+up0=2πΦ(uc,vc)∗T
解得:
Z c = x p t z − t x J x − J z x p Z_{c} = \frac{x_{p}t_{z}-t_{x}}{J_{x}-J_{z}x_{p}} Zc=Jx−Jzxpxptz−tx
J x = ( r 11 x c + r 12 y c + r 13 ) J_{x} = (r_{11}x_{c} + r_{12}y_{c} + r_{13}) Jx=(r11xc+r12yc+r13);
J z = ( r 31 x c + r 32 y c + r 33 ) J_{z} = (r_{31}x_{c} + r_{32}y_{c} + r_{33}) Jz=(r31xc+r32yc+r33);
x p = ( Φ ( u c , v c ) ∗ T 2 π − u p 0 ) / f p x x_{p} = (\frac{\Phi(u_{c},v_{c}) \ast T}{2\pi} - u_{p0})/f_{px} xp=(2πΦ(uc,vc)∗T−up0)/fpx。
相机坐标系下的得三维坐标点为:
{ X c = x c ∗ Z c Y c = y c ∗ Z c Z c = x p t z − t x J x − J z x p \left\{\begin{array}{ll}X_{c} = x_{c} \ast Z_{c} \\ Y_{c} = y_{c} \ast Z{c} \\ Z_{c} = \frac{x_{p}t_{z}-t_{x}}{J_{x}-J_{z}x_{p}} \end{array} \right. ⎩⎨⎧Xc=xc∗ZcYc=yc∗ZcZc=Jx−Jzxpxptz−tx