C:\Users\Public\Documents\MVTec\HALCON-23.05-Progress\examples\hdevelop\Tools\Geometry\distance_pc.hdev
例程相对简单
一直没关注过这个算子:最近总在找一个合适的矩形计算的东西。可能有用,记录
点到contour的距离
distance_pc (Hole, Row[i - 1], Column[i - 1], DistanceMin, DistanceMax)
点和contour可以是包含,可以是点在contour上,也可以是点在外。
读图并筛选到类圆形,且直接用外界椭圆的方式取得圆心。
计算得到圆心位置到待拟合轮廓的最大最小距离。
* Example program for distance_pc
* Determine the minimal and maximal radius of drill holes
*
dev_close_window ()
read_image (Rim, 'rim')
get_image_size (Rim, Width, Height)
dev_open_window (0, 0, 500, 500, 'black', WindowID)
dev_display (Rim)
set_display_font (WindowID, 14, 'mono', 'false', 'false')
* Segment the circular holes
* The alpha parameter was chosen different than the default value
* to ensure stronger smoothing and thus more connected edge components
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')
* Determine the midpoints
smallest_circle_xld (Holes, Row, Column, Radius)
gen_circle_contour_xld (ContCircle, Row, Column, Radius, 0, 6.28318, 'positive', 1)
dev_display (Rim)
dev_display (ContCircle)
stop ()
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
gen_circle (Circle, Row[i - 1], Column[i - 1], 2)
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 ()