## 查 找 圆 并 计 算 圆 的 半 径 查找圆并计算圆的半径 查找圆并计算圆的半径

connection()
select_obj()
select_shae()

select_shape_xld()
segment_contours_xld()
select_shape_xld()

shape_trans()

read_image (Image, 'pumpe')
* 1、阈值分割
* threshold (Image, Region, 0, 128)
local_threshold (Image, Region, 'adapted_std_deviation', 'dark', [], [])

connection (Region, ConnectedRegions)

* 2、提取圆形轮廓：select_shape、shape_trans
* edges_sub_pix (ConnectedRegions, Edges, 'canny', 1, 40, 70)
* select_shape (ConnectedRegions, SelectedRegions, 'circularity', 'and', 1, 100)
select_shape (ConnectedRegions, SelectedRegions2, 'outer_radius', 'and', 13, 1000)
select_shape (SelectedRegions2, SelectedRegions1, 'circularity', 'and', 0.06, 1)
select_shape (SelectedRegions1, SelectedRegions3, 'area', 'and', 75, 99999)
shape_trans( SelectedRegions3, RegionTrans, 'outer_circle')
dilation_circle (RegionTrans, RegionErosion, 3.5)
union1 (RegionErosion, RegionUnion)
reduce_domain (Image, RegionUnion, ImageReduced1)
connection (ImageReduced1, ConnectedRegions1)
count_obj (ConnectedRegions1, Number)
Tuple := []
for Index := 1 to Number  by 1
select_obj (ConnectedRegions1, ObjectSelected, Index)
reduce_domain (ImageReduced1, ObjectSelected, ImageReduced2)
threshold_sub_pix (ImageReduced2, Border, 100)
select_shape_xld (Border, SelectedXLD, 'contlength', 'and', 35, 9999)
segment_contours_xld (SelectedXLD, ContoursSplit, 'lines_circles', 5, 4, 2)
fit_circle_contour_xld (SelectedXLD, 'geometric', 100, 0, 0, 8, 2, Row, Column, Radius, StartPhi, EndPhi, PointOrder)
endfor

stop()

* 1.读取图片

* 2.创建模板

* 3.匹配模板

* 4.找出区域

* 5.画出轮廓

* 6.计算半径

* 1、阈值分割
* threshold (Image, Region, 0, 128)
local_threshold (Image, Region, 'adapted_std_deviation', 'dark', [], [])

connection (Region, ConnectedRegions)

* 2、提取圆形轮廓：select_shape、shape_trans
* edges_sub_pix (ConnectedRegions, Edges, 'canny', 1, 40, 70)
* select_shape (ConnectedRegions, SelectedRegions, 'circularity', 'and', 1, 100)

select_shape (ConnectedRegions, SelectedRegions, ['outer_radius','anisometry'], 'and',[16,1] ,[30,5])

* 3、从圆形轮廓中提取大圆：dilation_circle
dilation_circle (SelectedRegions, RegionDilation, 3.5)

* 4、把大圆的灰度图分割出来：reduce_domain
union1 (RegionDilation, RegionUnion)

reduce_domain (Image, RegionUnion, ImageReduced)

edges_sub_pix (ImageReduced, Edges, 'canny', 2, 4, 60)

select_shape_xld(Edges, SelectedXLD1, 'area', 'and', 0, 520000)

fit_circle_contour_xld(SelectedXLD1, 'algebraic', -1, 0, 0, 3, 2, Row, Column, Radius, StartPhi, EndPhi, PointOrder)

*disp_message(3600, '内轮廓圆心坐标：X=' + Row + ',Y=' + Column +',半径=' + Radius +'.', 'window', -1, -1, 'black', 'true')

* 5、从分割出来的图像中提取大圆的轮廓：threshold_sub_pix、select_shape_xld、segment_contours_xld

* 6、拟合大圆：fit_circle_contour_xld

read_image (Image, 'pumpe')
* 1、阈值分割
* threshold (Image, Region, 0, 128)
local_threshold (Image, Region, 'adapted_std_deviation', 'dark', [], [])

connection (Region, ConnectedRegions)

