HALCON示例程序measure_metal_part_extended.hdev金属零件尺寸测量
HALCON示例程序measure_metal_part_extended.hdev金屬零件尺寸測量
示例程序源碼(加注釋)
- 關(guān)于顯示類函數(shù)解釋
dev_update_off ()
read_image (Image, ‘metal-parts/metal-parts-01’)
init_visualization (Image, 3, ‘white’, ‘margin’, Width, Height, WindowID)
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) - 通過擬合橢圓求解區(qū)域角度
orientation_region (Region, OrientationRegion)
count_seconds (T2)
dev_display (Region)
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)
- 使用圓形與直線分割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: ')
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′+′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') 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′)devs?etc?olor(′yellow′)intersectionl?ines(RowsBegin[0],ColsBegin[0],RowsEnd[0],ColsEnd[0],RowsBegin[2],ColsBegin[2],RowsEnd[2],ColsEnd[2],RowIntersect2,ColumnIntersect2,IsOverlapping2)distancep?p(RowJunctions[0],ColJunctions[0],RowIntersect2,ColumnIntersect2,Distance2)genc?rossc?ontourx?ld(CrossIntersection2,RowIntersect2,ColumnIntersect2,10,0.785398)devd?isplay(CrossIntersection2)devs?etc?olor(′white′)sett?position(WindowHandleZoom,370,230)writes?tring(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′+′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, 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′)dispa?rrow(WindowHandleZoom,RowsEnd[0],ColsEnd[0],RowsBegin[0],ColsBegin[0],1)dispa?rrow(WindowHandleZoom,RowsBegin[1],ColsBegin[1],RowsEnd[1],ColsEnd[1],1)dispc?ontinuem?essage(WindowHandleZoom,′black′,′true′)stop()devd?isplay(Image)anglel?l(RowsBegin[0],ColsBegin[0],RowsEnd[0],ColsEnd[0],RowsBegin[2],ColsBegin[2],RowsEnd[2],ColsEnd[2],Angle2)sett?position(WindowHandleZoom,RowsBegin[0]?10,ColsBegin[0]+10)writes?tring(WindowHandleZoom,′P2′)sett?position(WindowHandleZoom,RowsEnd[0]?10,ColsEnd[0]+10)writes?tring(WindowHandleZoom,′P1′)sett?position(WindowHandleZoom,120,230)writes?tring(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)
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?Length2Region)'.4' + ' / ' + (2 * Length2_Region)′.4′+′/′+(2?Length2R?egion)’.4’ + ’ pixels’)
disp_continue_message (WindowHandleZoom, ‘black’, ‘true’)
stop ()
dev_set_window (WindowHandleZoom)
dev_close_window ()
處理思路
這個例子是主要講解了xld輪廓擬合進行尺寸測量的例子。其中展示了幾種方法:使用區(qū)域與xld邊緣進行區(qū)域的中心提取;使用不同的方式求取兩條直線的交點。
后記
大家有什么問題可以向我提問哈,我看到了第一時間回復(fù),希望在學(xué)習(xí)的路上多多結(jié)交良師益友。
總結(jié)
以上是生活随笔為你收集整理的HALCON示例程序measure_metal_part_extended.hdev金属零件尺寸测量的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 最优化方法(最速下降、牛顿法、高斯牛顿法
- 下一篇: bzoj1051[kosaraju算法]