Halcon routine (monocular camera calibration based on multiple calibration images) detailed explanation-Camera_calibration_multi_image.hdev

I. Introduction

I have already explained the theoretical part of camera calibration in my industrial camera column. Why do we need to calibrate and what parameters are required for calibration? Use a Halcon routine to help understand.
This routine is a relatively classic calibration procedure. The calibration process is basically explained clearly. The calibration image used is the system's own. If you want to do it yourself, you can take a photo and generate it in the Halcon assistant option.

Two, the code

* 设置窗口和字体
ImgPath := '3d_machine_vision/calib/'
dev_close_window ()
dev_open_window (0, 0, 640, 480, 'black', WindowHandle)
dev_update_off ()
dev_set_draw ('margin')
dev_set_line_width (3)
set_display_font (WindowHandle, 22, 'mono', 'true', 'false')
* 
* 相机标定过程
* 
* 生成面阵相机初始参数,参数均为相机已知参数
* (: : 焦距, 畸变因子, Sx, Sy, Cx, Cy, 图像宽度, 图像高度 : CameraParam存储元组)
* (Sx, Sy为传感器芯片上两个相邻单元之间的水平竖直距离,也就是像素的大小,单位为m/像素;Cx, Cy为图像原点的行列坐标,单位为像素;)
gen_cam_par_area_scan_division (0.012, 0, 0.00000375, 0.00000375, 640, 480, 1280, 960, StartCamPar)
*创建Halcon标定数据模型(标定类型,相机数量,标定物数量,模型句柄)
*其作用在于指定相机标定类型,设置标定过程,存储标定数据和结果。
create_calib_data ('calibration_object', 1, 1, CalibDataID)
*设定相机初始参数(句柄,相机参数索引,相机类型,起始相机参数)
*此段程序跑完的句柄见下图。
set_calib_data_cam_param (CalibDataID, 0, [], StartCamPar)
*设定标定板初始参数(句柄,标定板数量索引,标定板描述(文件名或者标定板所有点的三维坐标))此例生成三维坐标系坐标值(x,y,z),其中z为0;坐标原点在finder模式的中心Mark点中心,坐标系的z轴指向标定板,x轴指向右侧,y轴指向下方,视角沿z轴。
set_calib_data_calib_object (CalibDataID, 0, 'calplate_80mm.cpd')//此文件可用记事本打开,里面记录了标定板五个mark模式的点坐标和半径。
calibration marks at y = -0.0290538 m
-0.0374193548387097 -0.0290537554818005 0.000645161290322581

Insert picture description here

