本示例通過形態學處理,然後再通過矩形度和孔洞面積作爲判斷依據,來確認餅乾的完整情況。
示例代碼:
* 這個例子描敘了餅乾質量檢測。
* 使用形態學進行提取和檢查
* 通過一些形狀特徵,比如矩形度還有孔洞面積
* 讀圖像
read_image (Image, 'food/hazelnut_wafer_01')
dev_close_window ()
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
dev_update_window ('off')
dev_set_line_width (3)
dev_set_draw ('margin')
set_display_font (WindowHandle, 20, 'mono', 'true', 'false')
*
for Index := 1 to 24 by 1
* 讀圖像
read_image (Image, 'food/hazelnut_wafer_' + Index$'.02')
* 快速二值化,提取出屬於餅乾區域的亮區域
binary_threshold (Image, Foreground, 'smooth_histo', 'light', UsedThreshold)
* 開運算,去掉外圍的一些雜點
opening_circle (Foreground, FinalRegion, 8.5)
* 計算區域中所有孔洞的面積和
area_holes (FinalRegion, AreaHoles)
* 計算區域的矩形度,也就是給該區域打分,越與矩形相似,得分越高
rectangularity (FinalRegion, Rectangularity)
dev_display (Image)
* 以孔洞面積、矩形度來判斷餅乾的完整性
* 區域內孔洞面積越大,說明餅乾內部碎,如果矩形度得分低,說明餅乾的邊緣碎
if (AreaHoles > 300 or Rectangularity < 0.92)
dev_set_color ('red')
Text := 'Not OK'
else
dev_set_color ('forest green')
Text := 'OK'
endif
dev_display (FinalRegion)
disp_message (WindowHandle, Text, 'window', -1, -1, '', 'false')
if (Index < 24)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
endif
endfor
重點說明:
1. 計算區域內孔洞面積的算子爲area_holes,如果在區域內有多個孔洞,該算子會計算所有孔洞的面積和。
2. 得到區域的矩形度算子爲rectangularity,如果區域和矩形相似度越高,則得分越高。
3. 判斷一塊餅乾是否完整的判斷條件爲:如果矩形度得分小於0.92分,或者是孔洞面積大於300,但凡這兩個條件有一個滿足,則該餅乾是不完整的。
更多最新文章,請關注公衆號:
執行流程:
完整的餅乾圖像
完整餅乾的區域:
完整餅乾的檢測結果顯示:
破碎的餅乾圖像:
破碎餅乾的區域:
破碎餅乾的檢測結果顯示: