halcon 相机标定

* This example shows how to perform the basic calibration of a single
* camera with multiple images of a calibration object.
dev_update_off ()
* 
* Initialize visualization
*第一个参数:在给定的文件夹'calib',halcon默认的目录,第二个参数:图像格式Default Value: 'default',第三个参数:Default Value: [],第四个参数:AllCalibImageFiles为所有图像的元组
list_image_files ('calib', 'default', [], AllCalibImageFiles)
*从AllCalibImageFiles挑选指定calib_single_camera名称的图像,并保存变量为ImageFiles
tuple_regexp_select (AllCalibImageFiles, 'calib_single_camera', ImageFiles) 
*从存储区读取指定的ImageFiles[0]图像并生成Image对象,如果读取多张生成对应的元组
read_image (Image, ImageFiles[0])
*获得图像的宽和高
get_image_size (Image, Width, Height)
* 
*关闭之前的图形窗口
dev_close_window ()
*打开一个适应图像Width和Height的图形窗口,左上角行列坐标,右下角行列坐标,宽度值限制Default Value: -1,Default Value: -1,新图形窗口的窗口句柄。
dev_open_window_fit_size (0, 0, Width, Height, 600, -1, WindowHandle)
*设置当前窗口的文本字体,带有指定的属性大小粗体和倾斜,句柄;字体大小;字体是否加粗;字体是否倾斜
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
*返回左上角的位置,并输出窗口的宽和高
get_window_extents (WindowHandle, Row, Column, WindowWidth, WindowHeight)
*打开了一个新的图形窗口,它可以用来显示像图像、区域和行这样的标志性对象,以及执行文本输出。此窗口将自动变为活动状态,这意味着所有输出(显示操作符和自动显示结果)将被重定向到此窗口。
dev_open_window (0, WindowWidth + 12, 1000 - WindowWidth, WindowHeight, 'white', WindowHandleConfig)
set_display_font (WindowHandleConfig, 14, 'mono', 'true', 'false')
* 
* Create the 3d object models that visualize the current configuration
* of camera and calibration plate. This may take a moment.
*字符串赋值
Message := 'Creating 3D object models for visualization...'
*在指定句柄窗口中显示文本,指定的位置,字体颜色,是否带框
disp_message (WindowHandle, Message, 'window', 12, 12, 'gray', 'false')
create_models (MarksModel, PlateModel, CamModel, BaseModel)
disp_message (WindowHandle, 'Ready.\nNow starting example...', 'window', 32, 12, 'gray', 'false')
wait_seconds (.5)
* -----------------------------------------------------------------------------------------------
* Part I  创建和设置标定模型,
* -------------------------------------------------------------------------------------
* Create and setup the calibration model
* 
* First, create the calibration data structure
*标定设置的类型(5种类型),标定设置中摄像机的数量Default value: 1;标定对象的数量Default value: 1,创建标定数据模型的句柄
create_calib_data ('calibration_object', 1, 1, CalibDataID)

