halcon培訓-快速傅里葉變換(FFT)對塑料製品-@龍熙視覺
- 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.
**例程:detect_indent_fft.hdev
-
說明:這個程序展示瞭如何利用快速傅里葉變換(FFT)對塑料製品的表面進行目標(缺陷)的檢測,大致分爲三步:
-
首先,我們用高斯濾波器構造一個合適的濾波器(將原圖通過高斯濾波器濾波);
-
然後,將原圖和構造的濾波器進行快速傅里葉變換;
-
最後,利用形態學算子將缺陷表示在濾波後的圖片上(在缺陷上畫圈)。*
-
Initializations
-
在程序執行過程中選擇將PC更新操作打開或關閉
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’) -
線寬用Line Width 指定
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
-
構造兩個高斯濾波器
-
定義兩個常量
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) -
gauss_image(Filter, ImageGauss, 5)
-
gauss_filter(Image, ImageGauss1, 10)
-
Process the images iteratively
NumImages := 11
for Index :=1 to NumImages by 1
Index :=2
*-
Read an image and convert it to gray values
read_image (Image, ‘plastics/plastics_’ + Index$‘02’) -
把一個RGB圖像轉變成一個灰度圖像。
rgb1_to_gray (Image, Image) -
Perform the convolution in the frequency domain
*3-5-7-9-11
gauss_filter(Image, ImageGauss1, 9) -
在快速傅里葉變換中 計算一個圖像的實值。
rft_generic (Image, ImageFFT, ‘to_freq’, ‘none’, ‘complex’, Width) -
用在頻域內的濾波器使一個圖像卷積。
-
不做濾波的話 ,無法去除噪聲
convol_fft (ImageFFT, Filter, ImageConvol)
rft_generic (ImageFFT, ImageFiltered, ‘from_freq’, ‘n’, ‘real’, Width) -
Process the filtered image
-
用一個矩形掩碼計算像素點的灰度範圍
*是一個8位單通道圖像(灰度圖/二值圖)
*掩碼某個位置如果爲0,則在此位置上的操作不起作用
*掩碼某個位置如果不爲0,則在此位置上的操作會起作用
*可以用來提取不規則ROI
gray_range_rect (ImageFiltered, ImageResult, 10, 10)
-
-
intensity(ImageResult, ImageResult, MeanValue, Deviation)
- 決定區域內最小最大灰度值
min_max_gray (ImageResult, ImageResult, 0, Min, Max, Range)
- 決定區域內最小最大灰度值
if (Max > 6.8)
value := 6.8
else
value := Max * 0.8
endif
* 利用全局閾值對圖像進行分割
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,用於後面顯示生成缺陷個數
Number := |Area|
if (Number)
*畫出1個或者多個缺陷位置,並且顯示
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