几何测量是视觉应用中经常用到的需求,前节我们是通过blob分析后,将区域region转换为xld,再进行拟合成几何图形测量。Halcon中也可以使用算子,直接从图片中提取轮廓直接拟合成几何图形,再进行计算几何参数。
当前例程我们结合形态学常用算子,在xld和regions间来回切换,逼近与缩小我们要处理的图像,然后直接提到要测量的轮廓,计算圆弧两点间的距离。
例程使用的是halcon自带的图片,我们最终目的是计算图片上铁件边缘轮廓短边的长度,方法很多,思中也很多,可编写代码多加尝试。
下面是实例,
可以运行查看效果。
dev_close_window ()
read_image(Image,'metal-parts/metal-parts-01')
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
dev_display (Image)
stop()
edges_sub_pix (Image, Edges, 'canny', 1, 40, 80)
gen_region_contour_xld (Edges, Region, 'filled')
count_obj (Region, Number)
union1 (Region, RegionUnion)
erosion_circle (RegionUnion, RegionErosion, 5)
get_domain (Image, Domain)
difference (Domain, RegionErosion, RegionDifference)
reduce_domain (Image, RegionDifference, ImageReduced)
edges_sub_pix (ImageReduced, Edges, 'canny', 1, 40, 80)
*
segment_contours_xld (Edges, ContoursSplit, 'lines_circles', 5, 4, 2)
count_obj (ContoursSplit, Number1)
segment_contours_xld (Edges, ContoursSplit, 'lines_circles', 5, 4, 2)
count_obj (ContoursSplit, Number1)
for i:=1 to Number1 by 1
select_obj (ContoursSplit, ObjectSelected, i)
get_contour_global_attrib_xld (ObjectSelected, 'cont_approx', Attrib)
if(Attrib == -1)
fit_line_contour_xld (ObjectSelected, 'tukey', -1, 0, 5, 2, RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist)
elseif(Attrib == 1)
fit_circle_contour_xld (ObjectSelected, 'algebraic', -1, 0, 0, 3, 2, Row, Column, Radius, StartPhi, EndPhi, PointOrder)
if(Radius >30)
get_points_ellipse (StartPhi, Row, Column, 0, Radius, Radius, RowPoint, ColPoint)
get_points_ellipse (EndPhi, Row, Column, 0, Radius, Radius, RowPoint1, ColPoint1)
gen_circle_contour_xld (ContCircle, Row, Column, Radius, StartPhi, EndPhi, PointOrder, 1)
gen_arrow_contour_xld (Arrow, RowPoint, ColPoint, RowPoint1, ColPoint1, 5, 5)
gen_arrow_contour_xld (Arrow1, RowPoint1, ColPoint1, RowPoint, ColPoint, 5, 5)
dev_display (Image)
dev_display (Arrow)
dev_display (Arrow1)
distance_pp (RowPoint1, ColPoint1, RowPoint, ColPoint, Distance)
dev_disp_text ('长度:'+Distance$'3.2f', 'image', RowPoint, ColPoint, 'black', ['box'], ['false'])
endif
endif
endfor
帖子为下面HALCON视频教程的部分内容,
可以下载教程观看学习。
上位机VC MFC程序开发精典实例大全源码与视频讲解配套下载408例 经历1年的编程与录制点击进入查看
如果您认可,可联系功能定制! 如果您着急,充值会员可直接联系发您资料!
|