29.1、形態學----綜合例項
目的:根據形態學相關操作,算出圖中邊界粘連一起小石頭的個數以及每個小石頭的面積。
Halcon程式碼:
read_image (Dd, 'C:/dd.JPG')
*設定ROI
draw_region (Region, 3600)
reduce_domain (Dd, Region, ImageReduced)
*轉化灰度圖
rgb1_to_gray (ImageReduced, GrayImage)
*閾值分割
threshold (GrayImage,
*連通性分析
connection (Regions, ConnectedRegions)
*填補空洞
fill_up (ConnectedRegions, RegionFillUp)
*腐蝕使細小連結的region分開
erosion_circle (RegionFillUp, RegionErosion, 35)
*再進行連通性分析
connection (RegionErosion,
*用同樣的結構元素進行膨脹,使其拓撲結構不發生改變
dilation_circle (ConnectedRegions1, RegionDilation, 35)
*篩選並計算面積
select_shape (RegionDilation, SelectedRegions,
'area',
'and',
19278.9,
65000)
*顯示字型
sort_region (SelectedRegions, FinalBalls, 'first_point', 'true', 'column')
area_center (FinalBalls, Area, Row, Column)
dev_display (Dd)
dev_display (FinalBalls)
Num:=|Area|
disp_message (3600,'石頭個數:'+Num, 'image', 450,1000, 'white', 'false')
for i := 1 to Num by 1
disp_message (3600, Area[i-1], 'image', Row[i-1]-50,max([Column[i-1]-60,0]), 'white', 'false')
endfor
結果: