Halcon學習筆記之缺陷檢測(一)


例程:surface_scratch.hdev

說明:這個程序利用局部閾值和形態學處理提取表面划痕

代碼中綠色部分為個人理解和注釋,其余為例程中原有代碼

*surface_scratch.hdev:extraction of surface scratches via local thresholding and morphological post-processing*

dev_close_window()
dev_update_window(‘off’)
*****
*step: acquire image                  //獲取圖片
*****
read_image(Image,’surface_scratch’)  //讀入圖片名為’surface_scratch’的圖片
get_image_size(Image,Width,Height)    //獲取圖像的尺寸:寬、高
//在坐標為(0,0)處打開一個圖像窗口。其寬和高分別是Width和Width,窗口的句柄為WindowID
dev_open_window_fit_image(Image,0,0,Width,Width,WindowID)
//設置窗口中顯示的字體顏色,大小,加粗,傾斜
set_display_font(WindowID,12,’Courier’,’true’,’false’)                        
dev_set_draw(‘margin’)              //設置區域填充類型:’margin’ or ‘fill’
dev_set_line_width(4)                //設置線寬,這個在填充類型為fill時效果不明顯

dev_display(Image)                   //顯示圖片
disp_continue_message(WindowID,’black’,’true’)    //顯示繼續消息,等待用戶按F5繼續執行
stop()

*****
*step: segment image
*****
*-> using a local threshold
mean_image(Image,ImageMean,7,7)                      //用7×7的窗口對圖像進行均值濾波
dyn_threshold(Image,ImageMean,DarkPixels,5,’dark’)  //利用本地閾值進行圖像分割

*->extract connected components
connection(DarkPixels,ConnectedRegions)    //對區域進行連通運算
dev_set_colored(12)                        //設置區域顯示的顏色數目
dev_display(ConnectedRegions)              //顯示圖像
disp_continue_message(WindowID,’black’,’true’)//顯示繼續提示,提示繼續
stop()

*****
*step: process regions
*****
*->select large regions
select_shape(ConnectedRegions,SelsectedRegions,’area’,’and’,10,1000)    //區域選擇,用面積的形態特征選擇面積在10到10000間的區域
dev_display(Image)                      //顯示原圖
dev_display(SelectedRegions)            //顯示選擇的區域
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_set_colored類似
dev_display(Image)                //顯示原圖
dev_display(SelectedRegions)      //顯示選中的區域圖
disp_continue_message(WindowID,’black’,’true’)    //顯示繼續提示信息
stop()

*->merge fractioned scratches via morphology
union1(SelectedRegions,RegionUnion)             //合並所有區域
dilation_circle(RegionUnion,RegionDilation,3.5) //用半徑3.5 的掩膜進行膨脹
dev_display(Image)                              //顯示原圖
dev_display(RegionDilation)                     //顯示膨脹后的區域圖
disp_continue_message(WindowID,’black’,’true’)//顯示繼續提示信息
stop()

skeleton(RegionDialtion,Skeleton)      //計算區域的中軸,見例程:surface_scratch.hdev
connection(Skeleton,Errors)            //連通操作,以便后面進行區域選擇
dev_set_colored(12)                    //設置顏色
dev_display(Image)                     //顯示原圖
dev_display(Errors)                    //顯示所有抓痕
disp_continue_message(WindowID,’black’,’true’)    //顯示繼續提示信息
stop()

*->distinguish small and large scratches
close_zoom_window(WindowHandleZoom,Width,Height)       //關閉縮放窗口
select_shape(Errors,Scratches,’area’,’and’,50,10000)//選擇區域面積在50到10000的作為划痕
select_shape(Errors,Dots,’area’,’and’,1,50)         //選擇區域面積在1到50 的作為點
dev_display(Image)                    //顯示原圖
dev_set_color(‘red’)                 //設置區域顯示顏色為紅色
dev_display(Scratches)                //顯示條狀划痕(顯示為紅色)
dev_set_color(‘blue’)               //設置區域顯示顏色藍色
dev_display(Dots)                     //顯示點狀划痕(顯示為藍色)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM