Halcon 频域缺陷检测

news2024/11/23 21:28:48

文章目录

  • 傅里叶变换频谱
    • 矩形
    • 菱形
    • 黑白相间的亮带
    • 去除图纹(反傅里叶变换)
    • 去除图纹滤波器处理
  • 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 ()

在这里插入图片描述

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

leetcode 461. 汉明距离

比较简单的一题&#xff0c;先对两个整数进行异或操作&#xff0c;会将两个整数二进制形式中各个数字进行异或操作&#xff0c;不同的数字则为1&#xff0c;再通过移位操作统计得到的二进制数中为1的个数&#xff0c;即为所求。 Java代码如下&#xff1a; class Solution {pub…

例38:使用Frame(分组框)

建立一个EXE工程&#xff0c;在窗体上放两个Frame框。分别放两组单选按钮表示性别和收入&#xff0c;注意每组单选按钮的组名要一样。在按钮中输入代码&#xff1a; Sub Form1_Command1_BN_Clicked(hWndForm As hWnd, hWndControl As hWnd)If Frame1.Visible ThenFrame1.Visib…

车载诊断协议DoIP系列 —— 协议中术语解释和定义

车载诊断协议DoIP系列 —— 协议中术语解释和定义 我是穿拖鞋的汉子,魔都中坚持长期主义的汽车电子工程师(Wechat:gongkenan2013)。 老规矩,分享一段喜欢的文字,避免自己成为高知识低文化的工程师: 本就是小人物,输了就是输了,不要在意别人怎么看自己。江湖一碗茶,…

EXCEL中如何调出“数据分析”的菜单

今天发现&#xff0c;原来WPS还是和EXCEL比&#xff0c;还是少了“数据分析”这个日常基本做统计的菜单&#xff0c;只好用EXCEL了&#xff0c;但奇怪发现我的EXCEL中没发现这个菜单&#xff0c;然后查了下&#xff0c;才发现&#xff0c;要用如下的方法打开&#xff1a; 1&…

基于Java (spring-boot)的电影院管理系统

一、项目介绍 基于Java (spring-boot)的电影院管理系统功能&#xff1a;管理员登录、用户注册、用户登录、用户、影片介绍、购票、选坐、支付、我的订单、影片、榜单、关于、后台首页、影院信息管理、电影信息管理、电影类别管理、影厅信息管理、场次信息管理、订单信息管理、用…

Solidworks:从2D走向3D

Sokidworks 的强大之处在于三维实体建模&#xff0c;这个形状看似复杂&#xff0c;实际上只需要拉伸一次&#xff0c;再做一次减法拉伸就行了。第一次做三维模型&#xff0c;费了不少时间才搞明白。 接下来做一个稍微复杂一点的模型&#xff0c;和上面这个操作差不多&#xff0…

《SQLi-Labs》05. Less 29~37

title: 《SQLi-Labs》05. Less 29~37 date: 2024-01-17 22:49:10 updated: 2024-02-12 18:09:10 categories: WriteUp&#xff1a;Security-Lab excerpt: HTTP 参数污染&#xff0c;联合注入、宽字节注入。 comments: false tags: top_image: /images/backimg/SunsetClimbing.p…

nodejs爬虫框架

nodejs爬虫框架 在Node.js中&#xff0c;有一些常用的爬虫框架可以帮助你实现网页抓取和数据提取的任务。以下是几个流行的Node.js爬虫框架&#xff1a; 1. **Puppeteer**: Puppeteer 是由 Google 开发的一个用于控制 headless Chrome 或 Chromium 浏览器的 Node.js 库。它提供…

【深度学习 目标检测】R-CNN系列算法全面概述(一文搞懂R-CNN、Fast R-CNN、Faster R-CNN的来龙去脉)

&#x1f680;个人主页&#xff1a;为梦而生~ 关注我一起学习吧&#xff01; &#x1f4a1;相关专栏&#xff1a; 深度学习 &#xff1a;现代人工智能的主流技术介绍 机器学习 &#xff1a;相对完整的机器学习基础教学&#xff01; &#x1f4a1;往期推荐&#xff1a; 【机器学…

基于微信小程序的培训机构客户管理系统小程序

