用快速傅里叶 提取背景纹理 (halcon)

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', 'image', 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', 'image', 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, 'false')
    hom_mat2d_scale (HomMat2DIdentity, 1.9, 1.9, Height/2, Width/2, HomMat2DScale)
    affine_trans_region (RegionTrans, RegionTrans2, HomMat2DScale, 'false')
    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', 'image', 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', 'image', 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)

用快速傅里叶 提取背景纹理 (halcon)_第1张图片

原图

用快速傅里叶 提取背景纹理 (halcon)_第2张图片

滤波后处理结果

你可能感兴趣的:(halcon)