Halcon 2D测量Metrology找线/圆/矩形/椭圆
通过2D测量,可以获取物体的范围、方向、角度、位置、尺寸和个数等特征。其中,Halcon的2D Metrology模块提供了亚像素级别的卡尺测量功能,可以测量的几何形状包括直线、圆、椭圆、矩形等。对于2D度量,必须提供要测量的对象的位置,方向和几何形状的近似值。在显示对象的图像内,这些近似对象的边界用于定位对象的真实边缘以适应几何形状的参数,使得它们最佳地适合图像数据。测量结果是优化的参数。计量模型用于存储所有必要的信息,例如计量对象的位置和几何形状的初始参数,控制测量的参数以及测量结果。
测量流程:
创建模型:create_metrology_model
设置图像大小:set_metrology_model_image_size
添加测量模型:add_metrology_object_generic
add_metrology_object_rectangle2_measure
add_metrology_object_line_measure
add_metrology_object_circle_measure
add_metrology_object_ellipse_measure
设置对象参数:set_metrology_object_param
设置模型参数:set_metrology_model_param
对齐测量模型:align_metrology_model
应用测量模型:apply_metrology_model
获取结果:get_metrology_object_result
显示结果:get_metrology_object_result_contour、get_metrology_object_measures
代码案例
1):找线
read_image (Image, 'E:/数据/测量程序/找线找点/图片/33.bmp')
dev_set_draw('margin')
dev_set_line_width (1)
get_image_size (Image, Width, Height)
create_metrology_model (MetrologyHandle)
set_metrology_model_image_size (MetrologyHandle, Width, Height)
add_metrology_object_line_measure (MetrologyHandle, 69.1489, 137.685, 69.8938, 395.477,40, 10, 0.4, 20, [], [], Index1)
set_metrology_object_param (MetrologyHandle, 'all', 'measure_transition', 'negative')
set_metrology_object_param (MetrologyHandle, 'all', 'measure_select', 'first')
set_metrology_object_param (MetrologyHandle, 'all', 'num_measures', 8)
dev_set_color ('blue')
apply_metrology_model (Image, MetrologyHandle)
get_metrology_object_measures (Contours, MetrologyHandle, 'all', 'all', Row, Column)
gen_contour_polygon_xld (Contour, Row, Column)
fit_line_contour_xld (Contour, 'tukey', -1, 0, 5, 2, RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist)
gen_contour_polygon_xld (Contourline, [RowBegin, RowEnd], [ColBegin,ColEnd])
2):找圆
create_metrology_model (MetrologyHandle)set_metrology_model_image_size (MetrologyHandle, Width, Height)add_metrology_object_generic (MetrologyHandle, 'circle', [197, 778, 113.146], 30, 10, 0.4, 30, [], [], Index)set_metrology_object_param (MetrologyHandle, 'all', 'measure_transition', 'positive')set_metrology_object_param (MetrologyHandle, 'all', 'measure_select', 'first')set_metrology_object_param (MetrologyHandle, 'all', 'num_measures', 15)dev_set_color ('blue')apply_metrology_model (Image, MetrologyHandle)get_metrology_object_measures (Contours1, MetrologyHandle, 'all', 'all', Row, Column)gen_contour_polygon_xld (Contour1, Row, Column)fit_circle_contour_xld (Contour1, 'algebraic', -1, 0, 0, 3, 2, Row1, Column1, Radius, StartPhi, EndPhi, PointOrder)gen_circle_contour_xld (ContCircle, Row1, Column1, Radius, 0, 6.28, 'positive', 3)
3):找矩形
create_metrology_model (MetrologyHandle)set_metrology_model_image_size (MetrologyHandle, Width, Height)add_metrology_object_generic (MetrologyHandle, 'rectangle2', [408.304, 273.611, rad(-0), 164, 129], 30, 10, 0.4, 20, [], [], Index)set_metrology_object_param (MetrologyHandle, 'all', 'measure_transition', 'positive')set_metrology_object_param (MetrologyHandle, 'all', 'measure_select', 'first')set_metrology_object_param (MetrologyHandle, 'all', 'num_measures', 20)dev_set_color ('blue')apply_metrology_model (Image, MetrologyHandle)get_metrology_object_measures (Contours2, MetrologyHandle, 'all', 'all', Row, Column)gen_contour_polygon_xld (Contour2, Row, Column)fit_rectangle2_contour_xld (Contour2, 'regression', -1, 0, 0, 3, 2, Row2, Column2, Phi, Length1, Length2, PointOrder1)gen_rectangle2_contour_xld (Rectangle, Row2, Column2, Phi, Length1, Length2)
4):找椭圆
create_metrology_model (MetrologyHandle)set_metrology_model_image_size (MetrologyHandle, Width, Height)add_metrology_object_generic (MetrologyHandle, 'ellipse', [551.304, 772.611, rad(-90), 161, 90], 30, 10, 0.4, 20, [], [], Index)set_metrology_object_param (MetrologyHandle, 'all', 'measure_transition', 'positive')set_metrology_object_param (MetrologyHandle, 'all', 'measure_select', 'first')set_metrology_object_param (MetrologyHandle, 'all', 'num_measures', 20)dev_set_color ('blue')apply_metrology_model (Image, MetrologyHandle)get_metrology_object_measures (Contours3, MetrologyHandle, 'all', 'all', Row, Column)gen_contour_polygon_xld (Contour3, Row, Column)fit_ellipse_contour_xld (Contour3, 'fitzgibbon', -1, 0, 0, 200, 3, 2, Row3, Column3, Phi1, Radius1, Radius2, StartPhi1, EndPhi1, PointOrder2)gen_ellipse_contour_xld (ContEllipse, Row3, Column3, Phi1, Radius1, Radius2, 0, 6.28, 'positive', 1.5)