介绍完简单的一维测量之后,下面我们来介绍下常用的卡尺测量。Halcon中的Metrology方法即为卡尺工具,可用来拟合线,圆,这种方法对于目标比背景很明显的图像尺寸测量是很方便的,不需要用blob进行边缘提取等,但缺点也很明显,需要目标的相对位置基本不变才行。大致的步骤如图所示:
下面我直接用代码来演示,如何用halcon进行卡尺测量。供测量用的图像如下。
代码如下:
read_image (Image, 'C:/Users/Administrator/Desktop/卡尺.png')
rgb1_to_gray (Image, GrayImage)
*上来我们就先创建测量句柄,供后面使用
create_metrology_model (MetrologyHandle)
get_image_size (GrayImage, Width, Height)
*测量窗口制作
draw_line (3600, Row1, Column1, Row2, Column2)
gen_region_line (RegionLines, Row1, Column1, Row2, Column2)
draw_line (3600, Row3, Column3, Row4, Column4)
gen_region_line (RegionLines1, Row3, Column3, Row4, Column4)
stop()
*添加测量对象,设置测量区域大小
set_metrology_model_image_size (MetrologyHandle, Width, Height)
*添加直线对象到测量模型中
Line1:=[Row1, Column1, Row2, Column2]
Line2:=[Row3, Column3, Row4, Column4]
add_metrology_object_generic (MetrologyHandle, 'line', [Line1,Line2], 45, 10, 1, 15, [], [], Index)
*获取测量模型里的模型轮廓
get_metrology_object_model_contour (Contour, MetrologyHandle, 'all', 1.5)
*获取测量模型里的测量区域
get_metrology_object_measures (Contours, MetrologyHandle, 'all', 'all', Row, Column)
dev_display (GrayImage)
dev_display (Contours)
初步效果如图:
这里是为了准备好要找出的两个边的区域来画出两条线,为之后的设置测量区域做准备。
*测量并对测量区域拟合几何形状
apply_metrology_model (GrayImage, MetrologyHandle)
*获取测量结果里的开始行坐标
get_metrology_object_result (MetrologyHandle, Index, 'all', 'result_type', 'row_begin', RowBegin)
*获取测量结果里的开始列坐标
get_metrology_object_result (MetrologyHandle, Index, 'all', 'result_type', 'column_begin', ColBegin)
*获取测量结果里的结束行坐标
get_metrology_object_result (MetrologyHandle, Index, 'all', 'result_type', 'row_end', RowEnd)
*获取测量结果里的结束列坐标
get_metrology_object_result (MetrologyHandle, Index, 'all', 'result_type', 'column_end', ColEnd)
*获取测量区域
get_metrology_object_measures (Contours, MetrologyHandle, 'all', 'all', Row, Column)
*获取测量结果轮廓
get_metrology_object_result_contour (MeasuredLines, MetrologyHandle, 'all', 'all', 1.5)
*显示测量区域和测量结果轮廓
dev_display (GrayImage)
dev_set_color ('green')
dev_display (Contours)
dev_set_color ('blue')
dev_display (MeasuredLines)
*生成两个直线对象并弄到一个区域里面
*count_obj (MeasuredLines, Number)
gen_empty_obj (LineRegion)
for i := 0 to |RowBegin|-1 by 1
gen_region_line (RegionL, RowBegin[i], ColBegin[i], RowEnd[i], ColEnd[i])
concat_obj (LineRegion, RegionL, LineRegion)
endfor
*ji'suan两线之间的距离
select_obj (LineRegion, First, 1)
select_obj (LineRegion, Second, 2)
distance_rr_min (First, Second, MinDistance, Row11, Column11, Row21, Column21)
clear_metrology_model (MetrologyHandle)
最后效果图如下:
最终求得的两条线的距离是:
以上呢就是halcon中卡尺测量的大致实现步骤。