*添加测量模型对象
*将测量对象添加到测量模型中
*算子参数:
* MeasureHandle:输入测量模型的句柄;
* Shape:输入要测量对象的类型;默认值:‘circle’,参考值:‘circle’圆,‘ellipse’椭圆,‘line’线,‘rectangle2’矩形,长方形;
* ShapeParam:要添加的测量对象的参数。
* MeasureLength1:输入垂直于边界的测量区域的一半长度(卡尺长度);默认值:20,参考值:10,20,30;最小增量:1.0;建议增量:10.0
* MeasureLength2:输入与边界相切的测量区域的一半长度(卡尺宽度);默认值:5,参考值:3,5,10;最小增量:1.0;建议增量:10.0
* 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;最小增量: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, 300, 5, 1, 30, [], [], Index)
示例 找两条直线,并计算两条线的夹角和两个线的总长度,转换成毫米单位
效果
代码
dev_get_window (WindowHandle)
*定义一个像素代表的实际物理尺寸
hv_mm_Per_Pixel := 0.014128
*读取图像
read_image (Image, 'E:/机器视觉学习/海康视觉平台/二期VM视觉学习/二期VM视觉学习/机器视觉程序/侧视相机检查程序Update-0325/侧视相机检查程序/20240324165707.bmp')
*获取图像大小
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width/2, Height/2, 'black', WindowHandle)
rgb1_to_gray (Image, GrayImage)
*生成ROI区域
draw_rectangle1 (WindowHandle, rectangleRow, rectangleColumn, rectangleRow2, rectangleColumn2)
*生成矩形区域
gen_rectangle1 (Rectangle, rectangleRow, rectangleColumn, rectangleRow2, rectangleColumn2)
*缩小图像的域(ROI) Image
reduce_domain (GrayImage, Rectangle, ImageReduced)
*做模板,提取模板轮廓
create_scaled_shape_model (ImageReduced, 'auto', rad(0), rad(360), 'auto', 0.3, 1.1, 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID)
*根据模板去匹配(查找)图像
find_scaled_shape_model (ImageReduced, ModelID, rad(0), rad(360), 0.9, 1.1, 0.5, 1, 0.5, 'least_squares', 0, 0.9, Row3, Column3, Angle1, Scale, Score)
*显示匹配(查找)到的图像
dev_display_shape_matching_results (ModelID, 'red', Row3, Column3, Angle1, 1, 1, 0)
*设置显示的字体
set_display_font (WindowHandle, 26, 'mono', 'true', 'false')
disp_message (WindowHandle, '匹配坐标:'+Row3+','+Column3, 'window', 20, 20, 'red', 'true')
*显示十字中心
disp_cross (WindowHandle, Row3, Column3, 50, Angle1)
*方式一绘制测量位置直线
*draw_line (WindowHandle, Row1, Column1, Row2, Column2)
*方式二 固定测量位置直线坐标
*919
Row1 := Row3 + 200
*187
Column1 := 187
*显示十字中心
disp_cross (WindowHandle, Row3, Column1, 50, Angle1)
Row2 := Row1
Column2 := Column3-100
disp_cross (WindowHandle, Row2, Column2, 50, Angle1)
*根据绘制的直线坐标来构建参数对象
LineParam := [Row1, Column1,Row2, Column2]
*行1:846.5,列1:1710.5
*行2:1068.5,列2:1992.5
*RowR1 := Row1+10.5
*-100.5
RowR1 := Row1+15
*Column2
ColumnR1 := Column2 + 100
disp_cross (WindowHandle, RowR1, ColumnR1, 50, Angle1+10)
RowR2 := RowR1
ColumnR2 := ColumnR1 + 200
disp_cross (WindowHandle, RowR2, ColumnR2, 50, Angle1+10)
*根据绘制的直线坐标来构建参数对象
LineParam2 := [RowR1, ColumnR1,RowR2, ColumnR2]
*绘制测量位置直线 时把直线的坐标写入文本文件,以变查看。这样就可以知道绘制的坐标。
*实际测量时是不可能让人,手动的去绘制每张要测量的图片的
*open_file ('LineParam.txt', 'output', FileHandle)
fileStr := '绘制线的坐标Row1:'+Row1+',Column1:'+Column1+',Row2:'+Row2+',Column2:'+Column2
*fwrite_string (FileHandle, fileStr)
*设置显示的字体
set_display_font (WindowHandle, 20, 'mono', 'true', 'false')
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,参考值:10,20,30;最小增量:1.0;建议增量:10.0
* MeasureLength2:输入与边界相切的测量区域的一半长度(卡尺宽度);默认值:5,参考值:3,5,10;最小增量:1.0;建议增量:10.0
* 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;最小增量: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, 300, 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', 'measure_transition', 'positive')
*预期测量的区域个数。 拟合,成功拟合实例的最大数量
*set_metrology_object_param (MetrologyHandle, 'all', 'num_instances', 12)
set_metrology_object_param (MetrologyHandle, 'all', 'num_instances', 1)
*拟合数
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)
*获取计量模型的计量结果轮廓
*创建一个变化模型用于图像比较
*在图像坐标中执行的测量结果轮廓。
*参数说明
*Contour (输出参数)给定计量对象的结果轮廓。
*MetrologyHandle (输入参数)计量模型的句柄
*Index (输入参数)计量对象的索引。建议值:'all', 0,1,2
*Instance 计量对象的实例。建议值:'all', 0,1,2
*Resolution 相邻轮廓点之间的距离 默认值: 1.5
get_metrology_object_result_contour (Contour, MetrologyHandle, 0, 'all', 1.5)
*取第一条边放到ObjectSelected变量中
*函数可以用于根据对象的特征,从一组对象中选择满足条件的对象。
*参数说明
*Objects 输入对象
*ObjectSelected (输出参数)选定的对象
*Index (输入参数)待选择对象的索引
select_obj (Contour, ObjectSelected, 1)
*显示线的轮廓
ContourLen := |Contour|
dev_set_color ('red')
dev_set_line_width(3)
dev_display (Contour)
*获取轮廓对象长度
length_xld (ObjectSelected, Length)
*设置显示的字体
set_display_font (WindowHandle, 26, 'mono', 'true', 'false')
disp_message (WindowHandle, '线的轮廓长度:'+Length, 'window', 40, 0, 'green', 'true')
*取第二条边放到ObjectSelected1变量中
select_obj (Contour, ObjectSelected1, 2)
*get_region_polygon (ObjectSelected1, 5, Rows, Columns)
*设置显示的字体
set_display_font (WindowHandle, 26, 'mono', 'true', 'false')
*获取轮廓对象长度
length_xld (ObjectSelected1, Length1)
disp_message (WindowHandle, '线的轮廓长度1:'+Length1, 'window', 120, 0, 'green', 'true')
*测量两个轮廓之间的距离
*//参数1:第一个轮廓
*//参数2:第二个输入轮廓
*//参数3:距离计算模式
*'point_to_point' 确定轮廓线之间的最小和最大距离
*'point_to_segment' 轮廓线段之间的实际最小距离
*//参数4:返回两个轮廓之间的最小距离
*//参数5:两个轮廓之间的最大距离
*distance_cc (ObjectSelected, ObjectSelected1, 'point_to_point', DistanceMin, DistanceMax)
*disp_message (WindowHandle, 'length:'+DistanceMin, 'window', 200, 0, 'green', 'true')
********************************************
*找左边直线
*创建计算测量句柄
create_metrology_model (MetrologyHandleRight)
*句柄关联测试的图坐标位置
*设置测量对象图像的大小
*参数:
* MeasureHandle:输入测量模型的句柄;
* Width:输入图像宽;
* Height:输入图像高;
set_metrology_model_image_size (MetrologyHandleRight, Width, Height)
*添加测量模型对象(此处是线)
*将测量对象添加到测量模型中
*算子参数:
* MeasureHandle:输入测量模型的句柄;
* Shape:输入要测量对象的类型;默认值:‘circle’,参考值:‘circle’圆,‘ellipse’椭圆,‘line’线,‘rectangle2’矩形,长方形;
* ShapeParam:要添加的测量对象的参数。
* MeasureLength1:输入垂直于边界的测量区域的一半长度(卡尺长度);默认值:20,参考值:10,20,30;最小增量:1.0;建议增量:10.0
* MeasureLength2:输入与边界相切的测量区域的一半长度(卡尺宽度);默认值:5,参考值:3,5,10;最小增量:1.0;建议增量:10.0
* 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;最小增量: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 (MetrologyHandleRight, 'line', LineParam2, 50, 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 (MetrologyHandleRight, 'all', 'measure_transition', 'all')
set_metrology_object_param (MetrologyHandleRight, 'all', 'measure_transition', 'positive')
*预期测量的区域个数
set_metrology_object_param (MetrologyHandleRight, 'all', 'num_instances', 1)
*拟合数
set_metrology_object_param (MetrologyHandle, 'all', 'num_instances', 6)
*高斯平滑系数,值越大,唯一的边缘越清晰
set_metrology_object_param (MetrologyHandleRight, 'all', 'measure_sigma', 5)
*区域,垂直与边界的测量区域的一半长度(卡尺长度)
*set_metrology_object_param (MetrologyHandleRight, 'all', 'measure_length1', 80)
*区域,相切于边缘的测量区域的一半长度(卡尺宽度)
*set_metrology_object_param(MetrologyHandleRight, 'all', 'measure_length2', 10)
*最小边缘幅度越大,要求找到的边缘越锐利(灰度变换明显),反而不容易找到边缘
set_metrology_object_param (MetrologyHandleRight, 'all', 'measure_threshold', 20)
*测量双立方插入值,区别与bilinear双线性
set_metrology_object_param (MetrologyHandleRight, 'all', 'measure_interpolation', 'bicubic')
*取值 all,first,best,last
set_metrology_object_param (MetrologyHandleRight, 'all', 'measure_select', 'all')
*设置最小分数0.7
set_metrology_object_param (MetrologyHandleRight, 'all', 'min_score', 0.7)
*开始找边缘
apply_metrology_model (Image, MetrologyHandleRight)
*获取测量区域找到的边缘坐标集合
get_metrology_object_measures (ContoursRight, MetrologyHandleRight, 'all', 'all', RowRight, ColumnRight)
*把找到的点通过十字叉来显示
gen_cross_contour_xld (CrossRight, RowRight, ColumnRight, 6, 0.785398)
*得到线的起点坐标并显示出来
get_metrology_object_result (MetrologyHandleRight, 0, 'all', 'result_type', 'all_param', Parameter)
*获取计量模型的计量结果轮廓
get_metrology_object_result_contour (ContourRight, MetrologyHandleRight, 0, 'all', 1.5)
*显示线的轮廓
dev_set_color ('red')
dev_set_line_width(3)
dev_display (ContourRight)
*清除显示
*dev_clear_window ()
lastRowIndex := |Row|-1
lastColumIndex := |Column|-1
lastRowIndexRight := |RowRight|-1
lastColumIndexRight := |ColumnRight|-1
*显示第一条线
LineWidth := 3
LineHeight := 2
* 使用min_vector获取数组中的最小值
*tuple_max(Row,Max)
*2、求数组元素的最小值
tuple_min(Row,RowMin)
* 生成直线
*生成直线区域
*参数1:起点y坐标
*参数2:起点x坐标
*参数3:终点y坐标
*参数4:终点x坐标
*draw_line (WindowHandle, Row[0], Column[0], Row[lastRowIndex], Column[lastColumIndex])
*gen_rectangle1 (Rectangle, Row[0], Column[0], Row[lastRowIndex], Column[lastColumIndex])
gen_region_line (RegionLines1, RowMin, Column[0], Row[lastRowIndex], Column[lastColumIndex])
dev_set_color ('green')
dev_set_line_width(3)
dev_display(RegionLines1)
* 生成直线2
gen_region_line (RegionLines2, RowRight[0], ColumnRight[0], RowRight[lastRowIndexRight], ColumnRight[lastColumIndexRight])
dev_display(RegionLines2)
*计算两段线的夹角
*Row, Column
*参数
*RowA1 (输入参数)第一条直线第一点的行坐标。
*ColumnA1 (输入参数)第一条直线第一个点的列坐标。
*RowA2 (输入参数)第一条直线第二点的行坐标
*ColumnA2(输入参数)第一条直线第二个点的列坐标
*RowB1 (输入参数)第二条直线第一个点的行坐标。
*ColumnB1 (输入参数)第二条直线第一个点的列坐标。
*RowB2 (输入参数)第二条直线第二点的行坐标。
*ColumnB2(输入参数)第二条直线第二点的列坐标。
*Angle (输出参数)线条间的夹角[弧度]。
angle_ll (Row[0], Column[0], Row[lastRowIndex], Column[lastColumIndex], RowRight[0], ColumnRight[0], RowRight[lastRowIndexRight], ColumnRight[lastColumIndexRight], Angle)
*计算总长度
lastRowLen := |Row|
lastColumnLen := |Column|
*两点之间的距离算子
*Row1 点1的行坐标
*Column1 点1的列坐标
*Row2 点2的行坐标
*Column2 点2的列坐标
*Distance 输出的两点之间的距离
distance_pp (RowMin, Column[0], Row[lastRowLen-1], Column[lastColumnLen-1], Distance)
distance_pp (RowRight[0], ColumnRight[0], RowRight[lastRowIndexRight], ColumnRight[lastColumIndexRight], Distance2)
*Dis := Distance + 98 + Distance2
Dis := Distance + 110 + Distance2
disMM := Dis * hv_mm_Per_Pixel
disp_message (WindowHandle, '线的轮廓距离:'+Dis+'px, '+disMM+' mm,角度:'+Angle, 'window', 80, 0, 'green', 'true')
*获取直线角度,计算直线的方向
*前两个是线的起点XY坐标,
*第三,四个是线的终点XY坐标,
*Phi (输出参数)返回线的方向
*line_orientation (Row[0], Column[0], Row[lastRowLen-1], Column[lastColumnLen-1], Phi)
*disp_message (WindowHandle, '线的轮廓方向:'+Phi+',count:'+lastRowLen, 'window', 160, 0, 'green', 'true')
*-0.00942175 NG 向下
*-0.125872 正常 count:319
*-0.125347 正常 count:321
*-0.278258 NG 向上 count:322
*-0.133453 NG 向前 count:226
*-0.0609025 NG 向后 count:317
*清除句柄
clear_metrology_model (MetrologyHandle)
*清除句柄
clear_metrology_model (MetrologyHandleRight)