Halcon复习专题-第五章-拟合与测量

本文介绍了Halcon中的拟合技术,包括拟合圆、直线的过程及应用案例。通过边缘提取、轮廓分割与拟合,实现了距离、半径等参数的精确测量。

第五章-拟合

拟合

所有fit开头的算子都是和拟合相关的算子

一般测量时往往是通过blob分析获得区域然后设置测量矩形,或者通过助手直接设置测量矩形,这里提供了一种特殊的方法就是直接把目标进行拟合成几何图形,然后根据几何图形的参数来计算测量距离

拟合流程:

  1. 采集图像,
  2. 预处理,
  3. 边缘提取,
  4. 轮廓联合分割
  5. 拟合,获得几何参数
  6. 根据拟合结果求去目标距离

Halcon的拟合示例在ctrl+E中的方法:边缘提取(亚像素)&几何测量中

通过拟合,你可以获得相应图形的几何表示,更方便求距离,面积,等参数,进而根据标定板或者像素尺寸转换来获得实际测量距离

轮廓联合与分割

示例1:拟合圆

拟合3个圆圈⭕️,提取圆的一段轮廓

拟合结果如图中的弧线
* circles.hdev
* The edges in the image are segmented into lines and circles.
* For the edges that are part of a circle, the circle parameters
* are estimated and the resulting circle is displayed.
read_image (Image, 'double_circle')
* 
* Init window
dev_close_window ()
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
* 
* Segment a region containing the edges
fast_threshold (Image, Region, 0, 120, 7)
// 对区域求边缘
boundary (Region, RegionBorder, 'inner')
//裁剪区域
clip_region_rel (RegionBorder, RegionClipped, 5, 5, 5, 5)
dilation_circle (RegionClipped, RegionDilation, 2.5)
reduce_domain (Image, RegionDilation, ImageReduced)
* 
* In the subdomain of the image containing the edges,
* extract subpixel precise edges.
edges_sub_pix (ImageReduced, Edges, 'canny', 2, 20, 60)
segment_contours_xld (Edges, ContoursSplit, 'lines_circles', 5, 4, 3)
count_obj (ContoursSplit, Number)
dev_display (Image)
dev_set_draw ('margin')
dev_set_color ('white')
dev_update_window ('off')
for I := 1 to Number by 1
    select_obj (ContoursSplit, ObjectSelected, I)
    get_contour_global_attrib_xld (ObjectSelected, 'cont_approx', Attrib)
    * Fit a circle to the line segment that are arcs of a circle
    if (Attrib > 0)
        fit_circle_contour_xld (ObjectSelected, 'ahuber', -1, 2, 0, 3, 2, Row, Column, Radius, StartPhi, EndPhi, PointOrder)
        gen_circle_contour_xld (ContCircle, Row, Column, Radius, 0, rad(360), 'positive', 1.0)
        dev_display (ContCircle)
    endif
endfor
dev_set_colored (12)
dev_set_line_width (3)
dev_display (ContoursSplit)

步骤分析

1. blob分析,获得目标区域

read_image (Image, 'double_circle')
* 
* Init window
dev_close_window ()
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
* 
* Segment a region containing the edges
fast_threshold (Image, Region, 0, 120, 7)
// 对区域求边缘
boundary (Region, RegionBorder, 'inner')
//裁剪区域
clip_region_rel (RegionBorder, RegionClipped, 5, 5, 5, 5)
dilation_circle (RegionClipped, RegionDilation, 2.5)
reduce_domain (Image, RegionDilation, ImageReduced)

算子简介

clip_region_rel(Region输入区域,
				RegionClipped输入裁剪结果,
				Top, 
				Bottom, 
				Left,
				Right)

2. 亚像素边缘提取

edges_sub_pix (ImageReduced目标区域图片,
			     Edges输出亚像素边缘轮廓, 
			     'canny'边缘检测算法, 
				 2, 平滑系数(canny算子平滑系数越大越平滑,其他相反)
				 20, 边缘差分阈值
				 60)

3. 轮廓分割

上述轮廓需要拟合成三个圆,

/segment_contours_xld(Contours输入轮廓,
					 ContoursSplit输出分割的轮廓,
					 Mode分割模式, 即你想要分割成直线lines还是几个圆lines_circles或者椭圆lines_ellipses
					 SmoothCont, 用来平滑轮廓的点数
					 MaxLineDist1, 第一次迭代轮廓到多边形的距离阈值
					 MaxLineDist2第二次迭代轮廓到多边形的距离阈值)
/
segment_contours_xld (Edges, ContoursSplit, 'lines_circles', 5, 4, 3)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值