HALCON示例程序measure_metal_part_extended.hdev金属零件尺寸测量
示例程序源码(加注释)
-
关于显示类函数解释
dev_update_off ()
read_image (Image, ‘metal-parts/metal-parts-01’)
init_visualization (Image, 3, ‘white’, ‘margin’, Width, Height, WindowID)
init_visualization函数内部代码:
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 (LineWidth)
dev_set_color (Color)
dev_set_draw (DrawMode)
dev_display (Image)
return ()
set_display_font (WindowID, 16, ‘mono’, ‘true’, ‘false’)
disp_continue_message (WindowID, ‘black’, ‘true’)
stop ()
dev_set_draw (‘fill’)
count_seconds (T1)
-
二值化
threshold (Image, Region, 100, 255)
-
求中心与面积
area_center (Region, AreaRegion, RowCenterRegion, ColumnCenterRegion)
-
通过拟合椭圆求解区域角度
orientation_region (Region, OrientationRegion)
count_seconds (T2)
dev_display (Region)
set_tposition (WindowID, 10, 10)
write_string (WindowID, 'Region Processing')
set_tposition (WindowID, 10, 300)
write_string (WindowID, 'Time: ' + ((T2 - T1) * 1000)$'.2' + ' ms')
set_tposition (WindowID, 50, 10)
write_string (WindowID, 'Center Row: ' + RowCenterRegion$'.5')
set_tposition (WindowID, 50, 300)
write_string (WindowID, 'Center Column: ' + ColumnCenterRegion$'.5')
set_tposition (WindowID, 80, 10)
write_string (WindowID, 'Area: ' + AreaRegion + ' pixels')
set_tposition (WindowID, 80, 300)
write_string (WindowID, 'Orientation: ' + OrientationRegion$'.3' + ' rad')
dev_set_color ('gray')
disp_cross (WindowID, RowCenterRegion, ColumnCenterRegion, 10, 0)
* 求最小外接矩形
smallest_rectangle2 (Region, Row_Region, Column_Region, Phi_Region, Length1_Region, Length2_Region)
* 绘制最小外接矩形
gen_rectangle2 (Rectangle, Row_Region, Column_Region, Phi_Region, Length1_Region, Length2_Region)
disp_continue_message (WindowID, 'black', 'true')
stop ()
count_seconds (T3)
-
二值化、求边界、膨胀、减少定义域、求边界
threshold (Image, Region, 100, 255)
boundary (Region, RegionBorder, ‘inner’)
dilation_circle (RegionBorder, RegionDilation, 1.5)
reduce_domain (Image, RegionDilation, ImageReduced)
edges_sub_pix (ImageReduced, Edges, ‘lanser2’, 0.5, 40, 90)
-
求取xld轮廓面积与位置
area_center_xld (Edges, AreaContours, RowCenterContours, ColumnCenterContours, PointOrderContours)
AreaOfRegionByContours := AreaContours[0] - AreaContours[1] - AreaContours[2] - AreaContours[3]
-
求取轮廓方向
orientation_xld (Edges, OrientationContours)
count_seconds (T4)
dev_set_color (‘light gray’)
dev_display (Edges)
set_tposition (WindowID, 330, 10)
write_string (WindowID, 'Contour Processing')
set_tposition (WindowID, 330, 300)
write_string (WindowID, 'Time: ' + ((T4 - T3) * 1000)$'.3' + ' ms')
set_tposition (WindowID, 370, 10)
write_string (WindowID, 'Center Row: ' + RowCenterContours[0]$'.4')
set_tposition (WindowID, 370, 300)
write_string (WindowID, 'Center Column: ' + ColumnCenterContours[0]$'.4')
set_tposition (WindowID, 400, 10)
write_string (WindowID, 'Area: ' + AreaOfRegionByContours[0] + ' pixels')
set_tposition (WindowID, 400, 300)
write_string (WindowID, 'Orientation: ' + OrientationContours[0]$'.3' + ' rad')
disp_cross (WindowID, RowCenterContours, ColumnCenterContours, 6, 0)
disp_cross (WindowID, RowCenterContours[0], ColumnCenterContours[0], 10, 0)
smallest_rectangle2_xld (Edges, Row_Contours, Column_Contours, Phi_Contours, Length1_Contours, Length2_Contours)
disp_continue_message (WindowID, 'black', 'true')
stop ()
-
使用圆形与直线分割XLD轮廓
segment_contours_xld (Edges, ContoursSplit, ‘lines_circles’, 6, 4, 4)
-
对XLD轮廓进行排序
sort_contours_xld (ContoursSplit, SortedContours, ‘upper_left’, ‘true’, ‘column’)
dev_clear_window ()
dev_set_colored (12)
dev_display (SortedContours)
stop ()
open_zoom_window (0, round(Width / 2), 115, 225, 395, 535, 2, WindowHandleZoom)
set_display_font (WindowHandleZoom, 14, ‘mono’, ‘true’, ‘false’)
count_obj (SortedContours, NumSegments)
dev_display (Image)
NumCircles := 0
NumLines := 0
gen_empty_obj (Lines)
RowsBegin := []
ColsBegin := []
RowsEnd := []
ColsEnd := []
dev_set_color (‘white’)
set_tposition (WindowHandleZoom, 120, 230)
write_string (WindowHandleZoom, 'Line lengths and circle radii: ')
for i := 1 to NumSegments by 1
select_obj (SortedContours, 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)
* 创建椭圆XLD轮廓
gen_ellipse_contour_xld (ContEllipse, Row, Column, 0, Radius, Radius, 0, rad(360), 'positive', 1.0)
dev_set_color ('white')
dev_display (ContEllipse)
* 计算轮廓到椭圆的距离。
dist_ellipse_contour_xld (SingleSegment, 'algebraic', -1, 0, Row, Column, 0, Radius, Radius, MinDist, MaxDist, AvgDist, SigmaDist)
set_tposition (WindowHandleZoom, Row - Radius - 10, Column)
write_string (WindowHandleZoom, 'C' + NumCircles)
ResultText := 'C' + NumCircles + ': Radius = ' + Radius$'.3' + ' / MaxDeviation: ' + MaxDist$'.3'
else
NumLines := NumLines + 1
* 拟合直线
fit_line_contour_xld (SingleSegment, 'tukey', -1, 0, 5, 2, RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist)
* 由点生成多边形XLD轮廓
gen_contour_polygon_xld (Line, [RowBegin,RowEnd], [ColBegin,ColEnd])
dev_set_color ('yellow')
dev_display (Line)
* 计算点到点的距离
distance_pp (RowBegin, ColBegin, RowEnd, ColEnd, Length)
set_tposition (WindowHandleZoom, (RowBegin + RowEnd) / 2 - Nr * 10, (ColBegin + ColEnd) / 2)
write_string (WindowHandleZoom, 'L' + NumLines)
ResultText := 'L' + NumLines + ': Length = ' + Length$'.4'
concat_obj (Lines, Line, Lines)
RowsBegin := [RowsBegin,RowBegin]
ColsBegin := [ColsBegin,ColBegin]
RowsEnd := [RowsEnd,RowEnd]
ColsEnd := [ColsEnd,ColEnd]
endif
set_tposition (WindowHandleZoom, 275 + i * 10, 230)
write_string (WindowHandleZoom, ResultText)
endfor
disp_continue_message (WindowHandleZoom, 'black', 'true')
stop ()
dev_display (Image)
dev_set_color (‘white’)
-
使用foerstner检测特征点
points_foerstner (Image, 1, 2, 3, 200, 0.3, ‘gauss’, ‘false’, RowJunctions, ColJunctions, CoRRJunctions, CoRCJunctions, CoCCJunctions, RowArea, ColArea, CoRRArea, CoRCArea, CoCCArea)
-
绘制十字线
gen_cross_contour_xld (CrossFoerstner, RowJunctions, ColJunctions, 10, 0)
dev_display (CrossFoerstner)
set_tposition (WindowHandleZoom, RowJunctions[0] - 10, ColJunctions[0] + 10)
write_string (WindowHandleZoom, ‘P2’)
set_tposition (WindowHandleZoom, RowJunctions[1] - 10, ColJunctions[1] + 10)
write_string (WindowHandleZoom, ‘P1’)
set_tposition (WindowHandleZoom, 120, 230)
write_string (WindowHandleZoom, ‘Foerstner Points’)
disp_continue_message (WindowHandleZoom, ‘black’, ‘true’)
stop ()
dev_set_color (‘yellow’)
write_string (WindowHandleZoom, ’ vs. intersection_l_l: ')
-
求线段交点
intersection_lines (RowsBegin[0], ColsBegin[0], RowsEnd[0], ColsEnd[0], RowsBegin[1], ColsBegin[1], RowsEnd[1], ColsEnd[1], RowIntersect1, ColumnIntersect1, IsOverlapping1)
-
绘制十字线
gen_cross_contour_xld (CrossIntersection1, RowIntersect1, ColumnIntersect1, 10, 0.785398)
dev_display (CrossIntersection1)
dev_set_color (‘white’)
distance_pp (RowJunctions[1], ColJunctions[1], RowIntersect1, ColumnIntersect1, Distance1)
set_tposition (WindowHandleZoom, 325, 230)
write_string (WindowHandleZoom, ‘Distance between Foerstner points and’)
set_tposition (WindowHandleZoom, 335, 230)
write_string (WindowHandleZoom, ‘points obtained by intersection_ll:’)
set_tposition (WindowHandleZoom, 355, 230)
write_string (WindowHandleZoom, ‘in P1: ’ + Distance1 ′ . 3 ′ + ′ p i x e l s ′ ) d e v s e t c o l o r ( ′ y e l l o w ′ ) i n t e r s e c t i o n l i n e s ( R o w s B e g i n [ 0 ] , C o l s B e g i n [ 0 ] , R o w s E n d [ 0 ] , C o l s E n d [ 0 ] , R o w s B e g i n [ 2 ] , C o l s B e g i n [ 2 ] , R o w s E n d [ 2 ] , C o l s E n d [ 2 ] , R o w I n t e r s e c t 2 , C o l u m n I n t e r s e c t 2 , I s O v e r l a p p i n g 2 ) d i s t a n c e p p ( R o w J u n c t i o n s [ 0 ] , C o l J u n c t i o n s [ 0 ] , R o w I n t e r s e c t 2 , C o l u m n I n t e r s e c t 2 , D i s t a n c e 2 ) g e n c r o s s c o n t o u r x l d ( C r o s s I n t e r s e c t i o n 2 , R o w I n t e r s e c t 2 , C o l u m n I n t e r s e c t 2 , 10 , 0.785398 ) d e v d i s p l a y ( C r o s s I n t e r s e c t i o n 2 ) d e v s e t c o l o r ( ′ w h i t e ′ ) s e t t p o s i t i o n ( W i n d o w H a n d l e Z o o m , 370 , 230 ) w r i t e s t r i n g ( W i n d o w H a n d l e Z o o m , ′ i n P 2 : ′ + D i s t a n c e 2 '.3' + ' pixels') dev_set_color ('yellow') intersection_lines (RowsBegin[0], ColsBegin[0], RowsEnd[0], ColsEnd[0], RowsBegin[2], ColsBegin[2], RowsEnd[2], ColsEnd[2], RowIntersect2, ColumnIntersect2, IsOverlapping2) distance_pp (RowJunctions[0], ColJunctions[0], RowIntersect2, ColumnIntersect2, Distance2) gen_cross_contour_xld (CrossIntersection2, RowIntersect2, ColumnIntersect2, 10, 0.785398) dev_display (CrossIntersection2) dev_set_color ('white') set_tposition (WindowHandleZoom, 370, 230) write_string (WindowHandleZoom, 'in P2: ' + Distance2 ′.3′+′pixels′)devsetcolor(′yellow′)intersectionlines(RowsBegin[0],ColsBegin[0],RowsEnd[0],ColsEnd[0],RowsBegin[2],ColsBegin[2],RowsEnd[2],ColsEnd[2],RowIntersect2,ColumnIntersect2,IsOverlapping2)distancepp(RowJunctions[0],ColJunctions[0],RowIntersect2,ColumnIntersect2,Distance2)gencrosscontourxld(CrossIntersection2,RowIntersect2,ColumnIntersect2,10,0.785398)devdisplay(CrossIntersection2)devsetcolor(′white′)settposition(WindowHandleZoom,370,230)writestring(WindowHandleZoom,′inP2:′+Distance2’.3’ + ’ pixels’)
disp_continue_message (WindowHandleZoom, ‘black’, ‘true’)
stop ()
dev_display (Image)
-
计算两条线之间的角度。
angle_ll (RowsBegin[0], ColsBegin[0], RowsEnd[0], ColsEnd[0], RowsBegin[1], ColsBegin[1], RowsEnd[1], ColsEnd[1], Angle1)
set_tposition (WindowHandleZoom, RowsBegin[0] - 10, ColsBegin[0] + 10)
write_string (WindowHandleZoom, ‘P2’)
set_tposition (WindowHandleZoom, RowsEnd[0] - 10, ColsEnd[0] + 10)
write_string (WindowHandleZoom, ‘P1’)
set_tposition (WindowHandleZoom, 120, 230)
write_string (WindowHandleZoom, ‘Angle in P1: ’ + deg(Angle1) ′ . 3 ′ + ′ d e g r e e s ′ ) d i s p a r r o w ( W i n d o w H a n d l e Z o o m , R o w s E n d [ 0 ] , C o l s E n d [ 0 ] , R o w s B e g i n [ 0 ] , C o l s B e g i n [ 0 ] , 1 ) d i s p a r r o w ( W i n d o w H a n d l e Z o o m , R o w s B e g i n [ 1 ] , C o l s B e g i n [ 1 ] , R o w s E n d [ 1 ] , C o l s E n d [ 1 ] , 1 ) d i s p c o n t i n u e m e s s a g e ( W i n d o w H a n d l e Z o o m , ′ b l a c k ′ , ′ t r u e ′ ) s t o p ( ) d e v d i s p l a y ( I m a g e ) a n g l e l l ( R o w s B e g i n [ 0 ] , C o l s B e g i n [ 0 ] , R o w s E n d [ 0 ] , C o l s E n d [ 0 ] , R o w s B e g i n [ 2 ] , C o l s B e g i n [ 2 ] , R o w s E n d [ 2 ] , C o l s E n d [ 2 ] , A n g l e 2 ) s e t t p o s i t i o n ( W i n d o w H a n d l e Z o o m , R o w s B e g i n [ 0 ] − 10 , C o l s B e g i n [ 0 ] + 10 ) w r i t e s t r i n g ( W i n d o w H a n d l e Z o o m , ′ P 2 ′ ) s e t t p o s i t i o n ( W i n d o w H a n d l e Z o o m , R o w s E n d [ 0 ] − 10 , C o l s E n d [ 0 ] + 10 ) w r i t e s t r i n g ( W i n d o w H a n d l e Z o o m , ′ P 1 ′ ) s e t t p o s i t i o n ( W i n d o w H a n d l e Z o o m , 120 , 230 ) w r i t e s t r i n g ( W i n d o w H a n d l e Z o o m , ′ A n g l e i n P 2 : ′ + d e g ( A n g l e 2 ) '.3' + ' degrees') disp_arrow (WindowHandleZoom, RowsEnd[0], ColsEnd[0], RowsBegin[0], ColsBegin[0], 1) disp_arrow (WindowHandleZoom, RowsBegin[1], ColsBegin[1], RowsEnd[1], ColsEnd[1], 1) disp_continue_message (WindowHandleZoom, 'black', 'true') stop () dev_display (Image) angle_ll (RowsBegin[0], ColsBegin[0], RowsEnd[0], ColsEnd[0], RowsBegin[2], ColsBegin[2], RowsEnd[2], ColsEnd[2], Angle2) set_tposition (WindowHandleZoom, RowsBegin[0] - 10, ColsBegin[0] + 10) write_string (WindowHandleZoom, 'P2') set_tposition (WindowHandleZoom, RowsEnd[0] - 10, ColsEnd[0] + 10) write_string (WindowHandleZoom, 'P1') set_tposition (WindowHandleZoom, 120, 230) write_string (WindowHandleZoom, 'Angle in P2: ' + deg(Angle2) ′.3′+′degrees′)disparrow(WindowHandleZoom,RowsEnd[0],ColsEnd[0],RowsBegin[0],ColsBegin[0],1)disparrow(WindowHandleZoom,RowsBegin[1],ColsBegin[1],RowsEnd[1],ColsEnd[1],1)dispcontinuemessage(WindowHandleZoom,′black′,′true′)stop()devdisplay(Image)anglell(RowsBegin[0],ColsBegin[0],RowsEnd[0],ColsEnd[0],RowsBegin[2],ColsBegin[2],RowsEnd[2],ColsEnd[2],Angle2)settposition(WindowHandleZoom,RowsBegin[0]−10,ColsBegin[0]+10)writestring(WindowHandleZoom,′P2′)settposition(WindowHandleZoom,RowsEnd[0]−10,ColsEnd[0]+10)writestring(WindowHandleZoom,′P1′)settposition(WindowHandleZoom,120,230)writestring(WindowHandleZoom,′AngleinP2:′+deg(Angle2)’.3’ + ’ degrees’)
disp_arrow (WindowHandleZoom, RowsBegin[0], ColsBegin[0], RowsEnd[0], ColsEnd[0], 1)
disp_arrow (WindowHandleZoom, RowsEnd[2], ColsEnd[2], RowsBegin[2], ColsBegin[2], 1)
disp_continue_message (WindowHandleZoom, ‘black’, ‘true’)
stop ()
-
生成多边形的XLD轮廓。
gen_polygons_xld (ContoursSplit, Polygons, ‘ramer’, 2)
-
提取平行多边形XLD轮廓
gen_parallels_xld (Polygons, Parallels, 80, 75, 0.15, ‘true’)
count_obj (Parallels, NumberParallels)
dev_display (Image)
for i := 1 to NumberParallels by 1
dev_set_color ('white')
select_obj (Parallels, SelectedParallel, i)
dev_display (SelectedParallel)
* 得到XLD边缘平行数据
get_parallels_xld (SelectedParallel, Row1Parallels, Col1Parallels, Length1Parallels, Phi1Parallels, Row2Parallels, Col2Parallels, Length2Parallels, Phi2Parallels)
distance_pp (Row1Parallels[0], Col1Parallels[0], Row2Parallels[1], Col2Parallels[1], Distance2)
distance_pp (Row1Parallels[1], Col1Parallels[1], Row2Parallels[0], Col2Parallels[0], Distance1)
set_tposition (WindowHandleZoom, 120, 230)
write_string (WindowHandleZoom, 'Distance BC: ' + Distance1$'.4' + ' pixels')
set_tposition (WindowHandleZoom, 140, 230)
write_string (WindowHandleZoom, 'Distance AD: ' + Distance2$'.4' + ' pixels')
set_tposition (WindowHandleZoom, 340, 230)
write_string (WindowHandleZoom, 'Orientation AB: ' + deg(Phi1Parallels)$'.3' + ' degrees')
set_tposition (WindowHandleZoom, 360, 230)
write_string (WindowHandleZoom, 'Orientation DC: ' + deg(Phi2Parallels)$'.3' + ' degrees')
set_tposition (WindowHandleZoom, Row1Parallels[0] + 10, Col1Parallels[0])
write_string (WindowHandleZoom, 'A')
set_tposition (WindowHandleZoom, Row1Parallels[1] + 10, Col1Parallels[1])
write_string (WindowHandleZoom, 'B')
set_tposition (WindowHandleZoom, Row2Parallels[0] + 10, Col2Parallels[0])
write_string (WindowHandleZoom, 'C')
set_tposition (WindowHandleZoom, Row2Parallels[1] + 10, Col2Parallels[1])
write_string (WindowHandleZoom, 'D')
*
dev_set_color ('yellow')
disp_line (WindowHandleZoom, Row1Parallels[0], Col1Parallels[0], Row2Parallels[1], Col2Parallels[1])
disp_line (WindowHandleZoom, Row1Parallels[1], Col1Parallels[1], Row2Parallels[0], Col2Parallels[0])
disp_continue_message (WindowHandleZoom, 'black', 'true')
stop ()
endfor
dev_set_color (‘white’)
dev_set_draw (‘margin’)
dev_display (Image)
dev_display (Rectangle)
set_tposition (WindowHandleZoom, 120, 230)
write_string (WindowHandleZoom, ‘Lenght / Width of smallest enclosing rectangle:’)
set_tposition (WindowHandleZoom, 140, 230)
write_string (WindowHandleZoom, (2 * Length1_Region) ′ . 4 ′ + ′ / ′ + ( 2 ∗ L e n g t h 2 R e g i o n ) '.4' + ' / ' + (2 * Length2_Region) ′.4′+′/′+(2∗Length2Region)’.4’ + ’ pixels’)
disp_continue_message (WindowHandleZoom, ‘black’, ‘true’)
stop ()
dev_set_window (WindowHandleZoom)
dev_close_window ()
处理思路
这个例子是主要讲解了xld轮廓拟合进行尺寸测量的例子。其中展示了几种方法:使用区域与xld边缘进行区域的中心提取;使用不同的方式求取两条直线的交点。
后记
大家有什么问题可以向我提问哈,我看到了第一时间回复,希望在学习的路上多多结交良师益友。