Halcon 频域缺陷检测

文章目录

  • 傅里叶变换频谱
    • 矩形
    • 菱形
    • 黑白相间的亮带
    • 去除图纹(反傅里叶变换)
    • 去除图纹滤波器处理
  • 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-Pass Filter):低通滤波器可以去除图像中高频部分的信息,保留低频部分的信息。在频域上看,低通滤波器会将图像高频成分的能量减弱,从而产生模糊的效果。低通滤波器一般用于平滑图像、去除噪声等应用场景。

    高通滤波器(High-Pass Filter):高通滤波器可以去除图像中低频部分的信息,保留高频部分的信息。在频域上看,高通滤波器会将图像低频成分的能量减弱,从而使高频细节更加突出。高通滤波器一般用于图像增强、边缘检测等应用场景。

    中通滤波器(Band-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 ()

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/384750.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

C++实现二分查找

目录 例1 例2 例3 例4 例5 例6 例1 704. 二分查找 注意&#xff1a; ①left < right,这里的号是最后一次通过下标mid来判断 ②在偶数的时候mid&#xff0c;左右无所谓&#xff0c;因为left和right都有1&#xff1b; 参考代码 class Solution { public:int search…

【selenium】

selenium是一个Web的自动化测试工具&#xff0c;最初是为网站自动化测试而开发的。Selenium可以直接调用浏览器&#xff0c;它支持所有主流的浏览器。其本质是通过驱动浏览器&#xff0c;完成模拟浏览器操作&#xff0c;比如挑战&#xff0c;输入&#xff0c;点击等。 下载与打…

C++ Qt框架开发 | 基于Qt框架开发实时成绩显示排序系统(2)折线图显示

对上一篇的工作C学习笔记 | 基于Qt框架开发实时成绩显示排序系统1-CSDN博客继续优化&#xff0c;增加一个显示运动员每组成绩的折线图。 1&#xff09;在Qt Creator的项目文件&#xff08;.pro文件&#xff09;中添加对Qt Charts模块的支持&#xff1a; QT charts 2&#xf…

用HTML5 + JavaScript绘制花、树

用HTML5 JavaScript绘制花、树 <canvas>是一个可以使用脚本 (通常为JavaScript) 来绘制图形的 HTML 元素。 <canvas> 标签/元素只是图形容器&#xff0c;必须使用脚本来绘制图形。 HTML5 canvas 图形标签基础https://blog.csdn.net/cnds123/article/details/112…

opencv 图像色彩空间转化

今天看了b站贾志刚的课&#xff0c;觉得不错&#xff0c;特地做学习笔记来和小伙伴分享 贾志刚的这个好像是2.0版本,30小时的,语言更加精炼,适合初级入门学习 第一节是常规安装 看他的步骤装就行了,记得配置完点应用再点确定,我第一次就是 没点然后就失败了,又得重配置一次…

服务网格(Service Mesh)流行工具

在这篇博客中&#xff0c;我们将介绍微服务的最佳服务网格工具列表&#xff0c;这些工具提供安全性、金丝雀部署、遥测、负载均衡等。 用于部署和操作微服务的服务网格工具的数量不断增加。在这篇文章中&#xff0c;我们将探讨您应该用来构建自己的服务网格架构的顶级服务网格…

视觉开发板—K210自学笔记(五)

本期我们来遵循其他单片机的学习路线开始去用板子上的按键控制点亮LED。那么第一步还是先知道K210里面的硬件电路是怎么连接的&#xff0c;需要查看第二节的文档&#xff0c;看看开发板原理图到底是按键是跟哪个IO连在一起。然后再建立输入按键和GPIO的映射就可以开始变成了。 …

PHP特性知识点总结

description: 专门出的关于php的特性比较,后面好像也有java的特性。 大家直接去我的gitbook或者github看就能看到图片,这里就懒得把他弄到csdn上了。 这里放github和gitbook的链接,大家跳转就可以。gitbook链接用国内的网就能访问。 gitbook: http://22kaka.fun github:htt…

STM32 + ESP8266,连接阿里云 上报/订阅数据

&#xff08;文章正在编辑中&#xff0c;一点点地截图操作过程&#xff0c;估计要拖拉两三天&#xff09; 一、烧录MQTT固件 ESP8266出厂时&#xff0c;默认是AT固件。连接阿里云&#xff0c;需要使用MQTT固件。 1、独立EPS8266模块的烧录方法 2、魔女开发板&#xff0c;板载…

HTTP协议-请求Request

前言&#xff1a; 序列&#xff1a;HTTP - 002 1.请求格式 1.1标椎格式 HTTP请求是字符串的格式传输&#xff0c;具体包含以下四部分&#xff1a; 首行&#xff1a;[方法][url][版本号]&#xff0c;分别使用空格分隔&#xff1b;请求头&#xff08;Header&#xff09;&#…

apk反编译修改教程系列---简单修改apk默认横竖屏显示 手机端与电脑端同步演示【十一】

往期教程&#xff1a; apk反编译修改教程系列-----修改apk应用名称 任意修改名称 签名【一】 apk反编译修改教程系列-----任意修改apk版本号 版本名 防止自动更新【二】 apk反编译修改教程系列-----修改apk中的图片 任意更换apk桌面图片【三】 apk反编译修改教程系列---简单…

《UE5_C++多人TPS完整教程》学习笔记8 ——《P9 访问 Steam(Acessing Steam)》

本文为B站系列教学视频 《UE5_C多人TPS完整教程》 —— 《P9 访问 Steam&#xff08;Acessing Steam&#xff09;》 的学习笔记&#xff0c;该系列教学视频为 Udemy 课程 《Unreal Engine 5 C Multiplayer Shooter》 的中文字幕翻译版&#xff0c;UP主&#xff08;也是译者&…

[JavaWeb玩耍日记]Maven的安装与使用

目录 一.作用 二.安装 三.使用 2.对项目使用compile命令进行编译,看看新的文件会在哪里产生&#xff1f; 3.需要认识的命令 4.Maven对项目执行不同命令的生命周期特点&#xff1f; 5.如何导入工程外的Maven&#xff1f; 6.如何直观地查看Maven导入了哪些工程或哪些jar包…

Stream流学习笔记

Stream流 创建流中间操作1、filter2、map3、distinct4、sorted5、limit6、skip7、flatMap 终结操作1、forEach2、count3、max&min4、collect5、查找与匹配 创建流 单例集合&#xff1a;集合对象.stream() List<Integer> list new ArrayList<>(); Stream<…

前端JavaScript篇之Promise解决了什么问题、Promise.all和Promise.race的区别的使用场景

目录 Promise解决了什么问题Promise.all和Promise.race的区别的使用场景 Promise解决了什么问题 Promise 解决了 JavaScript 中回调地狱的问题。在传统的回调函数中&#xff0c;如果需要依次执行多个异步操作&#xff0c;就需要使用嵌套的回调函数&#xff0c;这样会导致代码难…

如何学习VBA_3.2.14:VBA中字符串的处理和判断函数

我给VBA的定义&#xff1a;VBA是个人小型自动化处理的有效工具。利用好了&#xff0c;可以大大提高自己的劳动效率&#xff0c;而且可以提高数据处理的准确度。我推出的VBA系列教程共九套和一部VBA汉英手册&#xff0c;现在已经全部完成&#xff0c;希望大家利用、学习。 如果…

EasyCaptcha,开源图形验证码新标杆!

引言&#xff1a; 随着互联网的普及&#xff0c;验证码已成为网站和应用程序中不可或缺的安全组件。它能够有效地防止自动化攻击、垃圾邮件和机器人活动。在众多验证码解决方案中&#xff0c;Easy-captcha以其简单易用和高度可定制的特点受到了开发者的青睐。本文将指导读者如…

leetcode:买卖股票最佳时机二

思路&#xff1a; 使用贪心算法&#xff1a;局部最优是将买卖过程中产生的正数进行相加&#xff0c;进而使得最后结果最大&#xff08;全局最优&#xff09;。 price [7,1,5,10,3,6,4] -6,4,5,-7,3,-2 正数相加就得到了最大 代码实现&#xff1a; 1.循环中下标从1开始 …

unity2017 遇到visual studio 2017(社区版) 30日试用期到了

安装unity2017 遇到visual studio 2017 30日试用期到了&#xff0c;网上百度搜了好多方法都没有成功。 最后用了这个方法&#xff1a; 1)启动vs2017&#xff0c;在弹出要登录的窗口之前&#xff0c;迅速的点击工具-》选项-》账户&#xff0c;勾选在添加账户或对账户重新进行身…

【技巧】Allegro实用技巧之模块复用

需求分析&#xff1a;使用Allegro软件进行PCB Layout设计时&#xff0c;当电路图中有很多路相同的模块&#xff0c;使用模块复用的的操作方法&#xff0c;可以显著提高工作效率&#xff0c;同时也可以使PCB布局在整体上显得美观。下面来讲述这个方法。 具体方法及说明&#xf…