HALCON示例程序measure_grid.hdev使用XLD分割鍵盤輪廓
示例程序源碼(加注釋)
- 關于顯示類函數解釋
dev_update_off ()
read_image (Image, ‘keypad’)
get_image_pointer1 (Image, Pointer, Type, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width, Height, ‘white’, WindowHandle)
set_display_font (WindowHandle, 16, ‘mono’, ‘true’, ‘false’)
dev_display (Image)
dev_set_colored (3)
dev_set_draw (‘fill’) - 動態閾值分割,提取連接區域
mean_image (Image, ImageMean, 7, 7)
dyn_threshold (Image, ImageMean, RegionDynThresh, 4, ‘dark’) - 分割連通域
connection (RegionDynThresh, ConnectedRegions) - 使用最大長度和最大直徑篩選區域
select_shape (ConnectedRegions, SelectedRegions, [‘max_diameter’,‘contlength’], ‘and’, [200,800], [99999,99999]) - 使用圓形元素進行閉運算
closing_circle (SelectedRegions, RegionClosing, 1.5)
dev_display (Image)
dev_display (RegionClosing)
disp_continue_message (WindowHandle, ‘black’, ‘true’)
stop () - 提取骨架
skeleton (RegionClosing, Skeleton) - 將骨架轉化為XLD輪廓
gen_contours_skeleton_xld (Skeleton, ContoursSkeleton, 1, ‘filter’) - 分割XLD輪廓
segment_contours_xld (ContoursSkeleton, ContoursSplitSkeleton, ‘lines’, 5, 2, 1) - 使用連續長度篩選XLD輪廓
select_contours_xld (ContoursSplitSkeleton, SelectedContours, ‘contour_length’, 30, 1000, -0.5, 0.5) - union_collinear_contours_xld - 聯合近似共線的輪廓。
union_collinear_contours_xld (SelectedContours, UnionCollinearContours, 100, 10, 20, rad(10), ‘attr_keep’)
dev_display (Image)
dev_display (UnionCollinearContours)
disp_continue_message (WindowHandle, ‘black’, ‘true’)
stop () - 計數
count_obj (UnionCollinearContours, NumberContours)
gen_empty_obj (LinesHorizontal)
gen_empty_obj (LinesVertical)
for i := 1 to NumberContours by 1select_obj (UnionCollinearContours, ObjectSelected, i)* 擬合直線XLD輪廓fit_line_contour_xld (ObjectSelected, 'tukey', -1, 0, 5, 2, RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist)* 由點生成多邊形XLD輪廓gen_contour_polygon_xld (Contour, [RowBegin,RowEnd], [ColBegin,ColEnd])Phi := atan2(-Nr,Nc)if (abs(Phi) < rad(5))concat_obj (LinesVertical, Contour, LinesVertical)endifif (rad(85) < abs(Phi) and abs(Phi) < rad(95))concat_obj (LinesHorizontal, Contour, LinesHorizontal)endif
endfor
dev_display (Image)
dev_set_color (‘red’)
dev_display (LinesVertical)
dev_set_color (‘yellow’)
dev_display (LinesHorizontal)
disp_continue_message (WindowHandle, ‘black’, ‘true’)
stop ()
RowJunction := []
ColJunction := []
RowRealJunction := []
ColRealJunction := []
count_obj (LinesHorizontal, NumberLH)
count_obj (LinesVertical, NumberLV)
for i := 1 to NumberLH by 1select_obj (LinesHorizontal, HorizontalLine, i)* get_contour_xld - 返回XLD輪廓的坐標。get_contour_xld (HorizontalLine, RowHorizontal, ColHorizontal)for j := 1 to NumberLV by 1select_obj (LinesVertical, VerticalLine, j)get_contour_xld (VerticalLine, RowVertical, ColVertical)* intersection_lines - 計算兩條線的交點intersection_lines (RowHorizontal[0], ColHorizontal[0], RowHorizontal[1], ColHorizontal[1], RowVertical[0], ColVertical[0], RowVertical[1], ColVertical[1], Row, Column, IsOverlapping)* distance_ps - 計算點和線段之間的距離distance_ps (Row, Column, RowHorizontal[0], ColHorizontal[0], RowHorizontal[1], ColHorizontal[1], DistanceH, DistanceHMax)distance_ps (Row, Column, RowVertical[0], ColVertical[0], RowVertical[1], ColVertical[1], DistanceV, DistanceVMax)RowJunction := [RowJunction,Row]ColJunction := [ColJunction,Column]if ((DistanceH <= 30) and (DistanceV <= 30))RowRealJunction := [RowRealJunction,Row]ColRealJunction := [ColRealJunction,Column]endifendfor
endfor
dev_set_color (‘white’)
gen_cross_contour_xld (Cross, RowJunction, ColJunction, 12, 0.785398)
dev_display (Cross)
disp_continue_message (WindowHandle, ‘black’, ‘true’)
stop ()
dev_display (Image)
dev_set_color (‘gray’)
dev_display (LinesHorizontal)
dev_display (LinesVertical)
dev_set_color (‘white’)
gen_cross_contour_xld (Cross, RowRealJunction, ColRealJunction, 12, 0.785398)
dev_display (Cross)
disp_continue_message (WindowHandle, ‘black’, ‘true’)
stop ()
set_display_font (WindowHandle, 14, ‘mono’, ‘true’, ‘false’)
dev_display (Image)
junctions_skeleton (Skeleton, EndPoints, JuncPoints)
get_region_points (JuncPoints, RowJunctionRegionProcessing, ColumnJunctionRegionProcessing)
gen_cross_contour_xld (CrossCenter, RowJunctionRegionProcessing, ColumnJunctionRegionProcessing, 12, 0.785398)
dev_set_color (‘gray’)
dev_display (Skeleton)
dev_set_color (‘white’)
dev_display (CrossCenter)
disp_message (WindowHandle, ‘Result of corresponding’, ‘window’, 40, 10, ‘white’, ‘false’)
disp_message (WindowHandle, ‘region processing’, ‘window’, 90, 10, ‘white’, ‘false’)
處理思路
這個例子是主要講解了XLD輪廓擬合直線,進而進行測量的例子。之前我們進行便于提取是提取最外邊的輪廓,之后進行sub_edge求取邊緣進行擬合,這次不同,這次使用的是求取區域,提取區域骨架,對骨架進行XLD擬合分析。
后記
大家有什么問題可以向我提問哈,我看到了第一時間回復,希望在學習的路上多多結交良師益友。