Halcon例程分析7:劃痕檢測

打開halcon,按下ctrl+e打開halcon自帶例程。工業領域->光學與精密工程->surface_scratch.hdev

* This programm shows the extraction of surface scratches via
* local thresholding and morphological post-processing
* 
dev_update_off ()
dev_close_window ()
* 
* Step 1: Acquire image
* 
*獲取圖像
read_image (Image, 'surface_scratch')
get_image_size (Image, Width, Height)
*原圖大小顯示圖像
dev_open_window_fit_image (Image, 0, 0, Width, Width, WindowID)
set_display_font (WindowID, 16, 'mono', 'true', 'false')
dev_set_draw ('margin')
dev_set_line_width (4)
dev_display (Image)
*提示信息
Message := 'This program shows the extraction of'
Message[1] := 'surface scratches via local thresholding'
Message[2] := 'and morphological post-processing'
disp_message (WindowID, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowID, 'black', 'true')
stop ()
* 
* Step 2: Segment image
* 
* Using a local threshold
*均值濾波,卷積核大小7×7
mean_image (Image, ImageMean, 7, 7)
*動態閾值化,關於這個函數的說明可以參考
*第一個參數原始圖像
*第二個參數參考圖像
*第三個參數輸出圖像
*第四個參數偏移值
*第五個參數明暗選擇參數。選擇'dark'是輸出圖像選擇比參考圖像灰度值小5的部分
dyn_threshold (Image, ImageMean, DarkPixels, 5, 'dark')
* 
* Extract connected components
*分割閾值化後的圖像
connection (DarkPixels, ConnectedRegions)
dev_set_colored (12)
dev_display (Image)
dev_display (ConnectedRegions)
Message := 'Connected components after image segmentation'
Message[1] := 'using a local threshold.'
disp_message (WindowID, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowID, 'black', 'true')
stop ()
* 
* Step 3: Process regions
* 
* Select large regions
*選擇面積特徵在10到1000之間的區域
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 10, 1000)
dev_display (Image)
dev_display (SelectedRegions)
disp_message (WindowID, 'Large Regions', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowID, 'black', 'true')
stop ()
* 
* Visualize fractioned scratch
*打開一個縮放窗口,顯示原圖像中小劃痕這部分
open_zoom_window (0, round(Width / 2), 2, 303, 137, 496, 3, WindowHandleZoom)
dev_set_color ('blue')
dev_display (Image)
dev_display (SelectedRegions)
set_display_font (WindowHandleZoom, 16, 'mono', 'true', 'false')
disp_message (WindowHandleZoom, 'Fractioned scratches', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandleZoom, 'black', 'true')
stop ()
* 
* Merge fractioned scratches via morphology
*聯結圖像,把前面connection函數分割的區域聯結起來
union1 (SelectedRegions, RegionUnion)
*圖像腐蝕,把大點的區域選擇出來,小的區域隱藏
dilation_circle (RegionUnion, RegionDilation, 3.5)
dev_display (Image)
dev_display (RegionDilation)
Message := 'Region of the scratches after dilation'
disp_message (WindowHandleZoom, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandleZoom, 'black', 'true')
stop ()
*對腐蝕後的圖像提取骨架
skeleton (RegionDilation, Skeleton)
*分割骨架圖像,把各自劃痕和污點分割開
connection (Skeleton, Errors)
dev_set_colored (12)
dev_display (Image)
dev_display (Errors)
Message := 'Fractioned scratches merged via morphology'
disp_message (WindowHandleZoom, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandleZoom, 'black', 'true')
stop ()
* 
* Distinguish small and large scratches
close_zoom_window (WindowHandleZoom, Width, Height)
*選擇面積特徵50到10000的區域爲劃痕
select_shape (Errors, Scratches, 'area', 'and', 50, 10000)
*選擇1到50的區域爲污點
select_shape (Errors, Dots, 'area', 'and', 1, 50)

dev_display (Image)
dev_set_color ('red')
dev_display (Scratches)
dev_set_color ('blue')
dev_display (Dots)
Message := 'Extracted surface scratches'
Message[1] := 'Not categorized as scratches'
disp_message (WindowID, Message, 'window', 440, 310, ['red','blue'], 'true')

測試原圖像

劃痕與污點測試結果圖像,紅色顯示劃痕區域,藍色顯示污點區域

 

發佈了60 篇原創文章 · 獲贊 8 · 訪問量 9681
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章