*读图,并生成落轮廓和点的特征参数值
NumImages := 7
for I := 1 to NumImages by 1
*读图,路径在C盘用户公共文档里,% 2d是将数字按宽度为2,采用右对齐方式输出,若数据位数不到2位,则左边补空格,所以文件名为calib_0X。
    read_image (Image, ImgPath + 'calib_image_' + I$'02d')
    dev_display (Image)
    *寻找标定板并在模型中设定提取的点和轮廓信息
    *(图像变量,句柄,相机的索引,标定板的索引,计数变量,参数名(使用六边形标定板,设置额外的参数值,可以使图像平滑等),参数值)
    *该算子将标定板每个点的轮廓和点坐标,索引和此图像标定板相对于相机坐标系的位姿提取出来,保存在句柄中。
    *find_caltab在图像中寻找标定板是基于标定板的特征——在一个亮的区域中存在黑色Mark点
    *算子对图像高斯滤波(核大小为SizeGauss),接着阈值分割(与之大小为MarkThresh)将标定板的区域找出来.
    find_calib_object (Image, CalibDataID, 0, 0, I, [], [])
    *从标定数据模型中获取基于轮廓的观测数据
    *(轮廓变量,句柄,返回标定板查找模式的轮廓内含三个参数(marks(返回每一个轮廓,calib(返回查找模式的轮廓),last_caltab(会返回上次成功的查找结果,但会忽略索引信息)),相机索引,标定板索引,变量)
    * 标定板查找器:有两种模式,一种是特殊标记六边形(即一个标记及其六个相邻标记),其中四个或六个标记包含一个孔;另外是带有矩形排列标记的校准板:校准板的边缘在一角有一个三角形。这里是第一种,结果如下图所示。
    get_calib_data_observ_contours (Caltab, CalibDataID, 'caltab', 0, 0, I)
    *从标定数据模型中获取基于点的观测数据
    get_calib_data_observ_points (CalibDataID, 0, 0, I, Row, Column, Index, StartPose)
    dev_set_color ('green')
    dev_display (Caltab)
    dev_set_color ('red')
    *画出坐标的轮廓,轮廓以中心点的方式显示(窗口句柄,中心点行坐标,列坐标,半径)tuple_gen_const(: : Length, Const : Newtuple)(元组长度,常量初始值)
    disp_circle (WindowHandle, Row, Column, gen_tuple_const(|Row|,1.5))
endfor

Insert picture description here
For more details about find_calib, please see the link:
Halcon Camera Calibration

*最重要的算子:相机标定,通过同步的最小化过程确定所有相机参数;
*计算相机内外参矩阵,原理见链接(https://blog.csdn.net/baidu_35536188/article/details/109772056)
*(句柄,优化后的反投影的均方根误差(RMSE),单位为像素,该误差用来反映优化是否成功,越接近0表示效果越好)
calibrate_cameras (CalibDataID, Errors)
*获取相机标定数据--内参值,将其存在CamParam上。
get_calib_data (CalibDataID, 'camera', 0, 'params', CamParam)
*获取标定板数据,将其第一幅图的位姿存在Pose里。
get_calib_data (CalibDataID, 'calib_obj_pose', [0,1], 'pose', Pose)

* To take the thickness of the calibration plate into account, the z-value
* of the origin given by the camera pose has to be translated by the
* thickness of the calibration plate.
* Deactivate the following line if you do not want to add the correction.
* *设置新的坐标原点。在Z轴坐标加0.02,主要是考虑标定板的厚度,该算子通过DX、DY和DZ给定的向量转换3D poseIn的原点,并以poseNewOrigin形式返回结果。
set_origin_pose (Pose, 0, 0, 0.002, Pose)
* measure the distance between the pitch lines

Insert picture description here
Insert picture description here

read_image (Image, ImgPath + 'ruler')
dev_display (Image)
* 准备提取垂直于矩形长轴的直边。 矩形的中心在(Row,Column),Phi为矩形主轴的角度,Lenth1和Lenth2为两轴的长度,即矩形两边长度的一半。
* (矩形中心点的行坐标,列坐标,矩形的纵轴与水平的角度(弧度),矩形的半宽,矩形的半高,图像的宽,高,要使用的插值类型,测量对象句柄)
gen_measure_rectangle2 (690, 680, rad(-0.25), 480, 8, 1280, 960, 'bilinear', MeasureHandle)
*提取垂直于矩形或环形弧的边缘对
*(图像,句柄,高斯平滑的西格玛参数值,最小边缘幅度,灰度值转换的类型以确定边缘如何成对,边缘对第一条边的中心的Row坐标,列坐标,
边缘对第一条边的边缘幅度(带符号),边缘对第二条边的中心的Row坐标,列坐标,边缘幅度,边缘对内部之间的距离,边缘间距离)
measure_pairs (Image, MeasureHandle, 0.5, 5, 'all', 'all', RowEdgeFirst, ColumnEdgeFirst, AmplitudeFirst, RowEdgeSecond, ColumnEdgeSecond, AmplitudeSecond, IntraDistance, InterDistance)
Row := (RowEdgeFirst + RowEdgeSecond) / 2.0
Col := (ColumnEdgeFirst + ColumnEdgeSecond) / 2.0
*显示X点,如下图所示。
disp_cross (WindowHandle, Row, Col, 20, rad(45))
*将图像点转换为世界坐标系的z=0平面
*(相机内参,位姿,行,列,单位,世界坐标系的X坐标点,Y坐标点)
image_points_to_world_plane (CamParam, Pose, Row, Col, 'mm', X1, Y1)
*计算两点间的距离
distance_pp (X1[0:11], Y1[0:11], X1[1:12], Y1[1:12], Distance)
*求平均距离和
tuple_mean (Distance, MeanDistance)
*计算距离的标准差
tuple_deviation (Distance, DeviationDistance)
disp_message (WindowHandle, 'Mean distance: ' + MeanDistance$'.3f' + 'mm +/- ' + DeviationDistance$'.3f' + 'mm', 'window', 30, 60, 'yellow', 'false')

Insert picture description here

Three, summary

The calibration steps of Halcon are summarized as follows:

  1. gen_cam_par_area_scan_division: Generate camera parameter matrix;
  2. create_calib_data, set_calib_data_cam_param, set_calib_data_calib_object: Create a calibration data model, and set the parameter values ​​of the camera and calibration board, including the initial internal parameters of the camera and the coordinate system of the calibration board.
  3. find_calib_object, find the calibration board in multiple calibration images, and extract its contour and point features to generate a pose matrix (7-dimensional) relative to the camera coordinate system.
  4. calibrate_cameras, calculate the internal and external parameters, and calculate the error value.
  5. Read the actual map to be measured, set the rectangular area of ​​interest, calculate and mark the edge point coordinates through edge detection, and convert the two-dimensional coordinates into three-dimensional coordinates according to the calculated internal and external parameters, and calculate the deviation.
    In summary, I feel that the HALCON calibration process is relatively clear, and the procedures in the key operators still need to be understood.

Guess you like

Origin blog.csdn.net/baidu_35536188/article/details/110004794