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例程代碼注釋,記錄以備查看。