笔记(HALCON相机标定代码)

**创建标定板
gen_caltab(7,7,0.008,0.5,'48_48mm.descr','48_48mm.ps')
           
*=======标定内参
dev_close_window ()
dev_open_window (0, 0, 652, 494, 'black', WindowHandle)
dev_update_off ()
dev_set_draw ('margin')
dev_set_line_width (3)
OpSystem := environment('OS')
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
           
*标定相机
StartCamPar := [0.0,0.0,0.0000299,0.0000299,4896/2,3264/2,4896,3264]
create_calib_data ('calibration_object', 1, 1, CalibDataID)
set_calib_data_cam_param (CalibDataID, 0, 'area_scan_telecentric_division', StartCamPar)
set_calib_data_calib_object (CalibDataID, 0, '48_48mm.descr')
           

for index := 1 to 13 by 1
    read_image (Image, '标定20/' + index + '.png')
    get_image_size(Image, Width, Height)
    dev_display (Image)
    find_calib_object (Image, CalibDataID, 0, 0, index, [], [])
    get_calib_data_observ_contours (Caltab, CalibDataID, 'caltab', 0, 0, index)
    dev_set_color ('green')
    dev_display (Caltab)
endfor
           
calibrate_cameras (CalibDataID, Error)
get_calib_data (CalibDataID, 'camera', 0, 'params', CamParam)
get_calib_data (CalibDataID, 'calib_obj_pose', [0,1], 'pose', PoseCalib)
           
*输出计算的相机内参
write_cam_par (CamParam, 'camera_parameters.dat')
           
           
Message:= '相机内参已经写入文件中'
disp_message (WindowHandle, Message, 'window', 12, 12, 'red', 'false')
clear_calib_data (CalibDataID)
stop()
           
           
*=====标定外参
dev_set_draw ('margin')
dev_set_line_width (1)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
           
*从文件中读取内参 存储文件:camera_parameters.dat
try
    read_cam_par ('camera_parameters.dat', CamParam)
catch (Exception)
    stop ()
endtry
           
*开始计算
open_file('data.csv','output', FileHandle)
fwrite_string(FileHandle,'Dis_pix*0.0299204,Dis_m*1000,Distance')
fnew_line (FileHandle)
close_file(FileHandle)
           
*选择一张作为标定作为最终标定位姿(任意一张都可以)
index:=1
read_image (Image,'标定20/'+index+'.png')
dev_display (Image)
CaltabName := '48_48mm.descr'
create_calib_data ('calibration_object', 1, 1, CalibDataID)

set_calib_data_cam_param (CalibDataID, 0, 'area_scan_telecentric_division', CamParam)
set_calib_data_calib_object (CalibDataID, 0, CaltabName)
find_calib_object (Image, CalibDataID, 0, 0, 1, [], [])
get_calib_data_observ_contours (Caltab, CalibDataID, 'caltab', 0, 0, 1)
get_calib_data_observ_points (CalibDataID, 0, 0, 1, RCoord, CCoord, Index, PoseForCalibrationPlate)
dev_set_color ('green')
dev_display (Caltab)
dev_set_color ('red')
disp_caltab (WindowHandle, CaltabName, CamParam, PoseForCalibrationPlate, 1)
dev_set_line_width (1)
disp_circle (WindowHandle, RCoord, CCoord, gen_tuple_const(|RCoord|,1.5))
* caltab_points (CaltabName, X, Y, Z)
* calibrate_cameras (CalibDataID, Error)
* 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.
set_origin_pose (PoseForCalibrationPlate, 0, 0, 0, PoseCalib)
* disp_continue_message (WindowHandle, 'black', 'true')
* stop ()
           
*像素距离
distance_pp(RCoord[0],CCoord[0],RCoord[48],CCoord[48], Dis_pix)
*像素直接转换mm然后计算
pix2mm(RCoord, CCoord,CamParam[2],CamParam[3],newCol,newRow)
distance_pp(newRow[0],newCol[0],newRow[48],newCol[48], Dis_m)
*用同一个世界坐标系来计算
image_points_to_world_plane(CamParam, PoseCalib,[RCoord[0],RCoord[48]], [CCoord[0],CCoord[48]], 'mm', X1, Y1)
distance_pp(Y1[0],X1[0],Y1[1],X1[1],Distance)
           
*输出计算结果比较
open_file('data.csv','append', FileHandle)
fwrite_string(FileHandle, Dis_pix*0.0299+','+Dis_m*1000+','+Distance+'\n')
close_file(FileHandle)    
           
Message:= '计算完毕'
disp_message (WindowHandle, Message, 'window', 12, 12, 'red', 'false')
stop()

你可能感兴趣的:(学习)