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

在工業領域,表面檢測是一個非常廣泛的應用領域。在halcon中,使用增強的光度立體視覺方法,三維表面檢測被加強。利用陰影可方便快速的檢測物體表面的缺口或凹痕。 使用光度立體視覺方法可在複雜圖像中輕鬆找到表面缺陷 。藥片包裝的背面,不平整並且還有很多文字。此示例便是使用光度立體法,檢測藥片包裝背面的缺陷。

示例代碼如下:

* 該示例通過使用光度立體技術檢測藥片包裝背面的缺陷
* 輸入是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算子獲得的,該算子可以同時得到表面梯度圖像和反照率圖像。該算子需要多張從不同角度拍照所得到的圖像作爲輸入。

更多最新文章,請關注公衆號:

執行流程:
待檢測的四張圖像之一:

反照率圖像:

高斯曲率圖像:

二值化處理得到藥片區域後:

預處理之後摳圖得到藥片圖像:

通過Blob分析和預處理,得到缺陷區域:

在高斯曲率圖像中標記出缺陷:

在反照率圖像中標記處缺陷:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章