* 2、提取圆形轮廓：select_shape、shape_trans
* edges_sub_pix (ConnectedRegions, Edges, 'canny', 1, 40, 70)
* select_shape (ConnectedRegions, SelectedRegions, 'circularity', 'and', 1, 100)
select_shape (ConnectedRegions, SelectedRegions2, 'outer_radius', 'and', 13, 1000)
select_shape (SelectedRegions2, SelectedRegions1, 'circularity', 'and', 0.06, 1)
select_shape (SelectedRegions1, SelectedRegions3, 'area', 'and', 75, 99999)
shape_trans( SelectedRegions3, RegionTrans, 'outer_circle')
dilation_circle (RegionTrans, RegionErosion, 2)
union1 (RegionErosion, RegionUnion)
reduce_domain (Image, RegionUnion, ImageReduced1)
connection (ImageReduced1, ConnectedRegions1)
count_obj (ConnectedRegions1, Number)
Tuple := []
for Index := 1 to Number  by 1
select_obj (ConnectedRegions1, ObjectSelected, Index)
reduce_domain (ImageReduced1, ObjectSelected, ImageReduced2)

edges_sub_pix(ImageReduced2,Border,'canny',1.1,5,18)
*select_shape_xld (Border, SelectedXLD, 'outer_radius', 'and', 17, 22)
select_shape_xld (Border, SelectedXLD1, 'contlength', 'and', 30,9000)
select_shape_xld (SelectedXLD1, SelectedXLD2, 'outer_radius', 'and', 15.3, 22)
fit_circle_contour_xld (SelectedXLD2, 'geometric', 100, 0, 0, 8, 2, Row, Column, Radius, StartPhi, EndPhi, PointOrder)
Tuple[Index-1]:=Max
endfor

* This program gets the deviation between
* circular contours and their best fitting circles
*
dev_update_var ('off')
dev_update_off ()
*
* step: acquire image and initialize the 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 (1)
dev_set_color ('red')
dev_set_draw ('margin')
dev_display (Image)
set_display_font (WindowID, 16, 'mono', 'true', 'false')
disp_continue_message (WindowID, 'black', 'true')
stop ()
*
* step: create ROI
*
fast_threshold (Image, Region, 0, 70, 150)
dev_set_colored (3)
connection (Region, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, ['outer_radius','anisometry','area'], 'and', [5,1,100], [50,1.8,99999])
shape_trans (SelectedRegions, RegionTrans, 'outer_circle')
dilation_circle (RegionTrans, RegionDilation, 5.5)
union1 (RegionDilation, RegionUnion)
reduce_domain (Image, RegionUnion, ImageReduced)
dev_clear_window ()
dev_display (ImageReduced)
disp_continue_message (WindowID, 'black', 'true')
stop ()
*
* step: create and select relevant contours
*
threshold_sub_pix (ImageReduced, Border, 80)
select_shape_xld (Border, SelectedXLD, ['contlength','outer_radius'], 'and', [70,15], [99999,99999])
segment_contours_xld (SelectedXLD, ContoursSplit, 'lines_circles', 4, 2, 2)
union_cocircular_contours_xld (SelectedXLD3, UnionContours2, 0.5, 0.1, 0.2, 2, 10, 10, 'true', 1)
sort_contours_xld (UnionContours2, SortedContours, 'upper_left', 'true', 'column')
dev_clear_window ()
dev_set_color ('white')
dev_display (ImageReduced)
dev_display (SortedContours)
disp_continue_message (WindowID, 'black', 'true')
stop ()
*
* step: fit circles into the contours and get the
* average deviation between contour and circle
*
count_obj (SortedContours, NumSegments)
dev_display (Image)
dev_display (SortedContours)
NumCircles := 0
disp_message (WindowID, 'Circle radius and average distance', 'window', 10, 10, 'white', 'false')
disp_message (WindowID, 'between circle and contour:', 'window', 30, 10, 'white', 'false')
for i := 1 to NumSegments by 1
select_obj (SortedContours, SingleSegment, i)
NumCircles := NumCircles + 1
fit_circle_contour_xld (SingleSegment, 'atukey', -1, 2, 0, 5, 2, Row, Column, Radius, StartPhi, EndPhi, PointOrder)
disp_message (WindowID, 'R' + NumCircles + ': ' + Radius$'.3', 'window', (i - 1) * 50 + 30, 450, 'white', 'false') disp_message (WindowID, 'D_avg: ' + AvgDist$'.3', 'window', ((i - 1) * 50) + 50, 450, 'white', 'false')