[机器视觉]halcon应用实例 边缘检测

一个学习找边的实例

边缘检测的步骤图解


步骤
1.通过Blob方法获取需要测量的Region

1.1 主要运用图像形态学、二值化

2.创建测量句柄

2.1 create_metrology_model (MetrologyHandle)

3.设置目标图像大小

3.1 set_metrology_model_image_size (MetrologyHandle, Width, Height)

4.添加测量对象

4.1 (通用算子) 默认值:‘circle’,参考值:‘circle’圆,‘ellipse’椭圆,‘line’线,‘rectangle2’矩形,长方形;

add_metrology_object_generic (MetrologyHandle, 'line', LineParam, 20, 5, 1, 30, [], [], Index)

4.2 *circle 圆形工具
add_metrology_object_circle_measure (MetrologyHandle, Row1, Column1, Row1, 20, 5, 1, 30, [], [], Index1)

4.3 circle 椭圆工具
add_metrology_object_ellipse_measure (MetrologyHandle, Row1, Column1, Index1, Index1, Index1, 20, 5, 1, 30, [], [], Index2)

4.4 rectangle 矩形工具
add_metrology_object_rectangle2_measure (MetrologyHandle, Row1, Column1, Index3, Index3, Index3, 20, 5, 1, 30, [], [], Index4)

4.5 line 直线工具

add_metrology_object_line_measure (MetrologyHandle, Row1, Column1, Row2, Column2, 20, 5, 1, 30, [], [], Index)

5.设置测量参数

5.1 *设置找线的方式(positive,negative,all)从黑到白,从白到黑
set_metrology_object_param (MetrologyHandle, 'all', 'measure_transition', 'all')

5.2 *预期测量的区域个数
set_metrology_object_param (MetrologyHandle, 'all', 'num_instances', 12)

5.3*拟合数
set_metrology_object_param (MetrologyHandle, 'all', 'num_instances', 6)

5.4*高斯平滑系数,值越大,唯一的边缘越清晰
set_metrology_object_param (MetrologyHandle, 'all', 'measure_sigma', 5)

5.5 *区域,垂直与边界的测量区域的一半长度
set_metrology_object_param (MetrologyHandle, 'all', 'measure_length1', 80)

5.6 *区域,相切于边缘的测量区域的一半长度
set_metrology_object_param(MetrologyHandle, 'all', 'measure_length2', 10)

5.7 *最小边缘幅度越大,要求找到的边缘越锐利(灰度变换明显),反而不容易找到边缘
set_metrology_object_param (MetrologyHandle, 'all', 'measure_threshold', 20)

5.8 *测量双立方插入值,区别与bilinear双线性
set_metrology_object_param (MetrologyHandle, 'all', 'measure_interpolation', 'bicubic')

5.9 *取值 all,first,best,last
set_metrology_object_param (MetrologyHandle, 'all', 'measure_select', 'all')

5.10 *设置最小分数0.7
set_metrology_object_param (MetrologyHandle, 'all', 'min_score', 0.7)

6.执行测量,获取边缘点集

7.获取最终测量数据和轮廓

8.释放测量句柄

代码

dev_get_window (WindowHandle)
*读取图像
read_image (Image, 'printer_chip/printer_chip_01')
 
*获取图像大小
get_image_size (Image, Width, Height)
 
*方式一绘制测量位置直线
*draw_line (WindowHandle, Row1, Column1, Row2, Column2)
 
*方式二 固定测量位置直线坐标
Row1 := 19.9
Column1 := 810
Row2 := 271.9
Column2 := 808.5
 
*根据绘制的直线坐标来构建参数对象
LineParam := [Row1, Column1,Row2, Column2]
 
*绘制测量位置直线 时把直线的坐标写入文本文件,以变查看。这样就可以知道绘制的坐标。
*实际测量时是不可能让人,手动的去绘制每张要测量的图片的
open_file ('LineParam.txt', 'output', FileHandle)
fileStr := '线的坐标Row1:'+Row1+',Column1:'+Column1+',Row2:'+Row2+',Column2:'+Column2
fwrite_string (FileHandle, fileStr)
disp_message (WindowHandle, fileStr, 'window', 0, 0, 'red', 'true')
 
