1. 程式人生 > 其它 >29.3、形態學---其他例項

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)