用MATLAB实现频域平滑滤波以及图像去噪代码

用MATLAB实现频域平滑滤波以及图像去噪代码,第1张

%%%%%%%%spatial frequency (SF) filtering by low pass filter%%%%%%%%

% the SF filter is unselective to orientation (doughnut-shaped in the SF

% domain)

[FileName,PathName,FilterIndex] = uigetfile ;

filename = fullfile(PathName, FileName) ;

[X map] = imread(filename, fmt); % read image

L = double(X); % transform to double

%%%%%%%%%%%%% need to add (-1)x+y to L

% calculate the number of points for FFT (power of 2)

fftsize = 2 ^ ceil(log2(size(L)));

% 2d fft

Y = fft2(X, fftsize(1), fftsize (2));

Y = fftshift(Y);

% obtain frequency (cycles/pixel)

f0 = floor([m n] / 2) + 1;

fy = ((m: -1: 1) - f0(1) + 1) / m;

fx = ((1: n) - f0(2)) / n;

[mfx mfy] = meshgrid(fx, fy);

% calculate radius

SF = sqrt(mfx ^ 2 + mfy ^ 2);

% SF-bandpass and orientation-unselective filter

filt = SF > k0;

A_filtered = filt A; % SF filtering

L_filtered = real(ifft2(ifftshift(A_filtered))); % IFFT

L_filtered = L_filtered(1: size(L, 1), 1: size(L, 2));

%%%%%%%%%%need to add (-1)x + y to L_filtered

% show

figure(1);

clf reset;

colormap gray;

% plot image

subplot(2, 2, 1);

imagesc(L);

colorbar;

axis square;

set(gca, 'TickDir', 'out');

title('original image');

xlabel('x');

ylabel('y');

imwrite(L, fullfile(FilePath, 'original imagebmp'), 'bmp') ;

% plot amplitude

A = abs(A);

A = log10(A);

% spectral amplitude

subplot(2, 2, 2);

imagesc(fx, fy, A);

axis xy;

axis square;

set(gca, 'TickDir', 'out');

title('amplitude spectrum');

xlabel('fx (cyc/pix)');

ylabel('fy (cyc/pix)');

imwrite(A, fullfile(FilePath, 'amplitude spectrumbmp'), 'bmp') ;

% filter in the SF domain

subplot(2, 2, 3);

imagesc(fx, fy, filt);

axis xy;

axis square;

set(gca, 'TickDir', 'out');

title('filter in the SF domain');

xlabel('fx (cyc/pix)');

ylabel('fy (cyc/pix)');

imwrite(filt, fullfile(FilePath, 'filter in SFbmp'), 'bmp') ;

% filtered image

subplot(2, 2, 4);

imagesc(L_filtered);

colorbar;

axis square;

set(gca, 'TickDir', 'out');

title('filtered image');

xlabel('x');

ylabel('y');

imwrite(filtered, fullfile(FilePath, 'filtered imagebmp'), 'bmp');

%%%%%%%%%%%%%%%%%median filter%%%%%%%%%%%%%%%%

[FileName,PathName,FilterIndex] = uigetfile ;

filename = fullfile(PathName, FileName) ;

[LNoise map] = imread(filename, fmt); % read image

L = medfilt2(LNoise, [3 3]); % remove the noise with 33 block

figure ;

imshow(LNoise) ;

title('image before fitlering') ;

figure

imshow(L)

title('filtered image') ;

imwrite(FilePath, 'filtered imagebmp', bmp)

在数字图像中,高频信号是指图像变化剧烈的部分,例如边缘、噪声等;低频信号是指图像中像素值变化平缓的部分。

所谓低通就是留下低频信号,滤掉高频信号。

在空间域,低通滤波主要是指平滑滤波,是用周围像素的值求平均或加权平均,得到一个值,赋给当前位置。

实现上来讲,就是取一个像素点,用一个33或更大的矩阵依次与这一像素点周围的邻域对应相乘,乘完的结果加起来,将这个结果赋给这个像素点,然后再对下一个像素点进行这样的计算,直到整幅图像算完。

