HALCON示例程序find_pads.hdev通過fit_rectangle2_contour_xld繪制精準輪廓
示例程序源碼(加注釋)
- 關于顯示類函數解釋
dev_update_pc (‘off’)
dev_update_window (‘off’)
dev_update_var (‘off’)
read_image (Image, ‘die_pads’)
dev_close_window ()
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width * 2, Height * 2, ‘black’, WindowHandle)
dev_set_part (0, 0, Height - 1, Width - 1) - fast_threshold - 使用全局閾值快速閾值處理圖像。
- fast_threshold(圖片:區域:最小灰度值,最大灰度值,最小閾值分割值:)
fast_threshold (Image, Region, 180, 255, 20) - 分割連通域
connection (Region, ConnectedRegions) - 使面積與長短軸比值篩選區域
select_shape (ConnectedRegions, SelectedRegions, [‘area’,‘anisometry’], ‘and’, [200,1], [1200,2])
fill_up (SelectedRegions, RegionFillUp) - 求取最大邊界
shape_trans (RegionFillUp, RegionTrans, ‘convex’) - boundary - 將區域縮小到其邊界。
- boundary(區域:區域邊界:邊界種類 ?
boundary (RegionTrans, RegionBorder, ‘inner’) - 使用圓形元素對區域進行膨脹
dilation_circle (RegionBorder, RegionDilation, 2.5)
union1 (RegionDilation, RegionUnion) - 減少圖片定義域
reduce_domain (Image, RegionUnion, ImageReduced) - 求取邊界
edges_sub_pix (ImageReduced, Edges, ‘sobel_fast’, 0.5, 20, 40) - 使用長度篩選邊界
select_shape_xld (Edges, SelectedContours, ‘contlength’, ‘and’, 10, 200) - union_adjacent_contours_xld - 計算端點靠近的輪廓的并集。
- union_adjacent_contours_xld(邊緣:合并邊緣:最大絕對距離,最大長度相對距離,模式:)
union_adjacent_contours_xld (SelectedContours, UnionContours, 2, 1, ‘attr_keep’) - fit_rectangle2_contour_xld - 擬合XLD輪廓為矩形。
fit_rectangle2_contour_xld (UnionContours, ‘tukey’, -1, 0, 0, 3, 2, Row, Column, Phi, Length1, Length2, PointOrder) - gen_rectangle2_contour_xld - 創建矩形形狀的XLD輪廓。
gen_rectangle2_contour_xld (Rectangle, Row, Column, Phi, Length1, Length2)
dev_display (Image)
dev_set_colored (12)
dev_display (Rectangle)
處理思路
這個例子是主要講解了如何利用邊緣提取與矩形擬合進行精確定位。使用了fast_threshold進行閾值分割,boundary并dilation_circle得到邊界圖像,使用edges_sub_pix進行邊緣提取,select_shape_xld篩選邊界,fit_rectangle2_contour_xld邊界矩形擬合,gen_rectangle2_contour_xld繪制邊緣輪廓。
后記
大家有什么問題可以向我提問哈,我看到了第一時間回復,希望在學習的路上多多結交良師益友。