我正在尝试验证我的相机校准,所以我想纠正校准图像。我希望这将涉及使用呼叫 warpPerspective
但我没有看到一个明显的功能,它采用相机矩阵,旋转和平移矢量生成此调用的透视矩阵。
基本上我想做所描述的过程 这里 (特别是看到最后的图像),但从已知的相机模型和姿势开始。
是否有一个简单的函数调用,它接受相机的内在和外在参数,并计算透视矩阵以供使用 warpPerspective
?
我会打电话的 warpPerspective
打完电话后 undistort
在图像上。
原则上,我可以通过求解在顶部定义的方程组来推导出解 opencv相机校准文档 在指定约束之后 Z=0
,但我认为必须有一个固定的例程,允许我对我的测试图像进行正射校正。
在我的搜索中,我发现很难通过所有的立体声校准结果 - 我只有一台摄像机,但想要在我只看平面测试模式的约束条件下纠正图像。
实际上,没有必要涉及正交相机。以下是如何获得适当的透视变换。
如果您使用校准相机 cv::calibrateCamera
,你获得了一个相机矩阵 K
透镜畸变系数的向量 D
对于您的相机,以及对于您使用的每个图像,旋转矢量 rvec
(您可以将其转换为3x3矩阵 R
运用 cv::rodrigues
, DOC)和翻译矢量 T
。考虑其中一个图像和相关的图像 R
和 T
。你打电话后 cv::undistort
使用失真系数,图像将像投影矩阵的相机一样 K * [ R | T ]
。
基本上(如@DavidNilosek直觉),你想要取消旋转并获得图像,好像它是由形式的投影矩阵获取 K * [ I | -C ]
哪里 C=-R.inv()*T
是相机的位置。为此,您必须应用以下转换:
Hr = K * R.inv() * K.inv()
唯一可能的问题是扭曲的图像可能会超出图像平面的可见部分。因此,您可以使用其他翻译来解决该问题,如下所示:
[ 1 0 | ]
Ht = [ 0 1 | -K*C/Cz ]
[ 0 0 | ]
其中Cz是沿Oz轴的C分量。
最后,根据上面的定义, H = Ht * Hr
是对所考虑图像的整流透视变换。
这是“解决方程组”的意思草图(在Python中):
import cv2
import scipy # I use scipy by habit; numpy would be fine too
#rvec= the rotation vector
#tvec = the translation *emphasized text*matrix
#A = the camera intrinsic
def unit_vector(v):
return v/scipy.sqrt(scipy.sum(v*v))
(fx,fy)=(A[0,0], A[1,1])
Ainv=scipy.array( [ [1.0/fx, 0.0, -A[0,2]/fx],
[ 0.0, 1.0/fy, -A[1,2]/fy],
[ 0.0, 0.0, 1.0] ], dtype=scipy.float32 )
R=cv2.Rodrigues( rvec )
Rinv=scipy.transpose( R )
u=scipy.dot( Rinv, tvec ) # displacement between camera and world coordinate origin, in world coordinates
# corners of the image, for here hard coded
pixel_corners=[ scipy.array( c, dtype=scipy.float32 ) for c in [ (0+0.5,0+0.5,1), (0+0.5,640-0.5,1), (480-0.5,640-0.5,1), (480-0.5,0+0.5,1)] ]
scene_corners=[]
for c in pixel_corners:
lhat=scipy.dot( Rinv, scipy.dot( Ainv, c) ) #direction of the ray that the corner images, in world coordinates
s=u[2]/lhat[2]
# now we have the case that (s*lhat-u)[2]==0,
# i.e. s is how far along the line of sight that we need
# to move to get to the Z==0 plane.
g=s*lhat-u
scene_corners.append( (g[0], g[1]) )
# now we have: 4 pixel_corners (image coordinates), and 4 corresponding scene_coordinates
# can call cv2.getPerspectiveTransform on them and so on..