1.参考这边文章https://blog.csdn.net/weixin_60275604/article/details/139068423
2.代码
dev_close_window()
dev_open_window(0, 0, 512, 512, 'black', WindowHandle)
dev_set_draw('margin')
***创建一个标定板参数 xNum,yNum标定板中行列标定点个数 MarkDist标定点中心距离 diameterratio标定点直径跟markdist的比值 calplatedescr,calplatepsfile生成标定文件的路径
gen_caltab(7, 7,0.004, 0.5, 'G:/CompanyProject/GearMeasure/caltab.descr', 'G:/CompanyProject/GearMeasure/caltab.ps')
TmpCtrl_ReferenceIndex := 0
***标定板参数路径
TmpCtrl_PlateDescription := 'G:/CompanyProject/GearMeasure/caltab.descr'
***相机参数 放大倍数 像元大小 图片中心坐标 图片像素大小
StartParameters := ['area_scan_telecentric_division',0.12,0,2.4e-06,2.4e-06,2736,1824,5472,3648]
***查找标定板的参数
TmpCtrl_FindCalObjParNames := ['gap_tolerance','alpha','skip_find_caltab']
TmpCtrl_FindCalObjParValues := [1,1,'false']
***创建标定句柄
create_calib_data ('calibration_object', 1, 1, CalibHandle)
***设置标定相机参数
set_calib_data_cam_param (CalibHandle, 0, [], StartParameters)
***设置标定板参数
set_calib_data_calib_object (CalibHandle, 0, TmpCtrl_PlateDescription)
***读取文件
list_files ('G:/CompanyProject/GearMeasure/CalImage', ['files','follow_links'], ImageFiles)
tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
for Index := 0 to |ImageFiles| - 1 by 1
read_image (Image, ImageFiles[Index])
find_calib_object (Image, CalibHandle, 0, 0, Index, TmpCtrl_FindCalObjParNames, TmpCtrl_FindCalObjParValues)
*获取所有圆点坐标
get_calib_data_observ_points (CalibHandle, 0, 0, Index, Row, Column, IndexPoint, StartPose)
gen_cross_contour_xld (Cross, Row, Column, 36, 0.785398)
get_calib_data_observ_contours (Contours, CalibHandle, 'caltab', 0, 0, Index)
dev_display(Image)
dev_display(Cross)
dev_display(Contours)
endfor
stop()
***执行标定
calibrate_cameras (CalibHandle, TmpCtrl_Errors)
***获取相机内参
get_calib_data (CalibHandle, 'camera', 0, 'params', CameraParameters)
***获取外参
get_calib_data (CalibHandle, 'calib_obj_pose', [0, TmpCtrl_ReferenceIndex], 'pose', CameraPose)
***设置坐标在原点
set_origin_pose (CameraPose, 0.0, 0.0, 0.003, CameraPose)
stop ()
***获取点
read_image (Image, 'G:/CompanyProject/GearMeasure/CalImage/1.jpg')
TmpCtrl_PlateDescription := 'G:/CompanyProject/GearMeasure/caltab.descr'
TmpCtrl_FindCalObjParNames := ['gap_tolerance','alpha','skip_find_caltab']
TmpCtrl_FindCalObjParValues := [1,1,'false']
create_calib_data ('calibration_object', 1, 1, CalibHandle)
set_calib_data_cam_param (CalibHandle, 0, [], CameraParameters)
set_calib_data_calib_object (CalibHandle, 0, TmpCtrl_PlateDescription)
find_calib_object (Image, CalibHandle, 0, 0, 0, TmpCtrl_FindCalObjParNames, TmpCtrl_FindCalObjParValues)
get_calib_data_observ_points (CalibHandle, 0, 0, 0, TmpCtrl_MarkRows, TmpCtrl_MarkColumns, TmpCtrl_Ind, CameraPose)
set_origin_pose (CameraPose, 0.0, 0.0, 0.003, CameraPose)
***点距离换算成世界距离
TmpCtrl_ImageRows := [TmpCtrl_MarkRows[0], TmpCtrl_MarkRows[1]]
TmpCtrl_ImageColumns := [TmpCtrl_MarkColumns[0], TmpCtrl_MarkColumns[1]]
gen_contour_polygon_xld (TmpObj_ImageContour, TmpCtrl_ImageRows, TmpCtrl_ImageColumns)
image_points_to_world_plane (CameraParameters, CameraPose, TmpCtrl_ImageRows, TmpCtrl_ImageColumns, 'mm', TmpCtrl_WorldX, TmpCtrl_WorldY)
distance_pp (TmpCtrl_WorldY[0], TmpCtrl_WorldX[0], TmpCtrl_WorldY[1], TmpCtrl_WorldX[1], TmpCtrl_Distance)
***变换轮廓到世界坐标
gen_contour_polygon_xld (TmpObj_ImageContour, TmpCtrl_MarkRows, TmpCtrl_MarkColumns)
contour_to_world_plane_xld (TmpObj_ImageContour, TmpObj_WorldContour, CameraParameters, CameraPose, 'mm')
get_contour_xld (TmpObj_WorldContour, TmpCtrl_WorldX, TmpCtrl_WorldY)
***放射变换图片
TmpCtrl_RectificationWidth := 80
TmpCtrl_RectificationWidth := TmpCtrl_RectificationWidth / 1000.0
set_origin_pose (CameraPose, -0.5*TmpCtrl_RectificationWidth, -0.4*TmpCtrl_RectificationWidth, 0, TmpCtrl_RectificationPose)
gen_image_to_world_plane_map (TmpObj_RectificationMap, CameraParameters, TmpCtrl_RectificationPose, 5472, 3648, 5472, 3648, TmpCtrl_RectificationWidth / 5472, 'bilinear')
map_image (Image, TmpObj_RectificationMap, TmpObj_RectifiedImage)
stop ()
3.注意点 标定板规格要对
使用的标定板规格对上gen_caltab(7, 7,0.004, 0.5, ‘G:/CompanyProject/GearMeasure/caltab.descr’, ‘G:/CompanyProject/GearMeasure/caltab.ps’)这个函数生成出来的标定板数据才能对上,find_calib_object不容易出错。
最终要的结果就是相机内参和外参数。