对应示例程序:
distance_pc.hdev
目标:确定钻孔最小和最大半径
思路为:
1.读入图像,利用算子edges_sub_pix进行亚像素边缘检测
2.利用算子select_shape_xld和sort_contours_xld进行边缘的细化提取和排序
3.计算边缘的最小外接圆(smallest_circle_xld)
4.最小外接圆的圆心与边缘轮廓点的最小和最大距离,作为钻孔最小和最大半径。借助算子distance_pc实现。
图像:
代码:
dev_close_window ()
read_image (Rim, 'rim')
get_image_size (Rim, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowID)
dev_display (Rim)
set_display_font (WindowID, 14, 'mono', 'false', 'false')
* Segment the circular holes
* The alpha parameter was choosen different than the default value
* to ensure stronger smoothing and thusmore connected edge components
*分割圆孔
*选择的alpha参数与默认值不同
*确保更平滑,从而连接更多边缘组件
edges_sub_pix (Rim, Edges, 'canny', 4, 20, 40) //亚像素边缘检测
select_shape_xld (Edges, Holes, 'circularity', 'and', 0.7, 1.0)
sort_contours_xld (Holes, Holes, 'upper_left', 'true', 'row') //XLD轮廓排序
* Determine the midpoints
smallest_circle_xld (Holes, Row, Column, Radius) //最小外接圆
count_obj (Holes, Number) //统计孔洞数目
dev_set_color ('yellow')
* For all boles ...
for i := 1 to Number by 1
select_obj (Holes, Hole, i)
dev_display (Rim)
dev_display (Hole)
* Compute the minimal and maximal radius of the holes by computing the
* distance of the midpoint to the contour
*通过最小外接圆的圆心到轮廓的距离来计算孔的最小和最大半径
distance_pc (Hole, Row[i - 1], Column[i - 1], DistanceMin, DistanceMax) //计算一个点和一个轮廓之间的距离。
* And visualize the results (minimal and maximal radius)
disp_cross (WindowID, Row[i - 1], Column[i - 1], 6, 0) //可视化圆心
//打印结果
tuple_string (DistanceMin, '.3f', SVal)
get_string_extents (WindowID, 'min: ' + SVal, Ascent, Descent, Width1, Height1)
disp_message (WindowID, 'min: ' + SVal, 'image', Row[i - 1] - 2 * Ascent, Column[i - 1] - Radius[i - 1] - Width1 - 10, 'black', 'true')
disp_message (WindowID, 'max: ' + DistanceMax$'.3f', 'image', Row[i - 1], Column[i - 1] - Radius[i - 1] - Width1 - 10, 'black', 'true')
disp_continue_message (WindowID, 'black', 'true')
stop ()
endfor
dev_close_window ()
用到的几个算子:
edges_sub_pix–使用Deriche、Lanser、Shen或Canny过滤器提取亚像素精确边缘
select_shape_xld --选择符合要求的XLD轮廓
sort_contours_xld–XLD轮廓排序
smallest_circle_xld–最小外接圆
distance_pc–计算一个点和一个轮廓之间的距离