相机标定Opencv实现以及标定流程(三)
7. 查看标定效果——利用标定结果对棋盘图进行矫正
利用求得的相机的内参和外参数据,可以对图像进行畸变的矫正,这里有两种方法可以达到矫正的目的,分别说明一下。
方法一:使用initUndistortRectifyMap和remap两个函数配合实现。
initUndistortRectifyMap用来计算畸变映射,remap把求得的映射应用到图像上。
initUndistortRectifyMap的函数原型:
1
2
3
4
|
//! initializes maps for cv::remap() to correct lens distortion and
optionally rectify the image
CV_EXPORTS_W
void
initUndistortRectifyMap( InputArray cameraMatrix, InputArray
distCoeffs,
InputArray
R, InputArray newCameraMatrix,
Size
size,
int
m1type, OutputArray map1, OutputArray map2 );
|
第一个参数cameraMatrix为之前求得的相机的内参矩阵;
第二个参数distCoeffs为之前求得的相机畸变矩阵;
第三个参数R,可选的输入,是第一和第二相机坐标之间的旋转矩阵;
第四个参数newCameraMatrix,输入的校正后的3X3摄像机矩阵;
第五个参数size,摄像机采集的无失真的图像尺寸;
第六个参数m1type,定义map1的数据类型,可以是CV_32FC1或者CV_16SC2;
第七个参数map1和第八个参数map2,输出的X/Y坐标重映射参数;
remap函数原型:
1
2
3
4
5
|
//! warps the image using the precomputed maps. The maps are stored
in either floating-point or integer fixed-point format
CV_EXPORTS_W
void
remap( InputArray src, OutputArray dst,
InputArray
map1, InputArray map2,
int
interpolation,
int
borderMode=BORDER_CONSTANT,
const
Scalar& borderValue=Scalar());
|
第一个参数src,输入参数,代表畸变的原始图像;
第二个参数dst,矫正后的输出图像,跟输入图像具有相同的类型和大小;
第三个参数map1和第四个参数map2,X坐标和Y坐标的映射;
第五个参数interpolation,定义图像的插值方式;
第六个参数borderMode,定义边界填充方式;
方法二:使用undistort函数实现
undistort函数原型:
1
2
3
4
5
|
//! corrects lens distortion for the given camera matrix and
distortion coefficients
CV_EXPORTS_W
void
undistort( InputArray src, OutputArray dst,
InputArray
cameraMatrix,
InputArray
distCoeffs,
InputArray
newCameraMatrix=noArray() );
|
第一个参数src,输入参数,代表畸变的原始图像;
第二个参数dst,矫正后的输出图像,跟输入图像具有相同的类型和大小;
第三个参数cameraMatrix为之前求得的相机的内参矩阵;
第四个参数distCoeffs为之前求得的相机畸变矩阵;
第五个参数newCameraMatrix,默认跟cameraMatrix保持一致;
方法一相比方法二执行效率更高一些,推荐使用。
以下是使用某一张标定图使用方法一和方法二进行矫正的效果图对比。
原始标定图像:
方法一,使用initUndistortRectifyMap和remap实现矫正效果:
方法二,使用undistort函数实现矫正效果:
两个方法从矫正效果上看,结果是一致的。
|
|