HALCON示例程序measure_metal_part_first_example.hdev通过拟合边缘进行尺寸测量
示例程序源码(加注释)
- 关于显示类函数解释
dev_update_off ()
read_image (Image, ‘metal-parts/metal-parts-01’)
get_image_size (Image, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width, Height, ‘light gray’, WindowID)
dev_set_part (0, 0, Height - 1, Width - 1)
dev_set_line_width (3)
dev_set_color (‘white’)
dev_set_draw (‘margin’)
dev_display (Image)
set_display_font (WindowID, 16, ‘mono’, ‘true’, ‘false’)
stop ()
dev_set_draw (‘fill’) - 二值化
threshold (Image, Region, 100, 255) - 求取区域中心
area_center (Region, AreaRegion, RowCenterRegion, ColumnCenterRegion) - 使用椭圆拟合求取区域角度
orientation_region (Region, OrientationRegion)
dev_display (Region)
disp_message (WindowID, ‘Center Row: ’ + RowCenterRegion′.5′,′window′,20,10,′white′,′false′)dispmessage(WindowID,′Area:′+AreaRegion+′pixel′,′window′,20,300,′white′,′false′)dispmessage(WindowID,′CenterColumn:′+ColumnCenterRegion'.5', 'window', 20, 10, 'white', 'false') disp_message (WindowID, 'Area: ' + AreaRegion + ' pixel', 'window', 20, 300, 'white', 'false') disp_message (WindowID, 'Center Column: ' + ColumnCenterRegion′.5′,′window′,20,10,′white′,′false′)dispmessage(WindowID,′Area:′+AreaRegion+′pixel′,′window′,20,300,′white′,′false′)dispmessage(WindowID,′CenterColumn:′+ColumnCenterRegion’.5’, ‘window’, 60, 10, ‘white’, ‘false’)
disp_message (WindowID, ‘Orientation: ’ + OrientationRegion$’.3’ + ’ rad’, ‘window’, 60, 300, ‘white’, ‘false’)
dev_set_color (‘gray’)
disp_cross (WindowID, RowCenterRegion, ColumnCenterRegion, 15, 0)
disp_arrow (WindowID, RowCenterRegion, ColumnCenterRegion, RowCenterRegion - 60 * sin(OrientationRegion), ColumnCenterRegion + 60 * cos(OrientationRegion), 2)
stop () - 提取边缘
edges_sub_pix (Image, Edges, ‘canny’, 0.6, 30, 70) - 分割边缘
segment_contours_xld (Edges, ContoursSplit, ‘lines_circles’, 6, 4, 4)
dev_clear_window ()
dev_set_colored (12)
dev_display (ContoursSplit)
stop ()
dev_open_window (0, round(Width / 2), (535 - 225) * 2, (395 - 115) * 2, ‘black’, WindowHandleZoom)
dev_set_part (round(115), round(225), round(395), round(535))
set_display_font (WindowHandleZoom, 18, ‘mono’, ‘true’, ‘false’)
count_obj (ContoursSplit, NumSegments)
dev_display (Image)
NumCircles := 0
RowsCenterCircle := []
ColumnsCenterCircle := []
disp_message (WindowHandleZoom, 'Circle radii: ', ‘window’, 120, 230, ‘white’, ‘false’)
for i := 1 to NumSegments by 1select_obj (ContoursSplit, SingleSegment, i)* 获取xld轮廓属性,是圆形椭圆还是直线get_contour_global_attrib_xld (SingleSegment, 'cont_approx', Attrib)if (Attrib == 1)NumCircles := NumCircles + 1* 拟合圆形fit_circle_contour_xld (SingleSegment, 'atukey', -1, 2, 0, 5, 2, Row, Column, Radius, StartPhi, EndPhi, PointOrder)* 绘制圆形gen_circle_contour_xld (ContCircle, Row, Column, Radius, 0, rad(360), 'positive', 1)RowsCenterCircle := [RowsCenterCircle,Row]ColumnsCenterCircle := [ColumnsCenterCircle,Column]dev_display (ContCircle)disp_message (WindowHandleZoom, 'C' + NumCircles, 'window', Row - Radius - 10, Column, 'white', 'false')disp_message (WindowHandleZoom, 'C' + NumCircles + ': Radius = ' + Radius$'.4', 'window', 275 + NumCircles * 15, 230, 'white', 'false')endif
endfor
distance_pp (RowsCenterCircle[1], ColumnsCenterCircle[1], RowsCenterCircle[2], ColumnsCenterCircle[2], Distance_2_3)
disp_line (WindowHandleZoom, RowsCenterCircle[1], ColumnsCenterCircle[1], RowsCenterCircle[2], ColumnsCenterCircle[2])
disp_message (WindowHandleZoom, 'Distance C2-C3 = ' + Distance_2_3$'.4', 'window', 275 + (NumCircles + 3) * 15, 230, 'magenta', 'false')
distance_pp (RowsCenterCircle[0], ColumnsCenterCircle[0], RowsCenterCircle[2], ColumnsCenterCircle[2], Distance_1_3)
disp_line (WindowHandleZoom, RowsCenterCircle[0], ColumnsCenterCircle[0], RowsCenterCircle[2], ColumnsCenterCircle[2])
disp_message (WindowHandleZoom, 'Distance C1-C3 = ' + Distance_1_3$'.4', 'window', 275 + (NumCircles + 2) * 15, 230, 'yellow', 'false')
distance_pp (RowsCenterCircle[3], ColumnsCenterCircle[3], RowsCenterCircle[4], ColumnsCenterCircle[4], Distance_4_5)
disp_line (WindowHandleZoom, RowsCenterCircle[3], ColumnsCenterCircle[3], RowsCenterCircle[4], ColumnsCenterCircle[4])
disp_message (WindowHandleZoom, 'Distance C4-C5 = ' + Distance_4_5$'.4', 'window', 275 + (NumCircles + 4) * 15, 230, 'cyan', 'false')
stop ()
dev_set_window (WindowHandleZoom)
dev_close_window ()
dev_set_part (0, 0, Height - 1, Width - 1)
dev_update_window (‘on’)
处理思路
这个例子是主要讲解了使用xld边缘拟合测量圆的大小与相对位置的例子。
后记
大家有什么问题可以向我提问哈,我看到了第一时间回复,希望在学习的路上多多结交良师益友。