其结果就是图像变模糊了,边缘变得不清楚。

'中值滤波:

Dim x As Integer, y As Integer, size As Integer = 3, point(8) As Integer

For x = CInt((size - 1) / 2) To CInt(jpgImageWidth - 1 - (size - 1) / 2)

For y = CInt((size - 1) / 2) To CInt(jpgImageHeight - 1 - (size - 1) / 2)

point(0) = jpgImageGetPixel(x - 1, y - 1)R

point(1) = jpgImageGetPixel(x - 1, y)R

point(2) = jpgImageGetPixel(x - 1, y + 1)R

point(3) = jpgImageGetPixel(x, y - 1)R

point(4) = jpgImageGetPixel(x, y)R

point(5) = jpgImageGetPixel(x, y + 1)R

point(6) = jpgImageGetPixel(x + 1, y - 1)R

point(7) = jpgImageGetPixel(x + 1, y)R

point(8) = jpgImageGetPixel(x + 1, y + 1)R

ArraySort(point)

jpgImageSetPixel(x, y, ColorFromArgb(point(4), point(4), point(4)))

Next

Next

这是别人写的一个gabor滤波器的程序,你看看吧!

% GABORFILTER Bi-dimensional Gabor filter with DC component compensation

% [G,GABOUT]=GABORFILTER(I,S,F,W,P) filters the input image I with the 2D

% Gabor filter G described by the parameters S, F, W and P to create the

% output filtered image GABOUT

% This version of the 2D Gabor filter is basically a bi-dimensional

% Gaussian function centered at origin (0,0) with variance S modulated by

% a complex sinusoid with polar frequency (F,W) and phase P described by

% the following equation:

%

% G(x,y,S,F,W,P)=kGaussian(x,y,S)(Sinusoid(x,y,F,W,P)-DC(F,S,P)),

% where:

% Gaussian(x,y,S)=exp(-piS^2(x^2+y^2))

% Sinusoid(x,y,F,W,P)=exp(j(2piF(xcos(W)+ysin(W))+P)))

% DC(F,S,P)=exp(-pi(F/S)^2+jP)

%

% PS: The term DC(F,S,P) compensates the inherent DC component produced

% by the Gaussian envelop as shown by Movellan in [1]

%

% Tips:

% 1) To get the real part and the imaginary part of the complex

% filter output use real(gabout) and imag(gabout), respectively;

%

% 2) To get the magnitude and the phase of the complex filter output

% use abs(gabout) and angle(gabout), respectively

% Author: Stiven Schwanz Dias e-mail: stivendias@gmailcom

% Cognition Science Group, Informatic Department,

% University of Esp韗ito Santo, Brazil, January 2007

%

% References:

% [1] Movellan, J R - Tutorial on Gabor Filters Tech rep, 2002

function [G,GABOUT]=gaborfilter(I,S,F,W,P);

if isa(I,'double')~=1

I=double(I);

end

size=fix(15/S); % exp(-15^2pi) < 01%

%k=2piS^2;

%F=S^2/sqrt(2pi);

k=1;

for x=-size:size

for y=-size:size

G(size+x+1,size+y+1)=kexp(-piS^2(xx+yy))

(exp(j(2piF(xcos(W)+ysin(W))+P))-exp(-pi(F/S)^2+jP));

end

end

GABOUT=conv2(I,double(G),'same');

以上就是关于用MATLAB实现频域平滑滤波以及图像去噪代码全部的内容,包括:用MATLAB实现频域平滑滤波以及图像去噪代码、数字图像处理 空间域低通滤波原理、vb.net 实现高斯滤波\中值滤波\均值滤波的一种 需求vb.net程序,实现对图像的滤波处理,以上任意一种均可等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/10111256.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-05
下一篇 2023-05-05

发表评论

登录后才能评论

评论列表(0条)

保存