Halcon的2d计量模型能自动测量圆的半径,矩形的长宽,椭圆和直线。2d测量模型使用起来很方便,比如我们要测圆的半径,只要设置圆心坐标和模板半径,之后就能在模板半径内设置个测量范围,在测量范围内的边缘会被提取出来,从而自动测出圆的实际半径。如下图,以模板半径为中心,画出上下波动的测量范围,波动的大小可以自己设置,即图中的彩色矩形构成的圆环区域,放大图片可以看见测量范围内的边缘已经自动用红色线提取出来,调用算子即可取得测量的半径结果。
测量矩形的宽度和高度也是相同的道理,设置矩形的中心点的坐标和模板长度和高度,之后就能在模板长度和宽度内设置个测量范围,在测量范围内的边缘会被提取出来,从而自动测出矩形的实际长度和宽度。如下图,以模板长度和宽度为中心,画出上下波动的测量范围,即图中的彩色矩形构成的区域,放大图片可以看见测量范围内的边缘已经自动用红色线提取出来,调用算子即可取得测量的结果。
关于如何设置模板半径,模板长度宽度,笔者的做法是用halcon的measure助手测出合格品的半径等设置为模板半径。
第一步、创建计量模型的算子create_metrology_model (MetrologyHandle),后续的使用都是围绕着句柄MetrologyHandle。
第二步、设置计量对象图像的大小set_metrology_model_image_size,参数设置为图像的长和宽。
第三步、
①如果要测量圆或圆弧,调用算子add_metrology_object_circle_measure( : : MetrologyHandle, Row, Column, Radius, MeasureLength1, MeasureLength2, MeasureSigma, MeasureThreshold, GenParamName, GenParamValue : Index),第一个参数是计量模型的句柄,第二和第三个参数是待测圆的圆心坐标。这里可以用Blob分析法选中待测圆,area_center求得圆心坐标。第四个参数是待测圆的半径,这个参数至关重要,之后的测量范围就是在这个半径上波动的。可以通过测量助手获得。第五个和第六个参数是测量矩形的高度和宽度, MeasureLength1越大,检测的范围就越大;MeasureLength2越大,范围内的测量矩形就越密。
②如果要测量椭圆,调用算子add_metrology_object_rectangle2_measure( : : MetrologyHandle, Row, Column, Phi, Length1, Length2, MeasureLength1, MeasureLength2, MeasureSigma, MeasureThreshold, GenParamName, GenParamValue : Index),第一个参数是计量模型的句柄,第二和第三个参数是待测矩形的中心点坐标。可以用Blob分析法选中待测矩形,area_center求得中心点坐标。第四个参数是矩形的角度。同样Blob分析法后orientation_region求得角度。第五和第六个参数是模板长度和宽度。注意这里的参数是模板长度的一半,故传入的参数应为“模板长度/2”和“模板宽度/2”。第七个和第八个参数是测量矩形的高度和宽度, MeasureLength1越大,检测的范围就越大;MeasureLength2越大,范围内的测量矩形就越密。
③如果要测量椭圆,调用算子add_metrology_object_ellipse_measure。
④如果要测量直线,调用算子add_metrology_object_line_measure
⑤也可以用add_metrology_object_generic( : : MetrologyHandle, Shape, ShapeParam, MeasureLength1, MeasureLength2, MeasureSigma, MeasureThreshold, GenParamName, GenParamValue : Index),在第二个参数里设置设置形状,很遗憾,能选择的形状只有’circle’, ‘ellipse’, ‘line’, 'rectangle2’这四种。所以我也不知道这算子有啥子用。。。
可以往一个句柄里添加多个测量模型,测量时就能同时测得多组数据。
第四步、显示
显示圆轮廓的测量范围get_metrology_object_measures
显示圆的边缘轮廓用算子get_metrology_object_model_contour
第五步,测量
apply_metrology_model(Image : : MetrologyHandle : ),第一个参数输入的图像,第二个参数输入的句柄。
第六步、取得结果
get_metrology_object_result倒数第二个参数GenParamValue设置我们需要取得的结果,有 ‘all_param’, ‘score’, ‘true’, ‘false’, ‘row’, ‘column’, ‘amplitude’, ‘radius’, ‘phi’, ‘radius1’, ‘radius2’, ‘length1’, ‘length2’, ‘row_begin’, ‘column_begin’, ‘row_end’, ‘column_end’, ‘nrow’, ‘ncolumn’, ‘distance’, ‘x’, ‘y’, ‘x_begin’, ‘y_begin’, ‘x_end’, ‘y_end’, ‘nx’, ‘ny’, ‘positive’, ‘negative’。
第七步、清空计量模型
clear_metrology_model (MetrologyHandle)
代码如下:
* Image Acquisition 01: Code generated by Image Acquisition 01
read_image (Image, 'C:/Users/liu/Desktop/测量/1.jpg')
get_image_size (Image, Width, Height)
dev_close_window ()
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
rgb1_to_gray (Image, GrayImage)
*通过测量助手,大概确定模板半径,模板长度,模板宽度
RecLength:=230/2
Recwidth:=120/2
circleraduis:=125
*读入图像路径
ImagePath := []
ImagePath[0] := 'C:/Users/liu/Desktop/测量/1.jpg'
ImagePath[1] := 'C:/Users/liu/Desktop/测量/2.jpg'
ImagePath[2] := 'C:/Users/liu/Desktop/测量/3.jpg'
ImagePath[3] := 'C:/Users/liu/Desktop/测量/4.jpg'
ImagePath[4] := 'C:/Users/liu/Desktop/测量/5.jpg'
ImagePath[5] := 'C:/Users/liu/Desktop/测量/6.jpg'
ImagePath[6] := 'C:/Users/liu/Desktop/测量/7.jpg'
for i := 0 to 6 by 1
read_image (Image, ImagePath[i])
rgb1_to_gray (Image, GrayImage)
*选中待测量的圆,求圆心坐标
threshold (GrayImage, Regions1, 0, 165)
fill_up (Regions1, RegionFillUp1)
connection (RegionFillUp1, ConnectedRegions1)
select_shape (ConnectedRegions1, SelectedRegions1, ['area','circularity'], 'and', [30496.5,0.8], [156738,1])
area_center (SelectedRegions1, Area, Row, Column)
*选中待测量的矩形,求矩形中心坐标和角度
threshold (GrayImage, Regions, 0, 5)
connection (Regions, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 18829.8, 29468.1)
area_center (SelectedRegions, Area1, Row2, Column2)
orientation_region (SelectedRegions, Phi)
*计算图中矩形的数量
count_obj (SelectedRegions, RecNumber)
*计算图中圆的数量
count_obj (SelectedRegions1, Number)
*创建计量模型
create_metrology_model (MetrologyHandle)
*设置计量模型的尺寸
set_metrology_model_image_size (MetrologyHandle, Width, Height)
dev_display (Image)
*只有一个圆
if(Number=1)
*添加圆的坐标和模板半径
add_metrology_object_circle_measure (MetrologyHandle, Row, Column, circleraduis, 30, 5, 1, 30, [], [], Index)
*显示圆的边缘轮廓
get_metrology_object_model_contour (Contour, MetrologyHandle, Index, 1.5)
*显示圆轮廓的测量范围
get_metrology_object_measures (Contours, MetrologyHandle, 'all', 'all', Row1, Column1)
*计算
apply_metrology_model (GrayImage, MetrologyHandle)
get_metrology_object_result_contour (Contour2, MetrologyHandle, 0, 'all', 1.5)
*得到半径
get_metrology_object_result (MetrologyHandle, Index, 'all', 'result_type', 'radius', Parameter)
*显示结果
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
disp_message (WindowHandle, '半径:'+Parameter, 'window', Row-480, Column-310, 'red', 'true')
endif
*有两个圆
if(Number=2)
*要设置两个半径
circleraduis2:=[circleraduis,circleraduis]
add_metrology_object_circle_measure (MetrologyHandle, Row, Column,circleraduis2, 30, 5, 1, 30, [], [], Index)
get_metrology_object_model_contour (Contour, MetrologyHandle, Index, 1.5)
get_metrology_object_measures (Contours, MetrologyHandle, 'all', 'all', Row1, Column1)
apply_metrology_model (GrayImage, MetrologyHandle)
get_metrology_object_result (MetrologyHandle, Index, 'all', 'result_type', 'radius', Parameter)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
disp_message (WindowHandle, '半径:'+Parameter[0], 'window', Row[0]-330, Column[0]-400, 'red', 'true')
disp_message (WindowHandle, '半径:'+Parameter[1], 'window', Row[1]-540, Column[1]-250, 'red', 'true')
endif
*只有一个矩形
if(RecNumber=1)
*添加矩形的坐标和角度,大致的测量范围
dev_display (GrayImage)
add_metrology_object_rectangle2_measure (MetrologyHandle, Row2, Column2, Phi, RecLength, Recwidth, 20, 5, 1, 30, [], [], Index1)
*显示矩形的边缘轮廓
get_metrology_object_model_contour (Contour1, MetrologyHandle, Index1, 1.5)
*得到矩形的测量范围
get_metrology_object_measures (Contours1, MetrologyHandle, 'all', 'all', Row3, Column3)
*计算
apply_metrology_model (GrayImage, MetrologyHandle)
get_metrology_object_result_contour (Contour3, MetrologyHandle, 0, 'all', 1.5)
*得到矩形的长和宽
get_metrology_object_result (MetrologyHandle, Index1, 'all', 'result_type', 'length1', Parameter11)
get_metrology_object_result (MetrologyHandle, Index1, 'all', 'result_type', 'length2', Parameter12)
*显示
disp_message (WindowHandle, '长度:'+2*Parameter11+' '+'宽度:'+Parameter12, 'window', Row2-300, Column2-300, 'red', 'true')
endif
*有两个矩形
if(RecNumber=2)
*设置两个大致的长的宽
RecLength2:=[RecLength,RecLength]
Recwidth2:=[Recwidth,Recwidth]
add_metrology_object_rectangle2_measure (MetrologyHandle, Row2, Column2, Phi, RecLength2, Recwidth2, 20, 5, 1, 30, [], [], Index1)
get_metrology_object_model_contour (Contour1, MetrologyHandle, Index1, 1.5)
get_metrology_object_measures (Contours1, MetrologyHandle, 'all', 'all', Row3, Column3)
apply_metrology_model (GrayImage, MetrologyHandle)
get_metrology_object_result (MetrologyHandle, Index1, 'all', 'result_type', 'length1', Parameter11)
get_metrology_object_result (MetrologyHandle, Index1, 'all', 'result_type', 'length2', Parameter12)
disp_message (WindowHandle, '长度:'+2*Parameter11[0]+' '+'宽度:'+Parameter12[1], 'window', Row2[0]-300, Column2[0]-300, 'red', 'true')
disp_message (WindowHandle, '长度:'+2*Parameter11[1]+' '+'宽度:'+Parameter12[1], 'window', Row2[1]-500, Column2[1]-650, 'red', 'true')
endif
*有三个圆
if(RecNumber=3)
*设置三个大致的长和宽
RecLength3:=[RecLength,RecLength,RecLength]
Recwidth3:=[Recwidth,Recwidth,Recwidth]
add_metrology_object_rectangle2_measure (MetrologyHandle, Row2, Column2, Phi, RecLength3, Recwidth3, 20, 5, 1, 30, [], [], Index1)
get_metrology_object_model_contour (Contour1, MetrologyHandle, Index1, 1.5)
get_metrology_object_measures (Contours1, MetrologyHandle, 'all', 'all', Row3, Column3)
apply_metrology_model (GrayImage, MetrologyHandle)
get_metrology_object_result (MetrologyHandle, Index1, 'all', 'result_type', 'length1', Parameter11)
get_metrology_object_result (MetrologyHandle, Index1, 'all', 'result_type', 'length2', Parameter12)
disp_message (WindowHandle, '长度:'+2*Parameter11[0]+' '+'宽度:'+Parameter12[1], 'window', Row2[0]-400, Column2[0]-200, 'red', 'true')
disp_message (WindowHandle, '长度:'+2*Parameter11[1]+' '+'宽度:'+Parameter12[1], 'window', Row2[1]-450, Column2[1]-550, 'red', 'true')
disp_message (WindowHandle, '长度:'+2*Parameter11[2]+' '+'宽度:'+Parameter12[2], 'window', Row2[2]-700, Column2[2]-500, 'red', 'true')
endif
*清空计量模型
clear_metrology_model (MetrologyHandle)
stop()
endfor
图片链接:https://pan.baidu.com/s/1Osz8x04omJEMu2eiYuHvzw
提取码:d00z
复制这段内容后打开百度网盘手机App,操作更方便哦
原创不易,点个关注,点个赞|•’-’•)و✧,后续我会继续更新机器视觉与运动控制的文章。
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_45617323/article/details/111596745