摘 要 随着社会的发展&#xff0c;社会的方方面面都在利用信息化时代的优势。互联网的优势和普及使得各种系统的开发成为必需。 本文以实际运用为开发背景&#xff0c;运用软件工程原理和开发方法&#xff0c;它主要是采用java语言技术和mysql数据库来完成对系统的设计。整个开…

秋招上岸大厂,分享一下经验

文章目录 秋招过程学习过程项目经验简历经验面试经验offer选择总结 秋招过程 今天是除夕&#xff0c;秋招已经正式结束了&#xff0c;等春节过完就到了春招的时间点了。 运气比较好&#xff0c;能在秋招的末尾进入一家大厂&#xff0c;拿到20k的sp offer。 从九月份十月份就开…

MYSQL学习笔记:mysql运算符

MYSQL学习笔记&#xff1a;mysql运算符 select * from user where score in (99,100); select * from user where name like zhang%;通配符放到后面或者中间是可以利用索引的&#xff0c;但是通配符放到开头没法用到索引

2024年湖南省考报名时间及流程,选岗很重要!

注册时间&#xff1a;2024年2月18日9:00-25日17:00 报名时间&#xff1a;2024年2月19日9:00-25日17:00 网上确认时间&#xff1a;2024年2月28日9:00-3月2日24:00 缴费时间&#xff1a;2024年2月28日9:00-3月2日24:00 打印准考证时间&#xff1a;2024年3月11日9:00-15日17:00 考…

团队配置管理规范浅见

在一段时间的工作过程中配置管理工作确实对我们的生产活动产生了巨大的工作量&#xff0c;现在就这个工作来进行梳理一下。 本文主要分为两部分&#xff1a; 1、借用软件系统分析师的配置管理部分内容来介绍配置管理的工作&#xff08;原谅时间精力有限&#xff0c;原文基本已…

ctfshow-php特性(web102-web115)

目录 web102 web103 web104 web105 web106 web107 web108 web109 web110 web111 web112 web113 web114 web115 实践是检验真理的 要多多尝试 web102 <?php highlight_file(__FILE__); $v1$_POST[V1]; $v2$_GET[v2]; $v3$_GET[v3]; $v4is_numeric($v2)and is…

就业|高校毕业生就业信息小程序|基于微信小程序的高校毕业生就业信息的设计与实现(源码+数据库+文档)

高校毕业生就业信息小程序目录 目录 基于微信小程序的高校毕业生就业信息的设计与实现 一、前言 二、系统功能设计 三、系统实现 1、用户小程序模块 2、用户信息管理 2、职位招聘管理 3、公司信息管理 4、论坛信息管理 四、数据库设计 1、实体ER图 五、核心代码 …

【十六】【C++】stack的常见用法和练习

stack的常见用法 C标准库中的stack是一种容器适配器&#xff0c;它提供了后进先出&#xff08;Last In First Out, LIFO&#xff09;的数据结构。stack使用一个底层容器进行封装&#xff0c;如deque、vector或list&#xff0c;但只允许从一端&#xff08;顶部&#xff09;进行…

C++ //练习 6.4 编写一个与用户交互的函数,要求用户输入一个数字,计算生成该数字的阶乘。在main函数中调用该函数。

C Primer&#xff08;第5版&#xff09; 练习 6.4 练习 6.4 编写一个与用户交互的函数&#xff0c;要求用户输入一个数字&#xff0c;计算生成该数字的阶乘。在main函数中调用该函数。 环境&#xff1a;Linux Ubuntu&#xff08;云服务器&#xff09; 工具&#xff1a;vim 代…

分析“e^iπ+1=0”的错谬及其违反数学规则

如果评选从远古到现代对人类智商羞辱最严重的事件&#xff0c;欧拉公式“e^iπ-1”若说第二、就没有哪个能称第一。 看下面罗列的关系&#xff0c;数学伦理在大数学家欧拉眼里形同虚设&#xff1a; ①“e^iπ-1”没有代码&#xff0c;不能表示数量变化关系&#xff0c;它来自e^…

【Docker】Docker Container(容器)

文章目录 一、什么是容器&#xff1f;二、为什么需要容器&#xff1f;三、容器的生命周期容器OOM容器异常退出容器暂停 四、容器命令详解docker createdocker logsdocker attachdocker execdocker startdocker stopdocker restartdocker killdocker topdocker statsdocker cont…