文章目录
- 傅里叶变换频谱
- Halcon 频域+空间域检测缺陷
- Halcon 频域+差分+空间域 缺陷检测(lines_gauss 提取线)
- Halcon 频域+差分+空间域(blob+特征)案例
- Halcon Blob+特征处理缺陷
- Halcon 频域+空间域检测划痕
- Halcon 傅里叶变换转换为功率图(频域+blob+差分)
- Halcon 训练学习缺陷检测(以神经网络mlp为例)
傅里叶变换频谱
矩形
read_image (Image1, 'C:/Users/Augustine/Desktop/频谱图形/1.png')
fft_image(Image1,ImageFFT)
圆
read_image (Image2, 'C:/Users/Augustine/Desktop/频谱图形/2.png')
fft_generic(Image2, ImageFFT, 'to_freq', -1, 'sqrt', 'dc_center', 'complex')
菱形
read_image (Image3, 'C:/Users/Augustine/Desktop/频谱图形/3.png')
fft_image(Image3, ImageFFT)
黑白相间的亮带
read_image (Image4, 'C:/Users/Augustine/Desktop/频谱图形/4.png')
fft_generic(Image4, ImageFFT, 'to_freq', -1, 'sqrt', 'dc_center', 'complex')
去除图纹(反傅里叶变换)
read_image (Image4, 'C:/Users/Augustine/Desktop/频谱图形/4.png')
* 图片转换为灰度图
rgb1_to_gray(Image4, GrayImage)
fft_image(GrayImage, ImageFFT)
gen_rectangle1 (ROI_0, 12.9631, 372.108, 377.501, 710.691)
gen_rectangle1 (ROI_0, 17.1055, 11.9479, 373.359, 347.212)
union2(ROI_0, ROI_0, RegionUnion)
gen_rectangle1 (ROI_0, 412.712, 13.6076, 754.467, 345.552)
union2(ROI_0, ROI_0, RegionUnion1)
gen_rectangle1 (ROI_0, 412.712, 368.788, 768.966, 709.031)
union2(ROI_0, ROI_0, RegionUnion2)
* 区域填充
paint_region(RegionUnion2, ImageFFT, ImageResult,0, 'fill')
* 反傅里叶变换
fft_image_inv(ImageResult, ImageFFTInv)
去除图纹滤波器处理
低通滤波器、高通滤波器和中通滤波器都是数字图像处理中常用的滤波器,它们的作用是将输入图像信号进行滤波处理,以达到去除噪声、增强图像特征等目的。
低通滤波器(Low<span class="token operator">-</span>Pass Filter):低通滤波器可以去除图像中高频部分的信息,保留低频部分的信息。在频域上看,低通滤波器会将图像高频成分的能量减弱,从而产生模糊的效果。低通滤波器一般用于平滑图像、去除噪声等应用场景。 高通滤波器(High<span class="token operator">-</span>Pass Filter):高通滤波器可以去除图像中低频部分的信息,保留高频部分的信息。在频域上看,高通滤波器会将图像低频成分的能量减弱,从而使高频细节更加突出。高通滤波器一般用于图像增强、边缘检测等应用场景。 中通滤波器(Band<span class="token operator">-</span>Pass Filter):中通滤波器可以去除图像中低频和高频部分的信息,保留中频部分的信息。中通滤波器一般用于分离出图像中特定频率范围内的信息,例如检测特定大小的物体。
read_image (Image4, 'C:/Users/Augustine/Desktop/频谱图形/4.png')
* 图片转换为灰度图
rgb1_to_gray(Image4, GrayImage)
*fft_image(GrayImage, ImageFFT)
fft_generic(ImageConvol, ImageFFT1, 'to_freq', -1, 'sqrt', 'dc_center', 'complex'))
get_image_size(ImageFFT, Width, Height)
* 高通滤波器,低频段挡住为黑色
gen_highpass(ImageHighpass, 0.2, 'none', 'dc_center', Width, Height)
* 频域中对两个经过傅里叶变换的图像进行卷积操作
convol_fft(ImageFFT, ImageHighpass, ImageConvol)
* 反傅里叶变换 第三 参数改成'from_freq',1和-1要反着来
fft_generic(ImageConvol, ImageFFT1, 'from_freq', 1, 'sqrt', 'dc_center', 'complex'))
Halcon 频域+空间域检测缺陷
高斯滤波主要用于祛除图像中的高频成分(低通滤波器),也就是去除图像中的细节和噪声。通过在图像上应用高斯核进行卷积操作,高频部分会被削弱,从而使图像变得更加平滑。
由于高斯核的特性,其在中心位置具有最大值,并且随着离中心的距离逐渐减小。这意味着高斯滤波会更强调图像中的低频信息,即图像中相对较平均和较大尺度的变化。而高频信息,例如细节和噪声,由于高斯核的衰减作用,会在滤波过程中被抑制或消除。
因此,高斯滤波的主要效果是减少图像中的高频成分,实现图像的平滑和模糊化。这种平滑可以有效地去除图像中的噪声,并在某些情况下有助于提高图像处理的结果。
* 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
* 1.采集图像
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
* 2.产生滤波器(产生高斯滤波)
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)
* 3.傅里叶变换(频域变换)
* 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
* 4.空间域blob 分析
* 灰度范围变化(把图像亮的地方更亮,暗的地方更暗)
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
Halcon 频域+差分+空间域 缺陷检测(lines_gauss 提取线)
* this example shows how to detect mura defects
* in blurred images
*
dev_close_window ()
dev_update_off ()
Path := 'lcd/mura_defects_blur_'
read_image (Image, Path + '01')
get_image_size (Image, Width, Height)
dev_open_window_fit_size (0, 0, Width, Height, 640, 480, WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
dev_set_draw ('margin')
dev_set_line_width (3)
dev_set_color ('red')
* 设置缩放因子
ScaleFactor := 0.4
* 计算高斯线性参数
calculate_lines_gauss_parameters (17, [25,3], Sigma, Low, High)
for f := 1 to 3 by 1
read_image (Image, Path + f$'.2i')
* 获取rgb 通道
decompose3 (Image, R, G, B)
* correct side illumination
* 频域处理
fft_generic(B, ImageFFT, 'to_freq', -1, 'none', 'dc_center', 'complex')
*rft_generic (B, ImageFFT, 'to_freq', 'none', 'complex', Width)
* 产生高斯过滤
*gen_gauss_filter (ImageGauss, 100, 100, 0, 'n', 'rft', Width, Height)
gen_gauss_filter(ImageGauss, 100, 100, 0, 'n', 'dc_center', Width, Height)
* 频域中对两个经过傅里叶变换的图像进行卷积操作
convol_fft (ImageFFT, ImageGauss, ImageConvol)
*从频域转换为空间域
fft_generic(ImageConvol, ImageFFT1, 'from_freq', -1, 'none', 'dc_center', 'byte')
*rft_generic (ImageConvol, ImageFFT1, 'from_freq', 'none', 'byte', Width)
* 图片相减 mageSub=(B-ImageFFT1)*2+100
sub_image (B, ImageFFT1, ImageSub, 2, 100)
* perform the actual inspection
* 按照比例因子缩放
zoom_image_factor (ImageSub, ImageZoomed, ScaleFactor, ScaleFactor, 'constant')
* avoid border effects when using lines_gauss()
* 由图像转换为区域,获取区域
get_domain (ImageZoomed, Domain)
* 腐蚀运算
erosion_rectangle1 (Domain, RegionErosion, 7, 7)
* 裁剪区域
reduce_domain (ImageZoomed, RegionErosion, ImageReduced)
* 空间域检测线条
lines_gauss (ImageReduced, Lines, Sigma, Low, High, 'dark', 'true', 'gaussian', 'true')
* 仿射变换
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_scale_local (HomMat2DIdentity, 1 / ScaleFactor, 1 / ScaleFactor, HomMat2DScale)
affine_trans_contour_xld (Lines, Defects, HomMat2DScale)
* 显示
dev_display (Image)
dev_display (Defects)
if (f < 3)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
endif
endfor
Halcon 频域+差分+空间域(blob+特征)案例
estimate_background_illumination (B, ImageFFT1)
get_image_size (Image, Width, Height)
* 空间域转换为频域
fft_generic(Image, ImageFFT, 'to_freq', -1, 'none', 'dc_center', 'complex')
*rft_generic (Image, ImageFFT, 'to_freq', 'none', 'complex', Width)
* 产生高斯滤波
*gen_gauss_filter (ImageGauss, 50, 50, 0, 'n', 'rft', Width, Height)
gen_gauss_filter(ImageGauss, 50, 50, 0, 'n', 'dc_center', Width, Height)
* 卷积
convol_fft (ImageFFT, ImageGauss, ImageConvol)
* 频域转换为空间域
fft_generic(ImageConvol, IlluminationImage, 'from_freq', 1, 'sqrt', 'dc_center', 'byte')
*rft_generic (ImageConvol, IlluminationImage, 'from_freq', 'none', 'byte', Width)
return ()
* This example shows how to detect mura defects
* in highly textured images
*
* 图片预处理
dev_close_window ()
dev_update_off ()
Path := 'lcd/mura_defects_texture_'
read_image (Image, Path + '01')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, 640, 480, 'black', WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
dev_set_draw ('margin')
dev_set_line_width (3)
dev_set_color ('red')
for F := 1 to 2 by 1
*1.采集图像
read_image (Image, Path + F$'.2i')
decompose3 (Image, R, G, B)
* Defects are characterized by dark patches. Hence, by substracting the
* estimated background illumination from the original image the
* defects become more apparent.
* 2.频域处理+差分
estimate_background_illumination (B, ImageFFT1)
*
sub_image (B, ImageFFT1, ImageSub, 2, 100)
* 3.空间域blob+特征
* Median filter smoothes out the fine texture, simplifying the following
* segmentation and final detection of defects.
* 中值滤波
median_image (ImageSub, ImageMedian, 'circle', 9, 'mirrored')
watersheds_threshold (ImageMedian, Basins, 20)
* Dark patches corresponding to defects have a very low energy.
cooc_feature_image (Basins, ImageMedian, 6, 0, Energy, Correlation, Homogeneity, Contrast)
*tuple_find(sgn(Energy-0.05),-1,Indices)
*select_obj(Basins, Defects, Indices+1)
* 如果Energy<= 0.05
Mask := Energy [<=] 0.05
* 获取Mask 个数
select_mask_obj (Basins, Defects, Mask)
* 显示
dev_display (Image)
dev_display (Defects)
count_obj (Defects, NDefects)
disp_message (WindowHandle, NDefects + ' \'mura\' defects detected', 'window', 12, 12, 'red', 'true')
if (F < 2)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
endif
endfor
Halcon Blob+特征处理缺陷
* This programm shows the extraction of surface scratches via
* local thresholding and morphological post-processing
*
dev_update_off ()
dev_close_window ()
*
* Step 1: Acquire image
* 1.采集图片
read_image (Image, 'surface_scratch')
get_image_size (Image, Width, Height)
dev_open_window_fit_image (Image, 0, 0, Width, Width, WindowID)
set_display_font (WindowID, 16, 'mono', 'true', 'false')
dev_set_draw ('margin')
dev_set_line_width (4)
dev_display (Image)
Message := 'This program shows the extraction of'
Message[1] := 'surface scratches via local thresholding'
Message[2] := 'and morphological post-processing'
disp_message (WindowID, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowID, 'black', 'true')
stop ()
*
* Step 2: Segment image
* 2.图像分割
* Using a local threshold
* 均值滤波
mean_image (Image, ImageMean, 7, 7)
* 动态二值化
dyn_threshold (Image, ImageMean, DarkPixels, 5, 'dark')
*
* Extract connected components
* 形成单独的连通域
connection (DarkPixels, ConnectedRegions)
dev_set_colored (12)
dev_display (Image)
dev_display (ConnectedRegions)
Message := 'Connected components after image segmentation'
Message[1] := 'using a local threshold.'
disp_message (WindowID, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowID, 'black', 'true')
stop ()
*
* Step 3: Process regions
*
* Select large regions
* 筛选面积大小
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 10, 1000)
dev_display (Image)
dev_display (SelectedRegions)
disp_message (WindowID, 'Large Regions', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowID, 'black', 'true')
stop ()
*
* Visualize fractioned scratch
* 在新的窗口打开
open_zoom_window (0, round(Width / 2), 2, 303, 137, 496, 3, WindowHandleZoom)
dev_set_color ('blue')
dev_display (Image)
dev_display (SelectedRegions)
set_display_font (WindowHandleZoom, 16, 'mono', 'true', 'false')
disp_message (WindowHandleZoom, 'Fractioned scratches', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandleZoom, 'black', 'true')
stop ()
*
* Merge fractioned scratches via morphology
* 联合
union1 (SelectedRegions, RegionUnion)
* 膨胀
dilation_circle (RegionUnion, RegionDilation, 3.5)
dev_display (Image)
dev_display (RegionDilation)
Message := 'Region of the scratches after dilation'
disp_message (WindowHandleZoom, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandleZoom, 'black', 'true')
stop ()
*提取骨架
skeleton (RegionDilation, Skeleton)
* 形成单独的连通域
connection (Skeleton, Errors)
dev_set_colored (12)
dev_display (Image)
dev_display (Errors)
Message := 'Fractioned scratches merged via morphology'
disp_message (WindowHandleZoom, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandleZoom, 'black', 'true')
stop ()
*
* Distinguish small and large scratches
close_zoom_window (WindowHandleZoom, Width, Height)
* 筛选出区域面积(大划痕)
select_shape (Errors, Scratches, 'area', 'and', 50, 10000)
select_shape (Errors, Dots, 'area', 'and', 1, 50)
* 小划痕
dev_display (Image)
dev_set_color ('red')
dev_display (Scratches)
dev_set_color ('blue')
dev_display (Dots)
Message := 'Extracted surface scratches'
Message[1] := 'Not categorized as scratches'
disp_message (WindowID, Message, 'window', 440, 310, ['red','blue'], 'true')
Halcon 频域+空间域检测划痕
* This program shows how to detect defects (scratches) in
* an inhomogeneously illuminated surface by filtering in
* the frequency domain.
* First, a suitable bandpass filter is created. Then, the
* input image is fourier transformed and filtered in the
* frequency domain, so that high frequency information is
* enhanced. Finally, it is transformed back to the
* spatial domain and the enhanced defects are post-processed
* by morphology.
* 1.采集图像
dev_update_off ()
dev_close_window ()
read_image (Image, 'surface_scratch')
* 图像翻转(暗点变成亮点,亮点变成暗点)
invert_image (Image, ImageInverted)
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_display (Image)
*
* Optimize the speed of the fast fourier transform
* Message := 'Optimize the speed of the fast fourier transform.'
* Message[1] := 'Please wait...'
* disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
* optimize_rft_speed (Width, Height, 'standard')
* disp_continue_message (WindowHandle, 'black', 'true')
* stop ()
* 2.频域变换
* Enhance the scratches by filtering in the frequency domain
* 形成一个正弦滤波器 (可以移到中间)
*gen_sin_bandpass (ImageBandpass, 0.4, 'none', 'rft', Width, Height)
gen_sin_bandpass(ImageBandpass, 0.4, 'none', 'dc_center', Width, Height)
*rft_generic (ImageInverted, ImageFFT, 'to_freq', 'none', 'complex', Width)
fft_generic(ImageInverted, ImageFFT, 'to_freq', -1, 'none', 'dc_center', 'complex')
* 卷积
convol_fft (ImageFFT, ImageBandpass, ImageConvol)
fft_generic(ImageConvol, Lines, 'from_freq', 1, 'n', 'dc_center', 'byte')
*rft_generic (ImageConvol, Lines, 'from_freq', 'n', 'byte', Width)
*
* Segment the scratches by using morphology
* blob 分析
threshold (Lines, Region, 5, 255)
connection (Region, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 5, 5000)
dilation_circle (SelectedRegions, RegionDilation, 5.5)
union1 (RegionDilation, RegionUnion)
reduce_domain (Image, RegionUnion, ImageReduced)
* 获取线
lines_gauss (ImageReduced, LinesXLD, 0.8, 3, 5, 'dark', 'false', 'bar-shaped', 'false')
* 共线联合
union_collinear_contours_xld (LinesXLD, UnionContours, 40, 3, 3, 0.2, 'attr_keep')
select_shape_xld (UnionContours, SelectedXLD, 'contlength', 'and', 15, 1000)
* xld 转区域
gen_region_contour_xld (SelectedXLD, RegionXLD, 'filled')
union1 (RegionXLD, RegionUnion)
dilation_circle (RegionUnion, RegionScratches, 10.5)
*
* Display the results
dev_set_draw ('margin')
dev_set_line_width (3)
dev_set_colored (12)
dev_display (Image)
dev_display (RegionScratches)
Halcon 傅里叶变换转换为功率图(频域+blob+差分)
* This program shows how to separate foreground information
* from a disturbing background texture by filtering in the
* frequency domain.
* First, the image is fourier transformed in order to obtain
* its frequency spectrum. Then, we detect the frequency peaks
* corresponding to the disturbing background texture in the
* frequency spectrum. Afterwards, a filter which eliminates those
* frequencies is built and applied to the spectrum. By applying
* the inverse fourier transform to the filtered spectrum, we
* obtain a filtered image from which the disturbing background
* texture was removed.
*
dev_update_off ()
dev_close_window ()
Scale := [1.0,.65]
MinGray := [50,100]
for Index := 0 to 1 by 1
*
* Read and display the image
read_image (Image, 'plan_' + (Index + 1)$'02')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width * Scale[Index], Height * Scale[Index], 'black', WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
dev_set_part (0, 0, Height - 1, Width - 1)
dev_display (Image)
disp_message (WindowHandle, 'Original image', 'window', 12, 12, 'black', 'true')
*
* Perform fft and display spectrum
* 优化速度
optimize_fft_speed (Width, Height, 'standard')
*
* We used 'fft_generic' 'sqrt' and 'dc_center' mainly
* for visualization purposes.
* To speed up the program, rft_generic should be used;
* but of course, the peak detection algorithm has to be
* adjusted in this case.
fft_generic (Image, ImageFFT, 'to_freq', -1, 'sqrt', 'dc_center', 'complex')
dev_open_window (0, Width * Scale[Index] + 7, Width * Scale[Index], Height * Scale[Index], 'black', WindowHandle1)
dev_set_color ('red')
dev_set_draw ('margin')
set_display_font (WindowHandle1, 14, 'mono', 'true', 'false')
dev_set_part (0, 0, Height - 1, Width - 1)
dev_display (ImageFFT)
disp_message (WindowHandle1, 'Fourier spectrum', 'window', 12, 12, 'black', 'true')
disp_cont_message (WindowHandle1, 'black', 'true')
stop ()
*
* Detect the eight most significant peaks in the spectrum
* 转换为功率图
power_real (ImageFFT, PowerSpectrum)
* 低通滤波
binomial_filter (PowerSpectrum, ImageSmooth, 9, 9)
* 二值化
threshold (ImageSmooth, Region, MinGray[Index], 100000)
* 形成单个连通域
connection (Region, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 5, 200)
union1 (SelectedRegions, RegionUnion)
reduce_domain (ImageSmooth, RegionUnion, ImageReduced)
* 获取局部最大值
local_max (ImageReduced, LocalMaxima)
*
* Next, detect peaks one octave higher, i.e., at twice
* the frequency of the most significant peaks
* 形成一个包凸
shape_trans (LocalMaxima, RegionTrans, 'convex')
* Construct ROI band at twice the frequency
* 形成一个缩放矩阵的仿射变换
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_scale (HomMat2DIdentity, 2.1, 2.1, Height / 2, Width / 2, HomMat2DScale)
affine_trans_region (RegionTrans, RegionTrans1, HomMat2DScale, 'nearest_neighbor')
hom_mat2d_scale (HomMat2DIdentity, 1.9, 1.9, Height / 2, Width / 2, HomMat2DScale)
affine_trans_region (RegionTrans, RegionTrans2, HomMat2DScale, 'nearest_neighbor')
* 进行差分
difference (RegionTrans1, RegionTrans2, RegionDifference)
* Extract the peaks at twice the frequency
* 差分区域进行裁剪
reduce_domain (ImageSmooth, RegionDifference, ImageReduced)
threshold (ImageReduced, Region, 15, 100000)
reduce_domain (ImageSmooth, Region, ImageReduced)
* 获取局部最大值
local_max (ImageReduced, LocalMaxima2)
*
* Merge the peaks of both octaves and enlarge them to
* integrate the relevant frequencies into the filter
* 将之前的到的区域和后面的区域进行联合
union2 (LocalMaxima, LocalMaxima2, RegionUnion)
* 膨胀处理
dilation_circle (RegionUnion, RegionDilation, 15.5)
* 将获取的区域得到填充
paint_region (RegionDilation, ImageFFT, ImageFFTFiltered, 0, 'fill')
dev_display (ImageFFT)
dev_display (RegionDilation)
disp_message (WindowHandle1, 'Frequencies of the\nbackground texture', 'window', 12, 12, 'black', 'true')
disp_cont_message (WindowHandle1, 'black', 'true')
stop ()
*
* Apply the filter and display the results
* 反傅里叶变换
fft_generic (ImageFFTFiltered, ImageFiltered, 'from_freq', 1, 'sqrt', 'dc_center', 'byte')
dev_display (ImageFiltered)
disp_message (WindowHandle1, 'Filtered image', 'window', 12, 12, 'black', 'true')
*
dev_open_window (0, 2 * (Width * Scale[Index]) + 14, Width * Scale[Index], Height * Scale[Index], 'black', WindowHandle2)
set_display_font (WindowHandle2, 14, 'mono', 'true', 'false')
dev_set_part (0, 0, Height - 1, Width - 1)
sub_image (Image, ImageFiltered, ImageTexture, 1, 128)
dev_display (ImageTexture)
disp_message (WindowHandle2, 'Removed texture', 'window', 12, 12, 'black', 'true')
if (Index < 1)
disp_cont_message (WindowHandle2, 'black', 'true')
stop ()
dev_close_window ()
dev_close_window ()
dev_close_window ()
endif
endfor
Halcon 训练学习缺陷检测(以神经网络mlp为例)
create_class_mlp 创建
add_samples_image_class_mlp添加样本
set_rejection_params_class_mlp 设置拒绝参数
train_class_mlp 训练
write_class_mlp 保存
classify_image_class_mlp 识别
* This example program shows how to use the MLP classifier for novelty
* detection to perform a web inspection task. To perform the novelty detection,
* a rejection class is trained internally.
* For the web inspection task, the MLP can subsequently be used to detect
* textures that do not correspond to the texture of the trained good objects.
* 1.采集图像
dev_update_off ()
*
ReadPretrainedClassifier := false
* Uncomment the following line to read the pretrained classifier from
* disk. The training may last up to half a minute.
* ReadPretrainedClassifier := true
SaveClassifier := false
* Uncomment the following line to write the MLP classifier to disk after training.
* SaveClassifier := true
*
read_image (Image, 'plastic_mesh/plastic_mesh_01')
get_image_size (Image, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_set_color ('red')
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
* 获取系统信息
get_system ('example_dir', HalconExamples)
* The texture filters used for the classification will return artifacts at the image
* borders because the images of the plastic mesh to be inspected do not
* contain an integer number of mesh cells. Because this would lead to wrongly
* detected errors at the image borders, we must exclude the area close to the
* image border from the training and classification. This is done with the following
* rectangle. Note that the image is later scaled down by a factor of two.
* 产生矩形窗口
gen_rectangle1 (Rectangle, 10, 10, Height / 2 - 11, Width / 2 - 11)
* 如果没有分类器则创建分类器
if (ReadPretrainedClassifier)
* Read the pretrained classifier from disk.
dev_display (Image)
disp_message (WindowHandle, 'Reading classifier from disk...', 'window', 10, 10, 'black', 'true')
read_class_mlp (HalconExamples + '/hdevelop/Segmentation/Classification/novelty_detection.gmc', MLPHandle)
wait_seconds (1.5)
else
* Create the MLP classifier.
* 创建分类器
* 第一个参数为特征变量的数量(和通道数对应-> gen_texture_image (ImageZoomed, ImageTexture))
create_class_mlp (5, 9, 2, 'softmax', 'principal_components', 3, 42, MLPHandle)
* The training is based on five images that contain no errors.
* 形成一个空的区域
gen_empty_region (EmptyRegion)
* 将Rectangle和EmptyRegion 放入到 ObjectsConcat 中
concat_obj (Rectangle, EmptyRegion, ObjectsConcat)
for J := 1 to 5 by 1
read_image (Image, 'plastic_mesh/plastic_mesh_' + J$'02')
* The images are zoomed down because the resolution of the mesh is very
* high. This saves a large amount of processing time.
* 等比例缩放图片
zoom_image_factor (Image, ImageZoomed, 0.5, 0.5, 'constant')
dev_display (ImageZoomed)
disp_message (WindowHandle, 'Adding training samples...', 'window', 10, 10, 'black', 'true')
* Generate the texture image.
* 形成一个纹理图像
gen_texture_image (ImageZoomed, ImageTexture)
* Add the samples to the classifier.
* 添加样本
add_samples_image_class_mlp (ImageTexture, ObjectsConcat, MLPHandle)
endfor
dev_display (ImageZoomed)
* Now configure the MLP that a rejection class will be added during training.
* 设置不符合要求的参数(必须要设置)
set_rejection_params_class_mlp (MLPHandle, 'sampling_strategy', 'hyperbox_ring_around_each_class')
set_rejection_params_class_mlp (MLPHandle, 'rejection_sample_factor', .3)
* Train the MLP.
disp_message (WindowHandle, 'Training MLP...', 'window', 10, 10, 'black', 'true')
* 训练
train_class_mlp (MLPHandle, 200, 1, 0.01, Error, ErrorLog)
if (SaveClassifier)
*保存分类器
write_class_mlp (MLPHandle, HalconExamples + '/hdevelop/Segmentation/Classification/novelty_detection.gmc')
endif
endif
* Now detect errors in the plastic meshes.
dev_set_draw ('margin')
dev_set_line_width (3)
for J := 1 to 14 by 1
read_image (Image, 'plastic_mesh/plastic_mesh_' + J$'02')
zoom_image_factor (Image, ImageZoomed, 0.5, 0.5, 'constant')
dev_display (ImageZoomed)
dev_set_color ('white')
dev_display (Rectangle)
gen_texture_image (ImageZoomed, ImageTexture)
reduce_domain (ImageTexture, Rectangle, ImageTextureReduced)
* Classify samples belonging to the trained class with the MLP.
* 识别
classify_image_class_mlp (ImageTextureReduced, ClassRegions, MLPHandle, 0.5)
* Post process the returned raw errors to remove insignificant parts of the
* detected errors.
select_obj (ClassRegions, Correct, 1)
select_obj (ClassRegions, Errors, 2)
opening_circle (Errors, ErrorsOpening, 2.5)
closing_circle (ErrorsOpening, ErrorsClosing, 12.5)
connection (ErrorsClosing, ErrorsConnected)
* 筛选出缺陷
select_shape (ErrorsConnected, FinalErrors, 'area', 'and', 20, 1000000)
* 缺陷放入容器
count_obj (FinalErrors, NumErrors)
dev_set_color ('red')
dev_display (FinalErrors)
if (NumErrors > 0)
disp_message (WindowHandle, 'Mesh not OK', 'window', 10, 10, 'red', 'true')
else
disp_message (WindowHandle, 'Mesh OK', 'window', 10, 10, 'forest green', 'true')
endif
if (J < 14)
disp_continue_message (WindowHandle, 'black', 'true')
endif
stop ()
endfor
产生滤波算子
gen_texture_image
* The texture image is a five-channel image that contains the result of applying
* five different Laws filters, which basically correspond to first and second
* derivatives, and smoothing them sufficiently.
* 纹理滤波
texture_laws (Image, ImageEL, 'el', 5, 5)
texture_laws (Image, ImageLE, 'le', 5, 5)
texture_laws (Image, ImageES, 'es', 1, 5)
texture_laws (Image, ImageSE, 'se', 1, 5)
texture_laws (Image, ImageEE, 'ee', 2, 5)
* 合成一个通道
compose5 (ImageEL, ImageLE, ImageES, ImageSE, ImageEE, ImageLaws)
smooth_image (ImageLaws, ImageTexture, 'gauss', 3)
return ()