1 *关闭窗口 2 dev_close_window () 3 dev_close_window () 4 *打开指定大小、颜色背景的窗口 5 dev_open_window (0, 0, 768/2, 576/2, 'black', WindowHandle1) 6 dev_update_pc ('off') 7 dev_update_window ('off') 8 dev_update_var ('off') 9 dev_update_time ('off') 10 dev_set_draw ('margin') 11 dev_set_line_width (3) 12 * 13 * Calibrate the camera.(标定相机)步骤一 14 * 15 * 标定板描述文件 16 CaltabName := 'caltab_big.descr' 17 * make sure that the file 'CaltabDescrName' is in the current directory, 18 * the HALCONROOT/calib directory, or use an absolut path 19 *初始相机参数:焦距、畸变系数Kappa,Sx,Sy,Cx,Cy,ImageWidth,ImageHeight 20 StartCamPar := [0.008,0,0.0000086,0.0000086,384,288,768,576] 21 *物体在空间坐标系中的位姿数组 22 NStartPose := [] 23 *行角点数组 24 NRow := [] 25 *列角点数组 26 NCol := [] 27 *X、Y、Z从标定表文件获取计算值 28 caltab_points (CaltabName, X, Y, Z) 29 *创建空的图像元组 30 gen_empty_obj (Images) 31 *图像的数目 32 NumImages := 10 33 *接下来for循环,依次读取、处理NumImages张图像 34 for I := 1 to NumImages by 1 35 *读取图像:Image得到图像数据 36 read_image (Image, 'calib/calib-3d-coord-'+I$'02d') 37 *将单个Image加入元组Images中 38 concat_obj (Images, Image, Images) 39 *显示Image 40 dev_display (Image) 41 *在图像中找到标定板的区域:图像,标定板描述文件,滤波核大小,二值化值 42 find_caltab (Image, Caltab, CaltabName, 3, 112, 5) 43 *设置区域颜色:绿色 44 dev_set_color ('green') 45 *显示图像中标定板的区域 46 dev_display (Caltab) 47 *(核心函数:搜寻图像的原始点)在图像中找到标定板的各个角点坐标和初始位姿 48 find_marks_and_pose (Image, Caltab, CaltabName, StartCamPar, 128, 10, 18, 0.9, 15, 100, RCoord, CCoord, StartPose) 49 *设置颜色:红色 50 dev_set_color ('red') 51 *在窗口上圆形标记角点的位置 52 disp_circle (WindowHandle1, RCoord, CCoord, gen_tuple_const(|RCoord|,1.5)) 53 *一个图像位姿加入位姿元组 54 NStartPose := [NStartPose,StartPose] 55 *所有图像角点行坐标数组相连 56 NRow := [NRow,RCoord] 57 *所有图像角点列坐标数组相连 58 NCol := [NCol,CCoord] 59 endfor 60 dev_update_time ('on') 61 disp_continue_message (WindowHandle1, 'black', 'true') 62 stop () 63 * 64 * Calculate the mapping.(计算带矫正信息的图像)步骤二 65 * 66 *(核心函数)标定相机参数,对步骤一的数据进行计算处理 67 camera_calibration (X, Y, Z, NRow, NCol, StartCamPar, NStartPose, 'all', CamParam, NFinalPose, Errors) 68 *生成空的对象:包含矫正映射信息的图像 69 gen_empty_obj (Maps) 70 *for循环一次处理一组对应的图像 71 for NumImage := 1 to NumImages by 1 72 * Obtain the pose of the calibration table. 73 *获取标定出来的位姿3D 74 Pose := NFinalPose[(NumImage-1)*7:(NumImage-1)*7+6] 75 *设置新的原始3D位姿 76 set_origin_pose (Pose, -1.125, -1.0, 0, PoseNewOrigin) 77 *(核心函数)生成带矫正映射信息的图像MapSingle 78 gen_image_to_world_plane_map (MapSingle, CamParam, PoseNewOrigin, 768, 576, 900, 800, 0.0025, 'bilinear') 79 *将单张图像加入图像元组Maps 80 concat_obj (Maps, MapSingle, Maps) 81 endfor 82 disp_continue_message (WindowHandle1, 'black', 'true') 83 stop () 84 *代码到此处:有了NumImages张标定的原图 + NumImages张带矫正映射信息的图像, 85 *接下来While循环,一张一张的矫正图像 86 * 87 * Map the images.(矫正图像)步骤三 88 * 89 *窗口设置 90 dev_open_window (0, 391, 900/2, 800/2, 'black', WindowHandle2) 91 Button := 0 92 NumImage := 1 93 while (Button # 1) 94 dev_set_window (WindowHandle1) 95 dev_set_part (0, 0, 575, 767) 96 dev_clear_window () 97 *从图像元组中选择一张图像 98 select_obj (Images, Image, NumImage) 99 *显示选择的图像 100 dev_display (Image) 101 disp_message (WindowHandle1, 'Press any mouse button to stop', 'image', -1, -1, 'black', 'true') 102 *选择对应位置的带矫正信息的图像 103 select_obj (Maps, MapSingle, NumImage) 104 *(核心函数)矫正图像:原图,带矫正信息图,矫正结果图 105 map_image (Image, MapSingle, ImageMapped) 106 *以下为窗口、循环之类的处理,与图像处理没什么关系 107 dev_set_window (WindowHandle2) 108 dev_set_part (0, 0, 799, 899) 109 dev_clear_window () 110 dev_display (ImageMapped) 111 NumImage := NumImage + 1 112 if (NumImage > NumImages) 113 NumImage := 1 114 endif 115 dev_error_var (Error, 1) 116 dev_set_check ('~give_error') 117 get_mposition (WindowHandle1, R, C, Button) 118 dev_error_var (Error, 0) 119 dev_set_check ('give_error') 120 if (Error#H_MSG_TRUE) 121 Button := 0 122 endif 123 if (Button) 124 break 125 endif 126 endwhile 127 dev_set_window (WindowHandle2) 128 dev_close_window ()
Halcon例程代码注释,记录以备查看。