29.3、形态学---其他实例

开运算:当目标物体区域无法容纳给定的开运算结的构元素时,在凸角点或毛刺和细小区域就会被祛除,可以将两个

        物体(交接部分较小无法容纳给定的结构元素)分开。

闭运算:闭运算是用来填充区域的凹角或沟壑和空洞,链接邻近的区域。

1:闭运算(膨胀)--链接邻近区域

read_image (Snap3, 'C:/Snap3.bmp')

threshold (Snap3, Regions, 0, 102)

connection (Regions, ConnectedRegions)

*结果如下图,缺点:ij字母有两部分组成,connection把它们分开了

*膨胀

dilation_circle (Regions, RegionDilation,4)

connection (RegionDilation, ConnectedRegions1)

*此处的膨胀也可以用闭运算来实现

* draw_rectangle1 (3600, Row1, Column1, Row2, Column2)

* gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2)

* closing (Regions, Rectangle, RegionClosing)

 

 

*取交集,结果连通性跟第一个输入参数保持一致

intersection (ConnectedRegions1,ConnectedRegions , RegionIntersection)

例子2:闭运算----链接邻近区域(计算圆心距)

read_image (Rigid, 'F:/Halcon学习/4、群共享资料下载/测量圆孔间距离/孔心距测量.png')

*分割小圆

threshold (Rigid, Regions1, 10, 255)

connection (Regions1, ConnectedRegions2)

select_shape (ConnectedRegions2, SelectedRegions, 'area', 'and', 100, 31212)

***************region分为三部分***************************************

union1 (SelectedRegions, RegionUnion)

*获得一个合理的结构元素

dev_display (Rigid)

bin_threshold (Rigid, Region)

fill_up (Region, RegionFillUp)

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

gen_rectangle2 (Rectangle, Row, Column, Phi, Length1, 1)

*进行开运算

closing (RegionUnion, Rectangle, RegionClosing)

*注意:这里不适合用膨胀来使它们连接起来,因为会超出左右两端

* 也不适合把闭运算分开即先膨胀再腐蚀

* dilation1 (RegionUnion, Rectangle, RegionDilation1, 1)

* erosion1 (RegionDilation1, Rectangle, RegionErosion, 1)

connection (RegionClosing, ConnectedRegions)

***************计算每部分的圆心距离***************************************

count_obj (ConnectedRegions, Number)

dev_display (Rigid)

for i:=1 to Number by 1

*选中第i部分

select_obj (ConnectedRegions, ObjectSelected, i)

*做差集

intersection (SelectedRegions, ObjectSelected, ConnectedRegions1)

sort_region (ConnectedRegions1, SortedRegions, 'first_point', 'true', 'column')

area_center (SortedRegions, Area, Row1, Column1)

for j:=0 to |Row1|-2 by 1

distance_pp (Row1[j], Column1[j], Row1[j+1], Column1[j+1], Distance1)

if(j%2=1)

disp_message (3600, Distance1, 'image', Row1[j]+10, Column1[j], 'red', 'false')

else

disp_message (3600, Distance1, 'image', Row1[j]-20, Column1[j], 'red', 'false')

endif

endfor

    

 

endfor

例子3:闭运算----填补凹角或空洞

read_image (Snap4, 'F:/Halcon学习/7、测试图片/Snap4.bmp')

*阈值分割

threshold (Snap4, Regions, 153, 255)

*闭运算填补空洞或沟壑

closing_circle (Regions, RegionClosing,400)

*闭运算前后做差集,求得沟壑或空洞大小

difference (RegionClosing,Regions , RegionDifference)

 

 

例子4:开运算---去除毛刺和凸角(查找相同物体)

read_image (Snap2, 'F:/Halcon学习/7、测试图片/Snap2.bmp')

rgb1_to_gray (Snap2, GrayImage)

*阈值分割某单个物体,作为开运算的结构元素

draw_region (Region, 3600)

reduce_domain (GrayImage, Region, ImageReduced)

threshold (ImageReduced, struc, 0, 4)

*阈值分割整个图像

threshold (GrayImage, Regions, 0, 111)

*开运算

opening (Regions, struc, RegionOpening)

例子5:开运算---去除毛刺和凸角

物体的大小比开运算的结构元素的大小 的时候,这些物体会被去除。并结果形状趋向于结构元素

 

read_image (Bond, 'die/die_03')

*设置ROI,减少处理图像的幅度

threshold (Bond, Bright, 100, 255)

shape_trans (Bright, Die, 'rectangle2')

reduce_domain (Bond, Die, DieGrey)

threshold (DieGrey, Wires, 0, 50)

*填补空洞

fill_up_shape (Wires, WiresFilled, 'area', 1, 100)

 

*开运算

opening_circle (WiresFilled, Balls, 15.5)

*当用矩形结构元素做开运算时候

opening_rectangle1 (WiresFilled, RegionOpening, 17, 17)

 

 

posted @ 2022-05-09 20:34  ihh2021  阅读(111)  评论(0编辑  收藏  举报