Halcon初学者知识 【20】如何实现矩形拟合

发布于:2022-12-21 ⋅ 阅读:(1598) ⋅ 点赞:(1)

一、提要

        如何将图中模糊的矩形给拟合出来?当然,第一步首先是从暗区提取亮区,然后要咋样做才能实现呢?

本文给出参考代码供大家日后慢慢分析。

二、代码示例

​
read_image (Image, 'cin01')
threshold(Image, Region, 128, 255)
opening_rectangle1 (Region, RegionOpening, 9, 9)
connection (RegionOpening, ConnectedRegions)
fill_up (ConnectedRegions, RegionFillUp)
select_shape (RegionFillUp, SelectedRegions, ['rectangularity','area'], 'and', [0.8,700], [1,99999])
smallest_rectangle2 (SelectedRegions, Row, Column, Phi, Length1, Length2)
gen_rectangle2 (Rectangle, Row, Column, Phi, Length1, Length2)
boundary (Rectangle, RegionBorder, 'inner_filled')
dilation_rectangle1 (RegionBorder, RegionDilation, 4, 4)
reduce_domain (Image, RegionDilation, ImageReduced)
edges_sub_pix (ImageReduced, Edges, 'canny', 1.5, 30, 40)
segment_contours_xld (Edges, ContoursSplit, 'lines', 5, 2, 2)
select_contours_xld (ContoursSplit, SelectedContours1, 'contour_length', 10, 99999, -0.5, 0.5)
union_adjacent_contours_xld (SelectedContours1, UnionContours1, 100, 1, 'attr_keep')
fit_rectangle2_contour_xld (UnionContours1, 'tukey', -1, 0, 0, 3, 2, Row1, Column1, Phi1, Length11, Length12, PointOrder1)
gen_rectangle2_contour_xld (Rectangle1, Row1, Column1, Phi1, Length11, Length12)

​

三、关键算子解释

3.1 opening_rectangle1开运算算子

        打开一个带有矩形结构元素的区域。

        opening_rectangle1 在输入区域 Region 上执行一个 Erosion_rectangle1,然后是一个 dilation_rectangle1。矩形结构元素的大小由参数 Width 和 Height 确定。

        与所有开运算的情况一样,保留较大的结构,而像线条或线条这样的小区域积分被淘汰。

1)算子调用格式

        opening_rectangle1 (Region, RegionOpening, 9, 9)

3.2 fill_up 区域填充算子

将区域的空洞填充为实区域。

fill_up (ConnectedRegions, RegionFillUp)

3.3 select_shape区域条件选择

        运算符 select_shape 根据形状选择区域。对于 Regions 中的每个输入区域,计算指示的特征 (Features)。如果每个 (Operation = 'and') 或至少一个 (Operation = 'or') 计算的特征在默认限制 (Min,Max) 区域适应输出(重复)。

比如:

select_shape (RegionFillUp, SelectedRegions, ['rectangularity','area'], 'and', [0.8,700], [1,99999])

选中['rectangularity','area']意义是【类似矩形的,面积】在【0.8-1, 700-99999】范围的矩形。

3.4 smallest_rectangle2

在诸多矩形中,取出最小面积的,一般类似于“非极大值抑制”

 smallest_rectangle2 (SelectedRegions, Row, Column, Phi, Length1, Length2)


3.5 boundary区域内切(外切)

将一个区域的内切区域取出,注意,类似于边缘提取,但不是xdl类型,而是区域。
boundary (Rectangle, RegionBorder, 'inner_filled')


segment_contours_xld (Edges, ContoursSplit, 'lines', 5, 2, 2)

3.6 取长度较大的外轮廓select_contours_xld

(在segment_contours_xld之后)选中长度较大的曲线
select_contours_xld (ContoursSplit, SelectedContours1, 'contour_length', 10, 99999, -0.5, 0.5)

3.7 曲线合并union_adjacent_contours_xld

        将同方向的间断的线段联成更长直线段
union_adjacent_contours_xld (SelectedContours1, UnionContours1, 100, 1, 'attr_keep')
 

3.8 从曲线中挑选最接近矩形的

        该算子将(union_adjacent_contours_xld)连起来的曲线封闭形状进行检测,如果曲线与矩形相似度高,就用矩形代替曲线。

fit_rectangle2_contour_xld (UnionContours1, 'tukey', -1, 0, 0, 3, 2, Row1, Column1, Phi1, Length11, Length12, PointOrder1)
 

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到