1,直线拟合示例。
dev_close_window ()
read_image(Image, 'screw_thread')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width/2, Height/2, 'black', WindowHandle)
dev_display (Image)
threshold (Image, Region, 0, 120)
closing_rectangle1 (Region, RegionClosing, 5, 5)
*进行仿射变换
hom_mat2d_identity (HomMat2DIdentity)
area_center (RegionClosing, Area, Row, Column)
orientation_region (RegionClosing, Phi)
hom_mat2d_rotate (HomMat2DIdentity, -Phi, Row, Column, HomMat2DRotate)
affine_trans_image (Image, ImageAffinTrans, HomMat2DRotate, 'constant', 'false')
*选择感兴趣的区域
rectangle1_domain (ImageAffinTrans, ImageReduced, 364, 100, 632, 900)
edges_sub_pix (ImageReduced, Edges, 'canny', 1, 30, 40)
select_contours_xld (Edges, SelectedContours, 'contour_length', 700, 1000, -0.5, 0.5)
count_obj (SelectedContours, Number)
if(Number!=2)
stop ()
else
*进行直线拟合
fit_line_contour_xld (SelectedContours, 'tukey', -1, 0, 5, 2, RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist)
*生成直线
gen_region_line (RegionLines, RowBegin, ColBegin, RowEnd, ColEnd)
dev_clear_window ()
dev_display (ImageReduced)
dev_display (RegionLines)
endif
效果。
原图 直线拟合
2,圆拟合示例。
dev_close_window ()
read_image (Image, 'earth')
rgb1_to_gray (Image, GrayImage)
get_image_size (GrayImage, Width, Height)
dev_open_window (0, 0, Width*2, Height*2, 'black', WindowHandle)
dev_display (GrayImage)
threshold (GrayImage, Regions, 60, 255)
closing_circle (Regions, RegionClosing, 10.5)
*获取轮廓
gen_contour_region_xld (RegionClosing, Contours, 'border')
*代数距离测度拟合,geotukey拟合更精准,其不受圆残缺的影响
fit_circle_contour_xld (Contours, 'atukey', -1, 2, 0, 10, 1, Row, Column, Radius, StartPhi, EndPhi, PointOrder)
*几何距离测度
fit_circle_contour_xld (Contours, 'geotukey', -1, 0, 0, 3, 2, Row1, Column1, Radius1, StartPhi1, EndPhi1, PointOrder1)
*绘制圆
gen_circle_contour_xld (ContCircle, Row, Column, Radius, 0, 6.28318, 'positive', 1)
gen_circle_contour_xld (ContCircle1, Row1, Column1, Radius1, 0, 6.28318, 'positive', 1)
dev_clear_window ()
dev_display (GrayImage)
dev_display (ContCircle)
dev_set_color ('green')
dev_display (ContCircle1)
效果
原图 圆拟合
3,矩形拟合示例。
read_image (Image, 'punched_holes')
fast_threshold (Image, Region, 128, 255, 20)
boundary (Region, RegionBorder, 'inner')
dilation_rectangle1 (RegionBorder, EdgeRoi, 7, 7)
reduce_domain (Image, EdgeRoi, ImageReduced)
edges_sub_pix (ImageReduced, Edges, 'canny', 1.7, 40, 120)
select_contours_xld (Edges, SelectedContours, 'contour_length', 500, 100000, -0.5, 0.5)
*进行矩形拟合
fit_rectangle2_contour_xld (SelectedContours, 'tukey', -1, 0, 0, 3, 2, Row, Column, Phi, Length1, Length2, PointOrder)
*绘制矩形
gen_rectangle2_contour_xld (Rectangle, Row, Column, Phi, Length1, Length2)
dev_clear_window ()
dev_display (Image)
dev_display (Rectangle)
效果
原图 矩形拟合
4,轮廓排序示例。
dev_close_window ()
read_image (Image, 'pumpe')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_display (Image)
fast_threshold (Image, Region, 0, 70, 150)
connection (Region, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, ['outer_radius','anisometry','area'], 'and', [5,1,100], [50,1.8,5000])
shape_trans (SelectedRegions, RegionTrans, 'outer_circle')
dilation_circle (RegionTrans, RegionDilation, 5.5)
union1 (RegionDilation, RegionUnion)
reduce_domain (Image, RegionUnion, ImageReduced)
stop ()
emphasize (ImageReduced, ImageEmphasize, 6, 5, 1)
threshold_sub_pix (ImageReduced, Border, 80)
*选择轮廓
select_shape_xld (Border, SelectedXLD, ['contlength','outer_radius'], 'and', [70,15], [9999,9999])
*将轮廓分解为直线,圆弧等片段
segment_contours_xld (SelectedXLD, ContoursSplit, 'lines_circles', 4, 2, 2)
select_shape_xld (ContoursSplit, SelectedXLD1, ['outer_radius','contlength'], 'and', [15,30], [45,99999])
*合并属于同于圆周的轮廓
union_cocircular_contours_xld (SelectedXLD1, UnionContours, 0.5, 0.1, 0.2, 30, 10, 10, 'true', 1)
*对其进行排序
sort_contours_xld (UnionContours, SortedContours, 'upper_left', 'true', 'column')
dev_clear_window ()
dev_display (ImageReduced)
dev_display (Image)
count_obj (SortedContours, Number)
for Index := 1 to Number by 1
*获取轮廓的拟合
select_obj (SortedContours, ObjectSelected, Index)
fit_circle_contour_xld (Object