opencv标定实现总结(圆点,棋盘格和非对称圆点)
opencv官方下载的时候会给我们安装一段标准标定代码,位置如下:
安装与配置教程见:64位OpenCV库生成32位库并配置环境变量
使用方法
1,文件意义
camera_calibration.cpp
是opencv标定源码,in_VID5
是标定参数设置文件,主要的修改就是在这里,通过代码读取,这里其实可以直接写入代码不要这个文件,当然那时后话了,out_camera_data
保存标定结果,VID5
存放标定图片路径,这些文件的修改见下面介绍
2,标定版支持种类
opencv官方标定是支持3种标定板的,棋盘格板,非对称圆点,对称圆点,只要修改部分代码即可标定三种板子,分别如下图:
3,创建工程
我直接创建了一个控制台应用,把源码复制进源.cpp,然后就是相关的配置了,可以参考64位OpenCV库生成32位库并配置环境变量
配置好了之后运行之前就不会报错之类的,然后就可以进行下一步相应的修改。
源码中需要进行的修改如下:
把"{@settings |default.xml| input setting file }"
和"Usage: camera_calibration [configuration_file -- default ./default.xml]\n"
中的default改成我们的文件名字,比如我的叫做in_VID5circle.xlm,于是就对应修改了,我有两个文件不同标定板是为了方便,其实一个就够了,具体见下方修改,改完之后就可以读取我们在那个文件设置的标定参数了。
const String keys
= "{help h usage ? | | print this message }"
"{@settings |in_VID5circle.xml| input setting file }"
"{d | | actual distance between top-left and top-right corners of "
"the calibration grid }"
"{winSize | 11 | Half of search window for cornerSubPix }";
CommandLineParser parser(argc, argv, keys);
parser.about("This is a camera calibration sample.\n"
"Usage: camera_calibration [configuration_file -- default ./in_VID5circlr.xml]\n"
"Near the sample file you'll find the configuration file, which has detailed help of "
"how to edit it. It may be any OpenCV supported file format XML/YAML.");
4,设置标定参数
右击标定参数文件,打开方式选择写字板
下面是里面的内容,我现在是用的圆点,需要修改的地方:
1,<BoardSize_Width> 7 </BoardSize_Width>
和<BoardSize_Height> 7 </BoardSize_Height>
那里我设置为7,我用的是上面的对称圆点标定板,7X7的,一行一列都是7个圆点,(如果是棋盘格,就是设置为每行每列的内点个数)
2,<Square_Size>15</Square_Size>
我设置为15,如果用圆点的,这个的意义是半个圆心距,单位毫米,参考的下面网址中的回答(如果是棋盘格,就是棋盘格的一格的宽度,实际上并没有什么影响,有默认值50,单位毫米)
3,<Calibrate_Pattern>"CIRCLES_GRID"</Calibrate_Pattern>
是指定图案类型:下面文件中枚举的三个分别对应棋盘格,对称圆点,非对称圆点,我用对称圆点选择CIRCLES_GRID
(如果棋盘格就选择CHESSBOARD
)
4,<Input>"D:/E_Dragon/OPENCV/opencv_cal/VID5circle.xml"</Input>
这个地方是给定图片存储文件的路径(如下图)
这个文件的书写方式(和图片对应,用绝对路径比较安全):
用一个文件夹保存:
5,<Calibrate_NrOfFrameToUse>18</Calibrate_NrOfFrameToUse>
这里是图片数量,我放了18张图片
6,<Write_outputFileName>"out_camera_data.yml"</Write_outputFileName>
这是输出文件路径,可以自己设定
其他的标定参数就不必要改了,用默认的就行了,如果想改就看一下英文解释修改也不难。下面就是完整的标定参数文件。
<?xml version="1.0"?>
<opencv_storage>
<Settings>
<!-- Number of inner corners per a item row and column. (square, circle) -->
<BoardSize_Width> 7 </BoardSize_Width>
<BoardSize_Height> 7 </BoardSize_Height>
<!-- The size of a square in some user defined metric system (pixel, millimeter)-->
<Square_Size>15</Square_Size>
<!-- The type of input used for camera calibration. One of: CHESSBOARD CIRCLES_GRID ASYMMETRIC_CIRCLES_GRID -->
<Calibrate_Pattern>"CIRCLES_GRID"</Calibrate_Pattern>
<!-- The input to use for calibration.
To use an input camera -> give the ID of the camera, like "1"
To use an input video -> give the path of the input video, like "/tmp/x.avi"
To use an image list -> give the path to the XML or YAML file containing the list of the images, like "/tmp/circles_list.xml"
-->
<Input>"D:/E_Dragon/OPENCV/opencv_cal/VID5circle.xml"</Input>
<!-- If true (non-zero) we flip the input images around the horizontal axis.-->
<Input_FlipAroundHorizontalAxis>0</Input_FlipAroundHorizontalAxis>
<!-- Time delay between frames in case of camera. -->
<Input_Delay>100</Input_Delay>
<!-- How many frames to use, for calibration. -->
<Calibrate_NrOfFrameToUse>18</Calibrate_NrOfFrameToUse>
<!-- Consider only fy as a free parameter, the ratio fx/fy stays the same as in the input cameraMatrix.
Use or not setting. 0 - False Non-Zero - True-->
<Calibrate_FixAspectRatio> 1 </Calibrate_FixAspectRatio>
<!-- If true (non-zero) tangential distortion coefficients are set to zeros and stay zero.-->
<Calibrate_AssumeZeroTangentialDistortion>1</Calibrate_AssumeZeroTangentialDistortion>
<!-- If true (non-zero) the principal point is not changed during the global optimization.-->
<Calibrate_FixPrincipalPointAtTheCenter> 1 </Calibrate_FixPrincipalPointAtTheCenter>
<!-- The name of the output log file. -->
<Write_outputFileName>"out_camera_data.yml"</Write_outputFileName>
<!-- If true (non-zero) we write to the output file the feature points.-->
<Write_DetectedFeaturePoints>1</Write_DetectedFeaturePoints>
<!-- If true (non-zero) we write to the output file the extrinsic camera parameters.-->
<Write_extrinsicParameters>1</Write_extrinsicParameters>
<!-- If true (non-zero) we write to the output file the refined 3D target grid points.-->
<Write_gridPoints>1</Write_gridPoints>
<!-- If true (non-zero) we show after calibration the undistorted images.-->
<Show_UndistortedImage>1</Show_UndistortedImage>
<!-- If true (non-zero) will be used fisheye camera model.-->
<Calibrate_UseFisheyeModel>0</Calibrate_UseFisheyeModel>
<!-- If true (non-zero) distortion coefficient k1 will be equals to zero.-->
<Fix_K1>0</Fix_K1>
<!-- If true (non-zero) distortion coefficient k2 will be equals to zero.-->
<Fix_K2>0</Fix_K2>
<!-- If true (non-zero) distortion coefficient k3 will be equals to zero.-->
<Fix_K3>0</Fix_K3>
<!-- If true (non-zero) distortion coefficient k4 will be equals to zero.-->
<Fix_K4>1</Fix_K4>
<!-- If true (non-zero) distortion coefficient k5 will be equals to zero.-->
<Fix_K5>1</Fix_K5>
</Settings>
</opencv_storage>
5,运行代码验证
所有的都设置好了之后,编译运行,结果如下:会显示标定过程
最后会显示矫正之后的图像,按下Enter键切换下一张
应用台窗口会打印出误差和是否成功
打开结果文件out_camera_data
,已经写入了标定结果。