1. Halcon 单相机标定

Halcon 单相机标定

      • 示例

示例

1. Halcon 单相机标定_第1张图片

* 标定文件路径
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')
* 
* 相机标定.
* 
* 1. 根据畸变模型生成参数数组,例程选择的是area_scan_division模型(适用场景:精度不高,标定图片数量少),所以有9参数
* area_scan_polynomial精度较高,对径向畸变、切向畸变都进行校正
* 参数 CameraParam := ['area_scan_division',Focus,Kappa,Sx,Sy,Cx,Cy,ImageWidth,ImageHeight]
* Focus: 代表焦距,按照镜头参数进行填写,远心镜头填写0* Kappa: 代表畸变大小,因为在标定之前,所以填写0* Sx,Sy: 代表像元大小,即像元尺寸,产找相应相机参数即可;0.00000375m,0.000375cm,0.00375mm,3.75um;上面的单位都是m
* Cx,Cy: 代表图像的坐标中心,即图像像素尺寸宽高的一半;
* ImageWidth, ImageHeight: 代表图像的宽和高
gen_cam_par_area_scan_division (0.012, 0, 0.00000375, 0.00000375, 640, 480, 1280, 960, StartCamPar)
* 创建标定数据模型
* CalibSetup:'calibration_object'用于标定一个或多个相机的内参以及相机位姿,其他的四个用于手眼标定
create_calib_data ('calibration_object', 1, 1, CalibDataID)
* 2. 在标定数据模型中写入标定的类型以及初始相机参数,StartCamPar 第一步已经生成,所以直接使用即可
set_calib_data_cam_param (CalibDataID, 0, [], StartCamPar)
* 3. 在标定模型中定义标定的目标,即标定板文件的相关配置,引号中应该是内置的,未找到相应文件
set_calib_data_calib_object (CalibDataID, 0, 'calplate_80mm.cpd')
* 5. 标定板识别
NumImages := 7
for I := 1 to NumImages by 1
    * 读取图像
    read_image (Image, ImgPath + 'calib_image_' + I$'02d')
    * 显示图像
    dev_display (Image)
    * 找到标定板,并向标定数据模型中写入提取到的点和finder pattern 轮廓,可以到帮助里面仔细看下finder pattern的介绍
    find_calib_object (Image, CalibDataID, 0, 0, I, [], [])
    * 从标定数据模型中获取提取到的 finder pattern 轮廓
    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')
    disp_circle (WindowHandle, Row, Column, gen_tuple_const(|Row|,1.5))
endfor
* 6. 通过标定数据模型计算相机的内参和外参,Errors:标定后投影的均方根误差,单位像素
calibrate_cameras (CalibDataID, Errors)
* 7. 获取标定数据模型中存储或计算的数据,CamParam中的数值与1中的相应项对应
get_calib_data (CalibDataID, 'camera', 0, 'params', CamParam)
* The reference image, i.e., the image in which the calibratio
* plate is located on the ruler is the first image

* 8. 获取相机的位姿
get_calib_data (CalibDataID, 'calib_obj_pose', [0,1], 'pose', Pose)
* To take the thickness of the calibration plate into account, the z-valu
* 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.
* 9. 如果将标定板的厚度考虑进去,请执行下面一条命令
set_origin_pose (Pose, 0, 0, 0.002, Pose)
* measure the distance between the pitch lines
* 10. 读取一张测试图像
read_image (Image, ImgPath + 'ruler')
dev_display (Image)
* 11. 创建一个测量句柄
gen_measure_rectangle2 (690, 680, rad(-0.25), 480, 8, 1280, 960, 'bilinear', MeasureHandle)
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
disp_cross (WindowHandle, Row, Col, 20, rad(45))
* 12.将像素左边转换到实际坐标系中
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')

1. Halcon 单相机标定_第2张图片
1. Halcon 单相机标定_第3张图片

你可能感兴趣的:(3D,计算机视觉,Halcon,标定,相机)