halcon拟合圆

通过边缘拟合圆和线

edges_sub_pix 边缘分割,segment_contours_xld 分割边缘为 圆和线或者椭圆

circle.hdev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
* 
* In the subdomain of the image containing the edges,
* extract subpixel precise edges.
edges_sub_pix (ImageReduced, Edges, 'canny', 2, 20, 60)
segment_contours_xld (Edges, ContoursSplit, 'lines_circles', 5, 4, 3)
count_obj (ContoursSplit, Number)
dev_display (Image)
dev_set_draw ('margin')
dev_set_color ('white')
dev_update_window ('off')
for I := 1 to Number by 1
select_obj (ContoursSplit, ObjectSelected, I)
get_contour_global_attrib_xld (ObjectSelected, 'cont_approx', Attrib)
* Fit a circle to the line segment that are arcs of a circle
if (Attrib > 0)
fit_circle_contour_xld (ObjectSelected, 'ahuber', -1, 2, 0, 3, 2, Row, Column, Radius, StartPhi, EndPhi, PointOrder)
gen_circle_contour_xld (ContCircle, Row, Column, Radius, 0, rad(360), 'positive', 1.0)
dev_display (ContCircle)
endif
endfor
dev_set_colored (12)
dev_set_line_width (3)
dev_display (ContoursSplit)

例子2:

measure_circles.hedv

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
* This program fits circles into circular contour segments
* to extract their positions and radii
*
dev_update_off ()
*
* step: acquire image
*
read_image (Image, 'circle_plate')
get_image_size (Image, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width / 2, Height / 2, '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, 14, 'mono', 'true', 'false')
disp_continue_message (WindowID, 'black', 'true')
stop ()
*
* step: segment image into regions
*
dev_set_colored (12)
dev_set_line_width (2)
dev_set_draw ('fill')
fast_threshold (Image, Region, 200, 255, 20)
connection (Region, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 70, 50000)
dev_display (Image)
dev_display (SelectedRegions)
disp_continue_message (WindowID, 'black', 'true')
stop ()
*
* step: create ROI for contour processing
*
boundary (SelectedRegions, RegionBorder, 'inner_filled')
dilation_circle (RegionBorder, RegionDilation, 3.5)
union1 (RegionDilation, RegionUnion)
reduce_domain (Image, RegionUnion, ImageReduced)
dev_clear_window ()
dev_display (ImageReduced)
disp_continue_message (WindowID, 'black', 'true')
stop ()
*
* step: create contours and fit circles to them
*
edges_sub_pix (ImageReduced, Edges, 'canny', 1.5, 10, 40)
segment_contours_xld (Edges, ContoursSplit, 'lines_circles', 5, 2, 2)
select_contours_xld (ContoursSplit, SelectedContours, 'contour_length', 25, 99999, -0.5, 0.5)
count_obj (SelectedContours, NumberContours)
gen_empty_obj (Circles)
for i := 1 to NumberContours by 1
select_obj (SelectedContours, ObjectSelected, i)
get_contour_global_attrib_xld (ObjectSelected, 'cont_approx', Attrib)
if (Attrib == 1)
concat_obj (Circles, ObjectSelected, Circles)
endif
endfor
union_cocircular_contours_xld (Circles, UnionContours, rad(60), rad(10), rad(30), 100, 50, 10, 'true', 1)
dev_clear_window ()
dev_set_color ('black')
dev_display (UnionContours)
disp_continue_message (WindowID, 'black', 'true')
stop ()
count_obj (UnionContours, NumberCircles)
CenterRow := []
CenterColumn := []
dev_clear_window ()
dev_set_color ('black')
set_display_font (WindowID, 12, 'mono', 'true', 'false')
dev_display (SelectedContours)
for i := 1 to NumberCircles by 1
select_obj (UnionContours, ObjectSelected, i)
fit_circle_contour_xld (ObjectSelected, 'algebraic', -1, 0, 0, 3, 2, Row, Column, Radius, StartPhi, EndPhi, PointOrder)
gen_circle_contour_xld (ContCircle, Row, Column, Radius, 0, rad(360), 'positive', 1.5)
dev_set_color ('white')
dev_display (ContCircle)
if (i == 1)
Row2 := Row + Radius * sin(rad(-45))
Column2 := Column + Radius * cos(rad(-45))
set_tposition (WindowID, Row2 - 35, Column2 + 5)
endif
if (i > 1)
exist := 0
for j := 0 to i - 2 by 1
distance_pp (Row, Column, CenterRow[j], CenterColumn[j], DistanceCenters)
if (DistanceCenters < 20)
exist := 1
endif
endfor
if (exist == 1)
Row2 := Row + Radius * sin(rad(-135))
Column2 := Column + Radius * cos(rad(-135))
set_tposition (WindowID, Row2 - 40, Column2 - 30)
else
Row2 := Row + Radius * sin(rad(-45))
Column2 := Column + Radius * cos(rad(-45))
set_tposition (WindowID, Row2 - 35, Column2 + 5)
endif
endif
disp_arrow (WindowID, Row, Column, Row2, Column2, 2)
write_string (WindowID, i)
if (i < 8)
disp_message (WindowID, 'R' + i + ' = ' + Radius$'.4', 'window', i * 20, 130, 'black', 'false')
else
disp_message (WindowID, 'R' + i + ' = ' + Radius$'.4', 'window', (i - 7) * 20, 400, 'black', 'false')
endif
CenterRow := [CenterRow,Row]
CenterColumn := [CenterColumn,Column]
endfor
*
dev_update_window ('on')

边缘分割的案例

1
2
3
4
5
6

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)

BoundaryType—边界类型

‘inner’,‘inner_filled’,‘outer’

如果是’inner’是内层边界,'inner_filled’是内部孔的边界,'outer’是外出边.