Halcon表面缺陷檢測-光度立體法檢測藥片包裝背面的缺陷

Geeyoo發表於2019-07-16

對應示例程式:
inspect_blister_photometric_stereo.hdev

目標:通過光度立體法檢測藥片包裝背面的缺陷

思路為:
      1.讀入多張從不同角度拍攝的藥片包裝的背面影像
      2.應用光度立體得到反照率影像和表面梯度影像
      3.使用之前得到的表面梯度,計算表面的高斯曲率,得到高斯曲率影像
      4. 對高斯曲率影像進行預處理和Blob分析,從而得到缺陷區域
      5. 在影像中標記缺陷區域

影像:
在這裡插入圖片描述

程式碼:

* 該示例通過使用光度立體技術檢測藥片包裝背面的缺陷
* 輸入是4張不同的藥片包裝背面圖片,光從不同的角度照射

* Initialization
dev_close_window ()
dev_update_off ()
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
Message := 'Inspect the backside of a blister'
Message[1] := 'using photometric stereo. In this case four'
Message[2] := 'different light orientations were used.'
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 
* Show input images with different illumination
* 1 讀影像,依次讀取多張影像
* 下面的for迴圈,依次顯示4張從不同角度拍攝的藥片包裝的背面影像
read_image (Images, 'photometric_stereo/blister_back_0' + [1:4])
for I := 1 to 4 by 1
    Message := 'Acquire image ' + I + ' of 4'
    select_obj (Images, ObjectSelected, I)
    dev_display (ObjectSelected)
    disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
    wait_seconds (0.5)
endfor
* 
* Apply photometric stereo to determine the albedo
* and the surface gradient.
* 2. 應用光度立體得到反照率影像和表面梯度影像

* 描述了從影像中心指向右側的方向與投射到平面中的光的方向之間的角度。 
* 也就是說,當觀察影像(或相應的場景)時,傾斜角度為0表示光線來自右側,
* 傾斜角度為90表示光線來自頂部,傾斜角度為180表示 光是從左邊來的
Tilts := [6.1,95.0,-176.1,-86.8]
* 物平面與照明方向之間的角度
Slants := [41.4,42.6,41.7,40.9]
ResultType := ['gradient','albedo']
* 該運算元得到反照率影像和表面梯度影像
photometric_stereo (Images, HeightField, Gradient, Albedo, Slants, Tilts, ResultType, 'poisson', [], [])
* 
* Display the albedo image
* 顯示反照率影像
dev_display (Albedo)
disp_message (WindowHandle, 'Albedo image', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 
* Calculate the gaussian curvature of the surface
* using the gradient field as input for the operator
* 3. 使用之前得到的表面梯度,計算表面的高斯曲率,得到高斯曲率影像
* derivate_vector_field.
* Defects are usually easy to detect in the curvature image.
* 在曲率影像上能更容易的進行檢測
* 該運算元通過之前得到的表面梯度得到高斯曲率影像
derivate_vector_field (Gradient, GaussCurvature, 1, 'gauss_curvature')
* 
* Detect defects
* 檢測缺陷
* 
* Segment the tablet areas in the curvature image
* 4. 對高斯曲率影像進行預處理和Blob分析,從而得到缺陷區域
* 在曲率影像中,我們先分開各個藥片區域
regiongrowing (GaussCurvature, Regions, 1, 1, 0.001, 250)
* 通過寬和高的特徵,進行特徵選擇
select_shape (Regions, TabletRegions, ['width','height'], 'and', [150,150], [200,200])
* 凸性形狀轉換,針對區域
shape_trans (TabletRegions, TabletRegions, 'convex')
* 區域聯合
union1 (TabletRegions, TabletRegions)
* 腐蝕
erosion_circle (TabletRegions, TabletRegions, 3.5)
* Search for defects inside the tablet areas
* 在藥片區域搜尋缺陷
* 摳圖
reduce_domain (GaussCurvature, TabletRegions, ImageReduced)
* 計算影像各個畫素的絕對值,存在此次處理的原因是:高斯曲率影像存在負值
* 缺陷處,高斯曲率會比較大
abs_image (ImageReduced, ImageAbs)
* 二值化 ,灰度直方圖
threshold (ImageAbs, Region, 0.03, 255)
* 閉運算
closing_circle (Region, RegionClosing, 10.5)
* 斷開得到連通域
connection (RegionClosing, ConnectedRegions)
* 通過面積特徵,進行特徵選擇
select_shape (ConnectedRegions, Defects, 'area', 'and', 10, 99999)
* 獲得缺陷區域的中心點行列座標
area_center (Defects, Area, Row, Column)
* 生成圓形區域
gen_circle (Circle, Row, Column, gen_tuple_const(|Row|,20.5))
* Display the defects in curvature image
* 5. 接下來在高斯曲率影像中標記缺陷區域
dev_set_draw ('margin')
dev_set_color ('red')
dev_set_line_width (2)
dev_display (GaussCurvature)
dev_display (Circle)
Message := 'The defect can easily be detected'
Message[1] := 'in the surface curvature image'
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
stop ()
* Display the defects in the albedo image
* 6. 在反照率影像中標記出缺陷
dev_set_draw ('margin')
dev_set_color ('red')
dev_display (Albedo)
dev_display (Circle)
disp_message (WindowHandle, 'Defect in albedo image', 'window', 12, 12, 'black', 'true')

重點說明:

1.光度立體法便是在得到高斯曲率影像之後,在高斯曲率影像上進行預處理和Blob分析,檢測出缺陷。

2.高斯曲率影像的獲得,是通過derivate_vector_field運算元獲得的,該運算元中,運算元中需要輸入表面梯度影像,因此,在使用該運算元之前,需要先獲得表面梯度影像。

3.表面梯度影像的獲得,是通過photometric_stereo運算元獲得的,該運算元可以同時得到表面梯度影像和反照率影像。該運算元需要多張從不同角度拍照所得到的影像作為輸入。

用到的幾個運算元:
      photometric_stereo—根據光度立體技術重建曲面,得到反照率影像和表面梯度影像
      derivate_vector_field–將向量場的分量與高斯函式的導數進行卷積,並計算由此得到的各種特徵

類似程式:
inspect_shampoo_label_photometric_stereo.hdev—檢查洗髮水瓶的標籤
inspect_flooring_photometric_stereo.hdev–檢測底板缺陷
inspect_leather_photometric_stereo.hdev–皮革樣品的檢驗
圖片:
在這裡插入圖片描述
在這裡插入圖片描述
在這裡插入圖片描述
參考資料:
[1]: https://mp.weixin.qq.com/s/rDig25W7iURgWnZey-eFKw
[2].https://www.doc88.com/p-4714835177160.html
[3].https://blog.csdn.net/rj_2080/article/details/103975551

相關文章