此例子是通过代码测出工件的长、宽
*采用拟合的方法
rgb1_to_gray (Image, GrayImage)
threshold (GrayImage, Regions, 0, 174)
connection (Regions, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 218807, 326147)
*获得行列坐标
area_center (SelectedRegions, AreaRegion, RowCenterRegion, ColumnCenterRegion)
orientation_region (SelectedRegions, OrientationRegion)
dev_display (SelectedRegions)
dev_display (Image)
*显示提示信息
disp_message (WindowHandle, 'Center Row: '+RowCenterRegion$'.5', 'window', 20, 10, 'red', 'true')
disp_message (WindowHandle, 'Area: '+AreaRegion + ' pixel', 'window', 20, 300, 'red', 'true')
disp_message (WindowHandle, 'Center Column: '+ColumnCenterRegion$'.5', 'window', 60, 10, 'blue', 'true')
disp_message (WindowHandle, 'Orientation: '+OrientationRegion$'.3' + ' rad' , 'window', 60, 300, 'blue', 'true')
*显示十字叉+箭头
disp_cross (WindowHandle, RowCenterRegion, ColumnCenterRegion, 25, 0)
*获得亚像素轮廓
edges_sub_pix (GrayImage, Edges, 'canny', 0.6, 20, 40)
*利用特征直方图
select_shape_xld (Edges, SelectedXLD, ['width','height'], 'and', [0,338.07], [2000,407.8])
*分割
segment_contours_xld (SelectedXLD, ContoursSplit, 'lines_circles', 5, 4, 2)
dev_set_colored (12)
*选出两条宽
select_shape_xld (ContoursSplit, SelectedXLD1, ['width','height'], 'and', [0,63.76], [2000,500])
sort_contours_xld (SelectedXLD1, SortedContour1, 'character', 'true', 'column')
*得到起点和终点的行列坐标 直线法向量的行列坐标 原点到直线的距离
fit_line_contour_xld (SortedContour1, 'tukey', -1, 0, 5, 2, RowBegin0, ColBegin0, RowEnd0, ColEnd0, Nr, Nc, Dist)
*显示第一条直线
gen_contour_polygon_xld (line1, [RowBegin0[0],RowEnd0[0]], [ColBegin0[0],ColEnd0[0]])
*显示第二条直线
gen_contour_polygon_xld (line2, [RowBegin0[1],RowEnd0[1]], [ColBegin0[1],ColEnd0[1]])
dev_display (ContoursSplit)
select_shape_xld (ContoursSplit, SelectedXLD2, ['width','height'], 'and', [107.34,0], [1000,241.74])
sort_contours_xld (SelectedXLD2, SortedContours, 'character', 'true', 'row')
fit_line_contour_xld (SelectedXLD2, 'tukey', -1, 0, 5, 2, RowBegin1, ColBegin1, RowEnd1, ColEnd1, Nr1, Nc1, Dist1)
*显示第三条直线
gen_contour_polygon_xld (line3, [RowBegin1[0],RowEnd1[0]], [ColBegin1[0],ColEnd1[0]])
*显示第四条直线
gen_contour_polygon_xld (line4, [RowBegin1[1],RowEnd1[1]], [ColBegin1[1],ColEnd1[1]])
*测算出长、利用一个点到一条直线的距离
distance_pl (RowBegin0[0], ColBegin0[0], RowBegin0[1], ColBegin0[1], RowEnd0[1], ColEnd0[1], Distance1)
disp_message (WindowHandle, '竖直边的距离 '+Distance1, 'window', 300, 10, 'blue', 'true')
*测算出宽、利用一个点到一条直线的距离
distance_pl (RowBegin0[0], ColBegin1[0], RowBegin1[1], ColBegin1[1], RowEnd1[1], ColEnd1[1], Distance2)
disp_message (WindowHandle, '水平边的距离 '+Distance2, 'window', 360, 10, 'blue', 'true')
*灰度化
rgb1_to_gray (Image, GrayImage)
*二值化
threshold (GrayImage, Regions, 0, 174)
*形成不同的连通域
connection (Regions, ConnectedRegions)
*利用面积特征选择出矩形
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 218807, 326147)
*获得行列坐标
area_center (SelectedRegions, AreaRegion, RowCenterRegion, ColumnCenterRegion)
orientation_region (SelectedRegions, OrientationRegion)
dev_display (SelectedRegions)
dev_display (Image)
*显示提示信息
disp_message (WindowHandle, 'Center Row: '+RowCenterRegion$'.5', 'window', 20, 10, 'red', 'true')
disp_message (WindowHandle, 'Area: '+AreaRegion + ' pixel', 'window', 20, 300, 'red', 'true')
disp_message (WindowHandle, 'Center Column: '+ColumnCenterRegion$'.5', 'window', 60, 10, 'blue', 'true')
disp_message (WindowHandle, 'Orientation: '+OrientationRegion$'.3' + ' rad' , 'window', 60, 300, 'blue', 'true')
*显示十字叉+箭头
disp_cross (WindowHandle, RowCenterRegion, ColumnCenterRegion, 25, 0)
*获得亚像素轮廓
edges_sub_pix (GrayImage, Edges, 'canny', 0.6, 20, 40)
*利用特征直方图
select_shape_xld (Edges, SelectedXLD, ['width','height'], 'and', [0,338.07], [2000,407.8])
*分割
segment_contours_xld (SelectedXLD, ContoursSplit, 'lines_circles', 5, 4, 2)
dev_set_colored (12)
*选出两条宽
select_shape_xld (ContoursSplit, SelectedXLD1, ['width','height'], 'and', [0,63.76], [2000,500])
*对两条直线按照列排序
sort_contours_xld (SelectedXLD1, SortedContour1, 'character', 'true', 'column')
*拟合直线
*得到起点和终点的行列坐标 直线法向量的行列坐标 原点到直线的距离
fit_line_contour_xld (SortedContour1, 'tukey', -1, 0, 5, 2, RowBegin0, ColBegin0, RowEnd0, ColEnd0, Nr, Nc, Dist)
*显示第一条直线
gen_contour_polygon_xld (line1, [RowBegin0[0],RowEnd0[0]], [ColBegin0[0],ColEnd0[0]])
*显示第二条直线
gen_contour_polygon_xld (line2, [RowBegin0[1],RowEnd0[1]], [ColBegin0[1],ColEnd0[1]])
*显示矩形
dev_display (ContoursSplit)
*利用宽度和高度特征挑选出上下两条线
select_shape_xld (ContoursSplit, SelectedXLD2, ['width','height'], 'and', [107.34,0], [1000,241.74])
*利用行进行排序
sort_contours_xld (SelectedXLD2, SortedContours, 'character', 'true', 'row')
*拟合直线
fit_line_contour_xld (SelectedXLD2, 'tukey', -1, 0, 5, 2, RowBegin1, ColBegin1, RowEnd1, ColEnd1, Nr1, Nc1, Dist1)
*显示第三条直线
gen_contour_polygon_xld (line3, [RowBegin1[0],RowEnd1[0]], [ColBegin1[0],ColEnd1[0]])
*显示第四条直线
gen_contour_polygon_xld (line4, [RowBegin1[1],RowEnd1[1]], [ColBegin1[1],ColEnd1[1]])
*清除窗口
dev_clear_window ()
*显示两条直线
dev_display (line1)
dev_display (line2)
*测算出长、利用一个点到一条直线的距离
distance_pl (RowBegin0[0], ColBegin0[0], RowBegin0[1], ColBegin0[1], RowEnd0[1], ColEnd0[1], Distance1)
*显示信息
disp_message (WindowHandle, '竖直边的距离 '+Distance1, 'window', 100, 10, 'blue', 'true')
*清除窗口
dev_clear_window ()
*显示两条直线
dev_display (line3)
dev_display (line4)
*测算出宽、利用一个点到一条直线的距离
distance_pl (RowBegin0[0], ColBegin1[0], RowBegin1[1], ColBegin1[1], RowEnd1[1], ColEnd1[1], Distance2)
*显示信息
disp_message (WindowHandle, '水平边的距离 '+Distance2, 'window', 100, 10, 'blue', 'true')
*对灰度图像进行亚像素轮廓提取
edges_sub_pix (GrayImage, Edges3, 'canny', 1, 20, 40)
*利用圆度和宽度特征选择
select_shape_xld (Edges3, SelectedXLD3, ['circularity','width'], 'and', [0.13486,27.52], [0.33119,647.71])
*排序
sort_contours_xld (SelectedXLD3, SortedContours1, 'character', 'true', 'row')
*联合共圆的
union_cocircular_contours_xld (SortedContours1, UnionContours, 0.5, 0.1, 0.2, 30, 10, 10, 'true', 1)
*拟合圆
fit_circle_contour_xld (UnionContours, 'algebraic', -1, 0, 0, 3, 2, Row1, Column1, Radius1, StartPhi1, EndPhi1, PointOrder)
*画出圆的轮廓
gen_circle_contour_xld (ContCircle, Row1, Column1, Radius1, 0, 6.28318, 'positive', 1)
*清除窗口
dev_clear_window ()
*显示左边的直线和圆
dev_display (line1)
dev_display (ContCircle)
*求圆心到直线的距离
distance_pl (Row1, Column1, RowBegin0[0], ColBegin0[0], RowEnd0[0], ColEnd0[0], DistanceR_left)
*显示信息
disp_message (WindowHandle, '圆心到左边直线的距离 '+DistanceR_left+' 半径 '+Radius1, 'window', 100, 10, 'blue', 'true')
*清除窗口
dev_clear_window ()
*显示上方的直线和圆
dev_display (line3)
dev_display (ContCircle)
*计算距离
distance_pp (Row1, Column1, RowEnd1[0], ColEnd1[0], DistanceR_upendpoint)
*显示信息
disp_message (WindowHandle, '圆心到上边直线末尾点(左边点)的距离 '+DistanceR_upendpoint+' 半径 '+Radius1, 'window', 100, 10, 'blue', 'true')
*清除窗口
dev_clear_window ()
*显示直线
dev_display (line2)
dev_display (line3)
*求右边直线与上面直线的夹角
angle_ll (RowBegin0[1], ColBegin0[1], RowEnd0[1], ColEnd0[1], RowBegin1[0], ColBegin1[0], RowEnd1[0], ColEnd1[0], Angle)
*显示信息
disp_message (WindowHandle, '两直线的夹角为 '+ Angle + ' rad', 'window', 100, 10, 'blue', 'true')