例程功能

找钻孔直径。

代码如下

dev_update_off ()
* ****
* step: acquire image
* ****
read_image (Image, 'rim')
get_image_size (Image, Width, Height)
dev_open_window_fit_image (Image, 0, 0, Width, Height, WindowID)
set_display_font (WindowID, 14, 'mono', 'true', 'false')
* ****
* step: determine region of interest (ROI)
* ****
threshold (Image, Dark, 0, 128)
connection (Dark, DarkRegions)
select_shape (DarkRegions, Circles, ['circularity', 'area'], 'and', [0.85, 50], [1.0, 99999])
boundary (Circles, RegionBorder, 'inner')
dilation_circle (RegionBorder, RegionDilation, 6.5)
union1 (RegionDilation, ROIEdges)
dev_display (Image)
dev_set_color ('yellow')
dev_set_draw ('margin')
dev_display (ROIEdges)
disp_continue_message (WindowID, 'black', 'true')
stop ()
* ****
* step: extract edges
* ****
reduce_domain (Image, ROIEdges, ImageROI)
edges_sub_pix (ImageROI, Edges, 'lanser2', 0.3, 10, 30)
sort_contours_xld (Edges, SortedContours, 'upper_left', 'true', 'row')
dev_display (Image)
colored_display (SortedContours, ['cyan', 'white'])
disp_continue_message (WindowID, 'black', 'true')
stop ()
* ****
* step: process contours
* ****
fit_ellipse_contour_xld (Edges, 'ftukey', -1, 2, 0, 200, 3, 2, Row, Column, Phi, Ra, Rb, StartPhi, EndPhi, PointOrder)
NumHoles := |Ra|
gen_ellipse_contour_xld (ContEllipse, Row, Column, Phi, Ra, Rb, gen_tuple_const(NumHoles,0), gen_tuple_const(NumHoles,rad(360)), gen_tuple_const(NumHoles,'positive'), 1)
dev_display (Image)
sort_contours_xld (ContEllipse, SortedContEllipse, 'upper_left', 'true', 'row')
colored_display (SortedContEllipse, ['cyan', 'white'])
dev_set_color ('yellow')
for i := 0 to NumHoles - 1 by 1
    sinPhi := sin(Phi[i])
    cosPhi := cos(Phi[i])
    disp_arrow (WindowID, Row[i], Column[i], Row[i] - sinPhi * Ra[i], Column[i] + cosPhi * Ra[i], 1)
    disp_arrow (WindowID, Row[i], Column[i], Row[i] - cosPhi * Rb[i], Column[i] - sinPhi * Rb[i], 1)
    disp_arrow (WindowID, Row[i], Column[i], Row[i] + sinPhi * Ra[i], Column[i] - cosPhi * Ra[i], 1)
    disp_arrow (WindowID, Row[i], Column[i], Row[i] + cosPhi * Rb[i], Column[i] + sinPhi * Rb[i], 1)
    set_tposition (WindowID, Row[i] - Rb[i] - 50, Column[i] - 85)
    write_string (WindowID, 'D1=' + 2 * Ra[i])
    set_tposition (WindowID, Row[i] - Rb[i] - 30, Column[i] - 85)
    write_string (WindowID, 'D2=' + 2 * Rb[i])
endfor

要求

  1. 轮廓提取是一个耗时的步骤,因此有必要在轮廓提取前提取ROI
	threshold (Image, Dark, 0, 128)
	connection (Dark, DarkRegions)
	select_shape (DarkRegions, Circles, ['circularity', 'area'], 'and', [0.85, 50], [1.0, 99999])
	boundary (Circles, RegionBorder, 'inner')
	dilation_circle (RegionBorder, RegionDilation, 6.5)
	union1 (RegionDilation, ROIEdges)
  1. 用椭圆合轮廓fit_ellipse_contour_xld,拟合返回的参数可以用来生成gen_ellipse_contour_xld并显示椭圆(用圆拟合也行fit_circle_contour_xld ~> gen_circle_contour_xld);
	fit_ellipse_contour_xld (Edges, 'ftukey', -1, 2, 0, 200, 3, 2, Row, Column, Phi, Ra, Rb, StartPhi, EndPhi, PointOrder)
	NumHoles := |Ra|
	gen_ellipse_contour_xld (ContEllipse, Row, Column, Phi, Ra, Rb, gen_tuple_const(NumHoles,0), gen_tuple_const(NumHoles,rad(360)), gen_tuple_const(NumHoles,'positive'), 1)
01-31 13:22