1.普通测量项目中,我们可以利用halcon的测量模型,例如add_metrology_object_line_measure。很方便的测量直线,圆,椭圆,矩形等。这些工具都有一个缺点是,需要提前绘制测量位置,然后利用仿射变换跟随,或者在项目中动态的生成测量位置。但是对于异形产品测量时,这些办法就不适应了,如下图形状,就需要根据微积分原理,实时的生成卡尺进行测量。
2.实现代码
dev_close_window ()
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
read_image (Image, 'C:/Users/haoyu.dou/Desktop/圆弧胶.bmp')
decompose3 (Image, R, G, B)
mean_image (R, ImageMean, 255, 255)
dyn_threshold (R, ImageMean, RegionDynThresh, 25, 'light')
opening_circle (RegionDynThresh, RegionOpening, 15)
connection (RegionOpening, ConnectedRegions)
skeleton (ConnectedRegions, Skeleton)
get_region_points (Skeleton, Rows, Columns)
gen_cross_contour_xld (Cross, Rows, Columns, 6, 0.785398)
tuple_sort_index (Columns, Indices)
dev_display (R)
dev_display (Cross)
get_image_size (ImageMean, Width, Height)
gen_empty_obj (Edge_MeasureOut)
gen_empty_obj (Rectangle_MeasureOut)
*积分间距
RecDis:=50
for Index := 0 to |Rows|-RecDis-1 by RecDis
*生成测量卡尺,隔50个像素,计算一小段区域角度
angle_lx (Rows[Indices[Index]], Columns[Indices[Index]], Rows[Indices[Index+RecDis]], Columns[Indices[Index+RecDis]], Angle)
*把每一个卡尺轮廓画出来
gen_rectangle2_contour_xld (Rectangle, (Rows[Indices[Index]]+Rows[Indices[Index+RecDis]])/2, (Columns[Indices[Index]]+Columns[Indices[Index+RecDis]])/2, Angle-rad(90), 150, RecDis/2)
concat_obj (Rectangle_MeasureOut, Rectangle, Rectangle_MeasureOut)
*生成垂直待测区域卡尺
gen_measure_rectangle2 ((Rows[Indices[Index]]+Rows[Indices[Index+RecDis]])/2, (Columns[Indices[Index]]+Columns[Indices[Index+RecDis]])/2, Angle-rad(90), 150, RecDis/2, Width, Height, 'nearest_neighbor', MeasureHandle)
measure_pairs (R, MeasureHandle, 1, 20, 'all', 'all', RowEdgeFirst, ColumnEdgeFirst, AmplitudeFirst, RowEdgeSecond, ColumnEdgeSecond, AmplitudeSecond, IntraDistance, InterDistance)
*获取测量上边缘
Get_MeasureEdge (Edge_MeasureOut, Marker, Edge_MeasureOut, RowEdgeFirst, ColumnEdgeFirst, RecDis/2, Angle-rad(90))
*获取测量下边缘
Get_MeasureEdge (Edge_MeasureOut, Marker, Edge_MeasureOut, RowEdgeSecond, ColumnEdgeSecond, RecDis/2, Angle-rad(90))
endfor
dev_display (R)
dev_set_color ('green')
dev_display (Rectangle_MeasureOut)
dev_set_color ('red')
dev_display (Edge_MeasureOut)