* Specify the used calibration plane using the name of the
* description file   标定板的描述文件,之后可以直接利用生成标定板
CalPlateDescr := 'calplate_80mm.cpd'
*标定数据模型的句柄;标定对象的索引;标定板的描述文件
set_calib_data_calib_object (CalibDataID, 0, CalPlateDescr)
* Set the initial camera parameters--------------相机内参数的含义
* focus: 0.008 (use the nominal focal length of the used lens)
* kappa: 0 (parameter for the division model for the lens distortion)
* sx, sy: 3.7 μm (nominal pixel size of the used sensor)
* cx, cy: 646,482 (use center of the image as initial values
*                   for the principal point)
* ImageWidth, ImageHeight (width and height of the used images)
*相机的初始参数
StartParam := [0.008,0,3.7e-006,3.7e-006,646,482,1292,964]
*标定数据模型的句柄;相机索引,相机的类型'area_scan_division';初始相机内参;
set_calib_data_cam_param (CalibDataID, 0, 'area_scan_division', StartParam)
* -------------------------------------------------------------------------------------------
* Part II从多辐标定图像中提取标记点
* ---------------------------------------------------------------------------------------------
* Extract calibration marks from the calibration images
* 从ImageFiles中开始遍历
for Index := 0 to |ImageFiles| - 1 by 1
    read_image (Image, ImageFiles[Index])
    * Extract the calibration marks.
    *找出标定版,并提取其中心和轮廓,并估计改标定板相对于相机的姿态,对于每一个标定板的姿态,把所有提取带的信息存储在标定数据模型中。
    *从输入图像Image,CalibDataID标定数据模型的句柄,标定板的索引,标定板的姿态索引
    find_calib_object (Image, CalibDataID, 0, 0, Index, [], [])
    * 
    * Visualize the extracted marks
    * 从标定数据模型CalibDataID中读取相机坐标系下的标定板的位姿;相机索引;被观测到的标定板的索引;被观测到的标定板的姿态的索引;存储相机坐标系下标定板的姿态
    get_calib_data_observ_pose (CalibDataID, 0, 0, Index, StartPose)
    if (Index == 0)
        * Just for visualization:
        * Create a 3D model of the viewing volume based on the
        * extracted pose in the first image.
        * (Assume that in image 0 the calibration plate is placed on the base plate.)
        create_pose (0.0, 0.0, StartPose[2] + 0.01, 0, 0, 0, 'Rp+T', 'gba', 'point', BasePoseWorld)
        create_viewing_volume (StartParam, StartPose[2] + 0.001, VolumeModel)
        CamWidth := 1000 - WindowWidth
        CamHeight := WindowHeight
        CamPar := [0.016,0,2.2e-6,2.2e-6,CamHeight / 2,CamWidth / 2,CamHeight,CamWidth]
        * setup the visualization scene
        create_scene_3d (Scene)
        add_scene_3d_camera (Scene, CamPar, CameraIndex)
        create_pose (0.0, 0.0, 0.0, 0, 0, 0, 'Rp+T', 'gba', 'point', Origin)
        * 
        add_scene_3d_instance (Scene, CamModel, Origin, InstanceCamera)
        set_scene_3d_instance_param (Scene, InstanceCamera, 'color', 'blue')
        set_scene_3d_instance_param (Scene, InstanceCamera, 'disp_pose', 'true')
        * 
        add_scene_3d_instance (Scene, BaseModel, BasePoseWorld, InstanceBase)
        set_scene_3d_instance_param (Scene, InstanceBase, 'color', 'gray')
        * 
        add_scene_3d_instance (Scene, PlateModel, StartPose, InstancePlate)
        set_scene_3d_instance_param (Scene, InstancePlate, 'color', 'black')
        * 
        add_scene_3d_instance (Scene, MarksModel, StartPose, InstanceMarks)
        set_scene_3d_instance_param (Scene, InstanceMarks, 'color', 'white')
        * 
        add_scene_3d_instance (Scene, VolumeModel, Origin, InstanceVolume)
        set_scene_3d_instance_param (Scene, InstanceVolume, 'color', 'yellow')
        set_scene_3d_instance_param (Scene, InstanceVolume, 'alpha', 0.4)
        * 
        set_scene_3d_to_world_pose (Scene, [-0.01,-0.07,4.0,306,2.2,330,0])
    endif
    * Display the image and the centers of the calibration marks
    *输出的轮廓图像;输入的标定数据模型;Default value: 'marks';相机索引,标定对象索引,标定对象姿态索引
    get_calib_data_observ_contours (MarkContours, CalibDataID, 'marks', 0, 0, Index)
    dev_set_window (WindowHandle)
    dev_display (Image)
    dev_set_colored (12)
    dev_set_line_width (1)
    dev_display (MarkContours)
    * Visualize the configuration of camera and calibration target
    set_scene_3d_instance_pose (Scene, InstancePlate, StartPose)
    set_scene_3d_instance_pose (Scene, InstanceMarks, StartPose)
    display_scene_3d (WindowHandleConfig, Scene, CameraIndex)
    disp_message (WindowHandle, 'Extracted marks in image ' + Index, 'window', 12, 12, 'black', 'true')
    disp_message (WindowHandleConfig, 'Simulated view of pose ' + Index, 'window', 12, 12, 'black', 'true')
    disp_continue_message (WindowHandle, 'black', 'true')
    stop ()
endfor
* 
* Part III
* 
* Finally calibrate the camera
* 
calibrate_cameras (CalibDataID, Error)
get_calib_data (CalibDataID, 'camera', 0, 'params', CameraParametersCalibrated)
get_calib_data (CalibDataID, 'camera', 0, 'params_labels', ParLabels)
* 
* Display results
dev_set_window (WindowHandle)
dev_clear_window ()
if (|ParLabels| == 12)
    Units := ['m','','','','','','m','m','px','px','px','px']
else
    Units := ['m','','m','m','px','px','px','px']
endif
FormatStringLabels := max(strlen(ParLabels))$'0' + 's'
FormatStringParams := ''
CamParTable := ParLabels$FormatStringLabels + ' = ' + CameraParametersCalibrated + ' ' + Units
Text := 'The camera is now calibrated and ready for'
Text[1] := 'high-accuracy metric measurements.'
Text[2] := 'The final camera parameters are:'
Text[3] := ' '
Message := [Text,CamParTable,' ','Back projection error = ' + Error$'.3f' + ' px']
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
stop ()
* 
* Part IV
* 
* Cleanup
clear_calib_data (CalibDataID)
clear_object_model_3d ([MarksModel,PlateModel,CamModel,BaseModel,VolumeModel])
clear_scene_3d (Scene)

猜你喜欢

转载自blog.csdn.net/qq_22904277/article/details/54177653
今日推荐