HALCON示例程序measure_ball_bond.hdev電路板焊點位置測量
示例程序源碼(加注釋)
- 關于顯示類函數解釋
dev_update_off ()
dev_close_window ()
FileName := ‘bonds/ball_bond_ccd_’
read_image (Image, FileName + 1$‘02’)
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
dev_set_line_width (2)
set_display_font (WindowHandle, 16, ‘mono’, ‘true’, ‘false’)
dev_set_draw (‘margin’)
get_system (‘store_empty_region’, StoreEmptyRegion)
set_system (‘store_empty_region’, ‘false’) - 定義變量并初始化
NumImages := 6
for Index := 1 to NumImages by 1
read_image (Image, FileName + Index$‘02’)
dev_display (Image)- 獲得運行時間
count_seconds (S1) - 二值化
threshold (Image, Region, 135, 255) - 分割連通域
connection (Region, ConnectedRegions) - 使用寬度高度面積進行區域篩選
select_shape (ConnectedRegions, SelectedRegions, [‘width’,‘height’,‘area’], ‘and’, [115,115,5000], [130,130,15000])
fill_up (SelectedRegions, RegionFillUp)
shape_trans (RegionFillUp, Pads, ‘convex’) - 求取內孔區域
difference (Pads, RegionFillUp, RegionDifference) - 開運算
opening_rectangle1 (RegionDifference, BondWire, 10, 10) - 求取最大內切圓
shape_trans (BondWire, BallBonds, ‘inner_circle’) - select_shape_proto基于區域之間的某些關系選擇區域,這里使用重疊區域
- select_shape_proto(測量區域,對比區域:篩選出的區域:關系,最小值,最大值:)
select_shape_proto (Pads, BallBonds, MissingBonds, ‘overlaps_rel’, 0, 0)
count_seconds (S2)
Time := (S2 - S1) * 1000 - 膨脹
dilation_circle (BallBonds, RegionDilation2, 10) - 求交集
intersection (RegionDilation2, BondWire, RegionIntersection) - 求邊緣的老方法了,之前的貼子傳送門
boundary (RegionIntersection, RegionBorder, ‘inner’)
dilation_circle (RegionBorder, RegionDilation, 3.5)
union1 (RegionDilation, RegionUnion)
reduce_domain (Image, RegionUnion, ImageReduced)
edges_sub_pix (ImageReduced, Edges, ‘canny’, 1, 20, 70) - union_adjacent_contours_xld - 計算端點靠近的輪廓的并集。
- union_adjacent_contours_xld(邊緣:合并的邊緣:端點最大絕對閾值,端點最大相對閾值,模式:)
union_adjacent_contours_xld (Edges, UnionContours, 10, 1, ‘attr_keep’) - 篩選XLD輪廓
select_contours_xld (UnionContours, SelectedContours, ‘contour_length’, 20, 20000, -0.5, 0.5) - 通過XLD輪廓擬合圓形
fit_circle_contour_xld (SelectedContours, ‘atukey’, -1, 0, 0, 5, 7, CircleRow, CircleColumn, Radius, StartPhi, EndPhi, PointOrder) - 繪制XLD輪廓圓形
gen_circle_contour_xld (ContCircle, CircleRow, CircleColumn, Radius, 0, 6.28318, ‘positive’, 1)
count_obj (BondWire, NBonds)
gen_empty_obj (MeasureArrows) - 下邊這些就非常簡單的啦
dev_display (Image)
dev_set_color (‘cyan’)
dev_display (ContCircle)
dev_set_color (‘magenta’)
area_center (RegionDilation2, Area, Row, Column)
area_center (MissingBonds, AreaM, RowM, ColumnM)
select_shape_proto (Pads, BallBonds, BallTooClose, ‘distance_contour’, 0, 10)
area_center (BallTooClose, AreaB, RowB, ColumnB)
dev_set_color (‘yellow’)
dev_display (Pads)
if (|AreaM| > 0 or |AreaB| > 0)
dev_set_color (‘red’)
dev_display (MissingBonds)
dev_display (BallTooClose)
for IndexE := 0 to |AreaM| - 1 by 1
NRow := RowM[IndexE]
NCol := ColumnM[IndexE]
disp_message (WindowHandle, ‘Ball bond\nis missing’, ‘image’, NRow - 150, NCol - 110, ‘red’, ‘true’)
endfor
for IndexE := 0 to |AreaB| - 1 by 1
NRow := RowB[IndexE]
NCol := ColumnB[IndexE]
disp_message (WindowHandle, ‘Ball bond is too\nclose to border’, ‘image’, NRow - 150, NCol - 150, ‘red’, ‘true’)
endfor
endif
for IndexM := 1 to NBonds by 1
select_obj (ContCircle, ObjectSelected, IndexM)- 求取XLD輪廓的最小外接矩形
smallest_rectangle1_xld (ObjectSelected, minY, minX, maxY, maxX) - 求取XLD輪廓中心
area_center_xld (ObjectSelected, Area1, Ybb, Xbb, PointOrder1) - 繪制XLD箭頭
gen_arrow_contour_xld (Arrow, Ybb, Xbb, minY, Xbb, 12, 12)
concat_obj (MeasureArrows, Arrow, MeasureArrows)
gen_arrow_contour_xld (Arrow, Ybb, Xbb, maxY, Xbb, 12, 12)
concat_obj (MeasureArrows, Arrow, MeasureArrows)
dev_set_color (‘green’)
dev_display (MeasureArrows)
select_obj (ContCircle, ObjectSelected, IndexM)
area_center_xld (ObjectSelected, Area2, Ybb, Xbb, PointOrder2)
disp_message (WindowHandle, (2 * Radius[IndexM - 1])KaTeX parse error: Expected 'EOF', got '\nin' at position 129: …ond(s) detected\?n?i?n? ?' + Time’.2f’ + ’ ms’, ‘window’, 12, 12, ‘black’, ‘true’)
- 求取XLD輪廓的最小外接矩形
- Evaluate and display results
if (Index != NumImages)
disp_continue_message (WindowHandle, ‘black’, ‘true’)
stop ()
endif
endfor
set_system (‘store_empty_region’, StoreEmptyRegion)
- 獲得運行時間
處理思路
這個例子是主要講解了使用blob分析方法,結合XLD輪廓擬合,對電路焊點的檢測與測量。count_seconds計時算子select_shape_proto、union_adjacent_contours_xld、select_contours_xld、fit_circle_contour_xld、gen_circle_contour_xld、gen_arrow_contour_xld、smallest_rectangle1_xld、area_center_xld、gen_arrow_contour_xld
后記
大家有什么問題可以向我提問哈,我看到了第一時間回復,希望在學習的路上多多結交良師益友。