HALCON示例程序check_bottle_crate.hdev啤酒箱內酒瓶數檢測
示例程序源碼(加注釋)
- 獲取系統關于“空白區域儲存的設置”
get_system (‘store_empty_region’, StoreEmptyRegion) - 系統“空白區域儲存”設置為 ‘false’
set_system (‘store_empty_region’, ‘false’) - 讀入圖片
read_image (Image, ‘bottles/bottle_crate_01’) - 關閉窗口
dev_close_window () - 打開一個新圖形窗口,保留給定的長寬比
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle) - 設置顯示字體格式
set_display_font (WindowHandle, 16, ‘mono’, ‘true’, ‘false’) - 關閉窗口更新
dev_update_off () - 區域以邊緣形式顯示
dev_set_draw (‘margin’) - 循環檢測
for Index := 1 to 24 by 1
read_image (Image, ‘bottles/bottle_crate_’ + Index$’.02’)-
- 提取背景
- 進行閾值分割,選取像素灰度值在50-130的區域
threshold (Image, BackgroundRegion, 50, 130) - 使用圓形元素進行開運算
opening_circle (BackgroundRegion, BackgroundRegion, 3.5) -
- 提取瓶子,瓶子口是很亮的。所以選取85-255區域
- 進行閾值分割選取85-255的區域
threshold (Image, Region, 85, 255) - 求取背景與提取瓶口區域的差集,將所有瓶口提取出來
difference (Region, BackgroundRegion, Region) - 分割連通域
connection (Region, ConnectedRegions) -
- 篩選大面積的反光區域,當做干擾處理
- 使用長度寬度元素進行篩選,篩選出非瓶子的遮擋區域
select_shape (ConnectedRegions, Clutter, [‘width’,‘height’], ‘or’, [100,100], [500,400]) - 使用圓形元素進行開運算
opening_circle (Clutter, Clutter, 8.5) - 求取差集,目的是在進行酒瓶識別時不要將不是酒瓶的區域選進去
difference (ConnectedRegions, Clutter, Region) - 分割連通域
connection (Region, ConnectedRegions) -
- 消除噪聲區域
- 使用長寬篩選認為是酒瓶子的區域
select_shape (ConnectedRegions, Candidates, [‘width’,‘height’], ‘and’, [25,25], [100,100]) - 對區域進行空洞填充
fill_up (Candidates, FilledCandidates) - 因為酒瓶區域均為圓形,我們要保留圓形去除干擾,就用圓形元素對區域進行開運算
opening_circle (FilledCandidates, FilledCandidates, 15.5) - 使用圓度對區域進行篩選
select_shape (FilledCandidates, RoundCandidates, ‘circularity’, ‘and’, 0.87, 1) - 篩選出區域兩點最大距離達到75以上的區域,也就是說不是瓶子的區域。
select_shape (RoundCandidates, BigBottles, ‘max_diameter’, ‘and’, 75, 99999) - 顯示
dev_set_shape (‘ellipse’)
count_obj (RoundCandidates, NumBottles)
count_obj (Clutter, NumClutter)
dev_display (Image)
dev_set_line_width (5)
dev_set_color (‘lime green’)
dev_display (RoundCandidates)
dev_set_color (‘goldenrod’)
dev_display (BigBottles)
dev_set_color (‘red’)
dev_display (Clutter)
dev_set_shape (‘original’)
disp_message (WindowHandle, NumBottles + ’ bottles found.’, ‘window’, -1, -1, ‘black’, ‘true’)
if (NumClutter > 0)
disp_message (WindowHandle, ‘Warning! Clutter detected!’, ‘window’, 40, -1, ‘red’, ‘true’)
endif
if (Index < 24)
disp_continue_message (WindowHandle, ‘black’, ‘true’)
stop ()
endif
endfor
-
- 恢復系統初始設置
set_system (‘store_empty_region’, StoreEmptyRegion)
處理思路
經典的bolo分析進行酒瓶統計的例程,首先提取出背景干擾部分,再進行瓶身提取,提取出的瓶身區域減去提取出的背景干擾,最終將瓶身提取出來;通過開運算提取圓形的平身,消除箱體的干擾;通過區域的寬高的特征篩選出哪些是真正的瓶身,哪些是沒有擺放好的瓶子。
后記
大家有什么問題可以向我提問哈,我看到了第一時間回復,希望在學習的路上多多結交良師益友。