在读取一张含有圆形和矩形的图片(如图所示)后,通过画圆或者画矩形确定图片中圆、矩形的大体位置,然后利用计量模型在具有挑战性的条件下轻松地以亚像素精度测量圆和矩形。
算法如下:
dev_update_off ()
read_image (Image, 'C:/Users/asus-pc/Desktop/测试图.png')
get_image_size (Image, Width, Height)
dev_close_window ()
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
dev_display (Image)
*画圆和矩形,粗劣的确定圆和矩形的位置
draw_circle (WindowHandle, Row1, Column1, Radius1)
draw_rectangle2 (WindowHandle, Row2, Column2, Phi2, Length1, Length2)
* 定义圆的近似位置和测量公差,即将之前确定的大概位置的参数赋值
CircleInitRow := [Row1]
CircleInitColumn := [Column1]
gen_cross_contour_xld (Cross1, CircleInitRow, CircleInitColumn, 6, 0.785398)
CircleInitRadius := [Radius1]
CircleRadiusTolerance := 12//圆半径的测量公差
* 定义矩形的近似位置和测量公差
RectangleInitRow := [ Row2]
RectangleInitColumn := [Column2]
RectangleInitPhi := [Phi2]
RectangleInitLength1 := [Length1]
RectangleInitLength2 := [Length2]
RectangleTolerance := 10//矩形测量公差
* gen_cross_contour_xld (Cross1, RectangleInitRow, RectangleInitColumn, 6, 0.785398)
*
* 编制计量模型数据结构
create_metrology_model (MetrologyHandle)
*
* 可以不必预先设置图像宽度,但如果提前设置,可以改善第一次测量的运行时间。
set_metrology_model_image_size (MetrologyHandle, Width, Height)
*
* 将计量矩形对象添加到上述定义的模型中
add_metrology_object_rectangle2_measure (MetrologyHandle, RectangleInitRow, RectangleInitColumn, RectangleInitPhi, RectangleInitLength1, RectangleInitLength2, RectangleTolerance, 5, .5, 1, [], [], MetrologyRectangleIndices)
* 将计量圆对象添加到上述定义的模型中
add_metrology_object_circle_measure (MetrologyHandle, CircleInitRow, CircleInitColumn, CircleInitRadius, CircleRadiusTolerance, 5, 1.5, 2, [], [], MetrologyCircleIndices)
* 可以在一个调用中度量每个度量对象的多个圆/矩形/线/椭圆实例。因为我们喜欢测量每个对象的两个圆,所以我们将'num_instances'设置为2。
set_metrology_object_param (MetrologyHandle, MetrologyCircleIndices, 'num_instances', 2)//该参数指定每个计量对象成功匹配的实例的最大数量,在此之后,匹配将停止
*
* 将“measure_transition”设置为“uniform”确保只返回一致的圆,即只有从亮到暗的边,或者从暗到亮的边。
set_metrology_object_param (MetrologyHandle, MetrologyCircleIndices, 'measure_transition', 'uniform')
* 设置最低分数可以使结果更加可靠
set_metrology_object_param (MetrologyHandle, MetrologyCircleIndices, 'min_score', .9)
*
* 执行测量
*
apply_metrology_model (Image, MetrologyHandle)//测量并拟合一个计量模型中所有计量对象的几何形状
*
get_metrology_object_result (MetrologyHandle, MetrologyRectangleIndices, 'all', 'result_type', 'all_param', RectangleParameter)
* 提取参数以提高可读性
Sequence := [0:5:|RectangleParameter| - 1]//定义五个序列
RectangleRow := RectangleParameter[Sequence]
RectangleColumn := RectangleParameter[Sequence + 1]
RectanglePhi := RectangleParameter[Sequence + 2]
RectangleLength1 := RectangleParameter[Sequence + 3]
RectangleLength2 := RectangleParameter[Sequence + 4]
*
*查看圆的测量结果
get_metrology_object_result (MetrologyHandle, MetrologyCircleIndices, 'all', 'result_type', 'all_param', CircleParameter)
* 提取参数以提高可读性
Sequence := [0:3:|CircleParameter| - 1]
CircleRow := CircleParameter[Sequence]
CircleColumn := CircleParameter[Sequence + 1]
CircleRadius := CircleParameter[Sequence + 2]
*
* 显示结果
*
* 得到测量轮廓
get_metrology_object_result_contour (Contours, MetrologyHandle, 'all', 'all', 1.5)
*
* 得到测量区域的轮廓和边缘点的坐标,作为拟合圆和矩形的基础
get_metrology_object_measures (Contour, MetrologyHandle, 'all', 'all', Row1, Column1)
gen_cross_contour_xld (Cross, Row1, Column1, 6, 0.785398)
* 显示相应结果
Color := ['gray','cyan','green']
dev_display (Image)
dev_set_line_width (1)
dev_set_color (Color[0])
dev_display (Contour)
dev_set_color (Color[1])
dev_display (Cross)
dev_set_line_width (2)
dev_set_color (Color[2])
dev_display (Contours)
Message := Color[2] + ': Measurement result'
Message[1] := Color[1] + ': Edge candidate points'
Message[2] := Color[0] + ': Measure regions'
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
stop ()
* 清除数据
clear_metrology_model (MetrologyHandle)