* 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.
*
* 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
optimize_rft_speed (Width, Height, 'standard')
*
* Construct a suitable filter by combining two gaussian
* filters
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, 10, 10)
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