Halcom 发表于 2018-12-1 23:27:25

FFT变换去除图像背景纹理

代码如下:
dev_update_off ()
dev_set_draw('margin')
Scale :=
MinGray :=
* Read and display the image
read_image (Image, 'C:/Users/ysw/Desktop/2.jpg')
count_channels(Image, Channels)
if(Channels>1)
    rgb1_to_gray(Image, Image)
endif
get_image_size (Image, Width, Height)
dev_set_part (0, 0, Height - 1, Width - 1)
dev_display (Image)
*
* Perform fft and display spectrum
optimize_fft_speed (Width, Height, 'standard')
*
fft_generic (Image, ImageFFT, 'to_freq', -1, 'sqrt', 'dc_center', 'complex')
dev_set_color ('red')
dev_display (ImageFFT)
*
* Detect the eight most significant peaks in the spectrum
power_real (ImageFFT, PowerSpectrum)
binomial_filter (PowerSpectrum, ImageSmooth, 9, 9)
threshold (ImageSmooth, Region, MinGray, 100000)
connection (Region, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 3, 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, 1.5, 1.5, Height / 2, Width / 2, HomMat2DScale)
affine_trans_region (RegionTrans, RegionTrans1, HomMat2DScale, 'nearest_neighbor')
hom_mat2d_scale (HomMat2DIdentity, 1.3, 1.3, 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, 16, 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)
*
* Apply the filter and display the results
fft_generic (ImageFFTFiltered, ImageFiltered, 'from_freq', 1, 'sqrt', 'dc_center', 'byte')
sub_image (Image, ImageFiltered, ImageTexture, 1, 128)
dev_display (ImageTexture)

(1)关键在于频率成分的选取;规则的纹理,均为低频成分,去掉低频成分即可。
(2)此代码中,关键点在于上、下两个频域成分的去除。

参考链接:https://pan.baidu.com/s/1RpiNyfULyjx7om_ta1LYzQ

页: [1]
查看完整版本: FFT变换去除图像背景纹理