halcon示例:物件边缘直线和圆形拟合,并计算长度和角度

By admin at 2018-02-03 • 0人收藏 • 1457人看过

blob.png

blob.png

* This program inpects metal parts in various ways
* 
dev_update_off ()
* 
* step: acquire image
* 
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 ()
* 
* step: get center, area, and orientation via region processing
* 
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 ()
* 
* step: create contours
* 
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)
* 
* step: get center, area, and orientation via contour processing
* 
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 ()
* 
* step: segment and sort contours
* 
segment_contours_xld (Edges, ContoursSplit, 'lines_circles', 6, 4, 4)
sort_contours_xld (ContoursSplit, SortedContours, 'upper_left', 'true', 'column')
dev_clear_window ()
dev_set_colored (12)
dev_display (SortedContours)
stop ()
* 
* step: open zoom window
* 
open_zoom_window (0, round(Width / 2), 115, 225, 395, 535, 2, WindowHandleZoom)
set_display_font (WindowHandleZoom, 14, 'mono', 'true', 'false')
* 
* step: fit circles and lines into contours
* 
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)
    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_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)
        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 ()
* 
* step: get corner points, two approaches (Foerstner, Intersection_ll)
* and the angle between the lines building the corners
* 
dev_display (Image)
dev_set_color ('white')
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 ()
* 
* intersection points and angles calculated
* by the lines obtained by line-fitting
* 
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')
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')
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')
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')
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 ()
* 
* step: get parallels with maximum lengths and the distances between them
* 
gen_polygons_xld (ContoursSplit, Polygons, 'ramer', 2)
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)
    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
* 
* step: visualize width of object obtained by the smallest enclosing rectangle
* 
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 * Length2_Region)$'.4' + ' pixels')
disp_continue_message (WindowHandleZoom, 'black', 'true')
stop ()
dev_set_window (WindowHandleZoom)
dev_close_window ()


登录后方可回帖

登 录
信息栏
 私人小站

本站域名

ChengXu.XYZ

投诉联系:  popdes@126.com



快速上位机开发学习,本站主要记录了学习过程中遇到的问题和解决办法及上位机代码分享

这里主要专注于学习交流和经验分享.
纯私人站,当笔记本用的,学到哪写到哪.
如果侵权,联系 Popdes@126.com

友情链接
Aardio官方
Aardio资源网


才仁机械


网站地图SiteMap

Loading...