This program demonstrates how to detect small texture
defects on the surface of plastic items by using the fast
fourier transform (FFT).
First, we construct a suitable filter using Gaussian
filters. Then, the images and the filter are convolved
by using fast fourier transforms. Finally, the defects
are detected in the filtered images by using
morphology operators.
這個程序是通過 FFT 來檢測物體表面的瑕疵。首先,我們要用高斯濾波器來構造一個合適的濾波器。然後,圖像和濾波器通過 FFT 被轉換。最後通過形態學操作在被濾波的圖像上進行檢測。
-
- Initializations
初始化
dev_update_off ()
dev_close_window ()
read_image (Image, ‘plastics/plastics_01’)
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, ‘black’, WindowHandle)
set_display_font (WindowHandle, 14, ‘mono’, ‘true’, ‘false’)
dev_set_draw (‘margin’)
dev_set_line_width (3)
dev_set_color (‘red’)
-
- Optimize the fft speed for the specific image size
為特殊圖像尺寸定制 FFT 速度。
optimize_rft_speed (Width, Height, ‘standard’)
-
Construct a suitable filter by combining two gaussian
filters
通過 2 個高斯濾波器來構造一個合適的濾波器
Sigma1 := 10.0
Sigma2 := 3.0
gen_gauss_filter (GaussFilter1, Sigma1, Sigma1, 0.0, ‘none’, ‘rft’, Width, Height)
gen_gauss_filter (GaussFilter2, Sigma2, Sigma2, 0.0, ‘none’, ‘rft’, Width, Height)
sub_image (GaussFilter1, GaussFilter2, Filter, 1, 0)
-
- Process the images iteratively
重複處理圖像
NumImages := 11
for Index := 1 to NumImages by 1
*
- Read an image and convert it to gray values
讀取圖像并轉化為灰度圖像
read_image (Image, 'plastics/plastics_' + Index$'02')
rgb1_to_gray (Image, Image)
- Perform the convolution in the frequency domain
在頻率域生成捲積
rft_generic (Image, ImageFFT, 'to_freq', 'none', 'complex', Width)
convol_fft (ImageFFT, Filter, ImageConvol)
rft_generic (ImageConvol, ImageFiltered, 'from_freq', 'n', 'real', Width)
*
- Process the filtered image
處理被濾波圖像
gray_range_rect (ImageFiltered, ImageResult, 11, 11)
min_max_gray (ImageResult, ImageResult, 0, Min, Max, Range)
threshold (ImageResult, RegionDynThresh, max([5.55,Max * 0.8]), 255)
connection (RegionDynThresh, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 4, 99999)
union1 (SelectedRegions, RegionUnion)
closing_circle (RegionUnion, RegionClosing, 10)
connection (RegionClosing, ConnectedRegions1)
select_shape (ConnectedRegions1, SelectedRegions1, 'area', 'and', 10, 99999)
area_center (SelectedRegions1, Area, Row, Column)
*
- Display the results
顯示結果
dev_display (Image)
Number := |Area|
if (Number)
gen_circle_contour_xld (ContCircle, Row, Column, gen_tuple_const(Number,30), gen_tuple_const(Number,0), gen_tuple_const(Number,rad(360)), 'positive', 1)
ResultMessage := ['Not OK',Number + ' defect(s) found']
Color := ['red','black']
dev_display (ContCircle)
else
ResultMessage := 'OK'
Color := 'forest green'
endif
disp_message (WindowHandle, ResultMessage, 'window', 12, 12, Color, 'true')
if (Index != NumImages)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
endif
endfor