*创建计算测量句柄
create_metrology_model (MetrologyHandle)
 
*句柄关联测试的图坐标位置
*设置测量对象图像的大小
*参数:
*    MeasureHandle:输入测量模型的句柄;
*    Width:输出图像宽;
*    Height:输出图像高;
set_metrology_model_image_size (MetrologyHandle, Width, Height)
 
*添加测量模型对象(此处是线)
*将测量对象添加到测量模型中
*算子参数:
*    MeasureHandle:输入测量模型的句柄;
*    Shape:输入要测量对象的类型;默认值:‘circle’,参考值:‘circle’圆,‘ellipse’椭圆,‘line’线,‘rectangle2’矩形,长方形;
*    ShapeParam:要添加的测量对象的参数。
*    MeasureLength1:输入垂直于边界的测量区域的一半长度;默认值:20,参考值:102030;最小增量:1.0;建议增量:10.0
*    MeasureLength2:输入与边界相切的测量区域的一半长度;默认值:5,参考值:3510;最小增量:1.0;建议增量:10.0
*    MeasureSigma:输入用于平滑的高斯函数的sigma;默认值:1,参考值:0.40.60.81.01.52.03.04.05.07.010.0;最小增量:0.01;建议增量:0.1;限制:(0.4 <= MeasureSigma)&&(MeasureSigma <= 100*    MeasureThreshold:输入测量阈值/最小边缘幅度;默认值:30,参考值:5.0, 10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 90.0, 110.0;最小增量:0.5;建议增量:2*    GenParamName:输入参数名称;参考: ‘distance_threshold’, ‘end_phi’, ‘instances_outside_measure_regions’, ‘max_num_iterations’, ‘measure_distance’, ‘measure_interpolation’, ‘measure_select’, ‘measure_transition’, ‘min_score’, ‘num_instances’, ‘num_measures’, ‘point_order’, ‘rand_seed’, ‘start_phi’;
*    GenParamValue:输入参数值;参考:1, 2, 3, 4, 5, 10, 20, ‘all’, ‘true’, ‘false’, ‘first’, ‘last’, ‘positive’, ‘negative’, ‘uniform’, ‘nearest_neighbor’, ‘bilinear’, ‘bicubic’;
*    Index:输出创建测量对象的索引值;
add_metrology_object_generic (MetrologyHandle, 'line', LineParam, 20, 5, 1, 30, [], [], Index)
 
*为测量模型的测量对象设置参数 set_metrology_object_param
*根据需求设置测量的参数
*设置找线的方式(positive,negative,all)从黑到白,从白到黑
*算子参数:
*    MeasureHandle:输入测量模型的句柄;
*    Index:指定测量对象,为all时所有计量对象的参数都被设置(‘all’);
*    GenParamName:输入参数名称;默认:‘num_instances’,参考:
*            'measure_length1':区域,垂直于边界的测量区域的一半长度
*            'measure_length2':区域,相切于边界的测量区域的一半长度
*            'measure_distance':区域,两个测量区域中心之间的距离
*            'num_measures':区域,测量区域数
*            'measure_sigma':测量,用于平滑的高斯函数的 Sigma
*            'measure_threshold':测量,最小边缘幅度阈值
*            'measure_select':测量,边缘端点的选择('last''first')
*            'measure_transition':测量,方向('all''negative''positive')
*            'measure_interpolation':测量,插值类型
*            'min_score':拟合,最小分数
*            'num_instances':拟合,成功拟合实例的最大数量
*            'distance_threshold':拟合,距离阈值 
 
set_metrology_object_param (MetrologyHandle, 'all', 'measure_transition', 'all')
 
*预期测量的区域个数
set_metrology_object_param (MetrologyHandle, 'all', 'num_instances', 12)
 
*拟合数
set_metrology_object_param (MetrologyHandle, 'all', 'num_instances', 6)
 
*高斯平滑系数,值越大,唯一的边缘越清晰
set_metrology_object_param (MetrologyHandle, 'all', 'measure_sigma', 5)
 
*区域,垂直与边界的测量区域的一半长度
set_metrology_object_param (MetrologyHandle, 'all', 'measure_length1', 80)
 
*区域,相切于边缘的测量区域的一半长度
set_metrology_object_param(MetrologyHandle, 'all', 'measure_length2', 10)
 
*最小边缘幅度越大,要求找到的边缘越锐利(灰度变换明显),反而不容易找到边缘
set_metrology_object_param (MetrologyHandle, 'all', 'measure_threshold', 20)
 
*测量双立方插入值,区别与bilinear双线性
set_metrology_object_param (MetrologyHandle, 'all', 'measure_interpolation', 'bicubic')
 
*取值 all,first,best,last
set_metrology_object_param (MetrologyHandle, 'all', 'measure_select', 'all')
 
*设置最小分数0.7
set_metrology_object_param (MetrologyHandle, 'all', 'min_score', 0.7)
 
*开始找边缘
apply_metrology_model (Image, MetrologyHandle)
 
*获取测量区域找到的边缘坐标集合
get_metrology_object_measures (Contours, MetrologyHandle, 'all', 'all', Row, Column)
 
*把找到的点通过十字叉来显示
gen_cross_contour_xld (Cross, Row, Column, 6, 0.785398)
 
*得到线的起点坐标并显示出来
get_metrology_object_result (MetrologyHandle, 0, 'all', 'result_type', 'all_param', Parameter)
 
*获取计量模型的计量结果轮廓
get_metrology_object_result_contour (Contour, MetrologyHandle, 0, 'all', 1.5)
*取第一条边放到ObjectSelected变量中
select_obj (Contour, ObjectSelected, 1)
*取第二条边放到ObjectSelected1变量中
select_obj (Contour, ObjectSelected1, 2)
 
*测量两个轮廓之间的距离
distance_cc (ObjectSelected, ObjectSelected1, 'point_to_point', DistanceMin, DistanceMax)
disp_message (WindowHandle, 'length:'+DistanceMin, 'window', 20, 0, 'green', 'true')
*清除句柄
clear_metrology_model (MetrologyHandle)

结果

 


部分参数说明
*line 直线工具
*添加直线测量对象到模型
*函数参数:
*    MeasureHandle:输入测量模型的句柄;
*    RowBegin:输入测量区域行坐标起点;(测量直线的第一个点X坐标)
*    ColumnBegin:输入测量区域列坐标起点;(测量直线的第一个点Y坐标)
*    RowEnd:输入测量区域行坐标终点;(测量直线的第二个点X坐标)
*    ColumnEnd:输入测量区域列坐标终点;(测量直线的第二个点Y坐标)
*    MeasureLength1:输入垂直于边界的测量区域的一半长度;默认值:20,参考值:10,20,30;
*    MeasureLength2:输入与边界相切的测量区域的一半长度;默认值:5,参考值:3,5,10;
*    MeasureSigma:输入用于平滑的高斯函数的sigma;默认值:1,参考值:0.4,0.6,0.8,1.0,1.5,2.0,3.0,4.0,5.0,7.0,10.0;
*    MeasureThreshold:输入测量阈值;默认值:30,参考值:5.0, 10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 90.0, 110.0;
*    GenParamName:输入参数名称;参考: ‘distance_threshold’, ‘end_phi’, ‘instances_outside_measure_regions’, ‘max_num_iterations’, ‘measure_distance’, ‘measure_interpolation’, ‘measure_select’, ‘measure_transition’, ‘min_score’, ‘num_instances’, ‘num_measures’, ‘point_order’, ‘rand_seed’, ‘start_phi’;
*    GenParamValue:输入参数值;参考:1, 2, 3, 4, 5, 10, 20, ‘all’, ‘true’, ‘false’, ‘first’, ‘last’, ‘positive’, ‘negative’, ‘uniform’, ‘nearest_neighbor’, ‘bilinear’, ‘bicubic’;
*    Index:输出创建测量对象的索引值;
add_metrology_object_line_measure (MetrologyHandle, Row1, Column1, Row2, Column2, 20, 5, 1, 30, [], [], Index)

————————————————

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原文链接:https://blog.csdn.net/cjh16606260986/article/details/136291046

posted @ 2024-06-20 19:17  baivfhpwxf  阅读(426)  评论(0编辑  收藏  举报