%% 一维Gabor滤波器
clc,clear,close all % 清理命令区、清理工作区、关闭显示图形
warning off % 消除警告
feature jit off % 加速代码运行
x=-4:0.01:4;
t=1;
y=1*exp(-(x.^2)./(sqrt(2*pi)*t^2));
plot(x,y,'b','linewidth',2);
hold on
x1=-4:0.01:4;
y1=1*sin(9*x1+pi/2);
plot(x1,y1,'k','linewidth',1.2);
y2=y.*y1;
plot(x1,y2,'r','linewidth',2)
%% gabor滤波器的实部与虚部图像
clc,clear,close all % 清理命令区、清理工作区、关闭显示图形
warning off % 消除警告
feature jit off % 加速代码运行
x = 0;
theta = 0;
f0 = 0.2;
for i = linspace(-15,15,50)
x = x + 1;
y = 0;
for j = linspace(-15,15,50)
y = y + 1;
z(y,x)=compute_gabor(i,j,f0,theta);
end
end
x = linspace(-15,15,50);
y = linspace(-15,15,50);
surf(x,y,real(z))
title('Gabor filter:实部');
xlabel('x');
ylabel('y');
zlabel('z');
figure(2);
surf(x,y,imag(z))
title('Gabor filter:虚部');
xlabel('x');
ylabel('y');
zlabel('z');
figure(3)
plot(real(z(1,:,:)),'r','linewidth',2)
hold on
plot(imag(z(1,:,:)),'b--','linewidth',2)
grid on
legend('实部','虚部')```
```clike
%% gabor滤波器的实部与虚部图像
clc,clear,close all % 清理命令区、清理工作区、关闭显示图形
warning off % 消除警告
feature jit off % 加速代码运行
x = 0;
theta = 0;
f0 = 0.2;
for i = linspace(-15,15,50)
x = x + 1;
y = 0;
for j = linspace(-15,15,50)
y = y + 1;
z(y,x)=compute_gabor(i,j,f0,theta);
end
end
x = linspace(-15,15,50);
y = linspace(-15,15,50);
surf(x,y,real(z))
title('Gabor filter:实部');
xlabel('x');
ylabel('y');
zlabel('z');
figure(2);
surf(x,y,imag(z))
title('Gabor filter:虚部');
xlabel('x');
ylabel('y');
zlabel('z');
figure(3)
plot(real(z(1,:,:)),'r','linewidth',2)
hold on
plot(imag(z(1,:,:)),'b--','linewidth',2)
grid on
legend('实部','虚部')```
```clike
function gabor_k = compute_gabor(x,y,f0,theta)
r = 1; g = 1;
x1 = x*cos(theta) + y*sin(theta);
y1 = -x*sin(theta) + y*cos(theta);
gabor_k = f0^2/(pi*r*g)*exp(-(f0^2*x1^2/r^2+f0^2*y1^2/g^2))*exp(i*2*pi*f0*x1);
function [G,gabout] = gabor_filter(I,Sx,Sy,U,V)
% 函数输入:
% I : 输入的二维图像
% Sx & Sy : 方差 along x and y-axes respectively
% U & V : 中心频率 along x and y-axes respectively
% 函数输出:
% G : G(x,y)
% gabout : Gabor滤波图像
% G(x,y)表达式如下:
% 1 -1 x ^ y ^
%%% G(x,y) = ---------- * exp ([----{(----) 2+(----) 2}+2*pi*i*(Ux+Vy)])
% 2*pi*sx*sy 2 sx sy
if isa(I,'double')~=1
I = double(I);
end
for x = -fix(Sx):fix(Sx)
for y = -fix(Sy):fix(Sy)
G(fix(Sx)+x+1,fix(Sy)+y+1) = (1/(2*pi*Sx*Sy))*exp(-.5*((x/Sx)^2+(y/Sy)^2)+2*pi*i*(U*x+V*y));
end
end
Imgabout = conv2(I,double(imag(G)),'same'); % 卷积
Regabout = conv2(I,double(real(G)),'same'); % 卷积
gabout = uint8(sqrt(Imgabout.*Imgabout + Regabout.*Regabout)); % 输出滤波图像```
```clike
%% Gabor滤波器
clc,clear,close all % 清理命令区、清理工作区、关闭显示图形
warning off % 消除警告
feature jit off % 加速代码运行
[filename ,pathname]=...
uigetfile({'*.bmp';'*.tif';'*.jpg';},'选择图片'); %选择图片路径
str=[pathname filename]; % 合成路径+文件名
im = imread(str); % 读图
im = imnoise(im,'gaussian',0,1e-3); % 原图像 + 白噪声
figure,
subplot(121),imshow(im);title('原始图像')
colormap(jet) % 颜色
shading interp % 消隐
Sx = 0.5; % x方向的差异系数
Sy = 0.5; % y方向的差异系数
U = 1.0; % x方向的中心频率
V = 1.0; % y方向的中心频率
[G,gabout] = gabor_filter(im,Sx,Sy,U,V);
subplot(122),imshow(gabout);title('Gabor滤波图像')
colormap(jet) % 颜色
shading interp % 消隐
function [G,gabout] = gabor_filter(I,Sx,Sy,U,V)
% 函数输入:
% I : 输入的二维图像
% Sx & Sy : 方差 along x and y-axes respectively
% U & V : 中心频率 along x and y-axes respectively
% 函数输出:
% G : G(x,y)
% gabout : Gabor滤波图像
% G(x,y)表达式如下:
% 1 -1 x ^ y ^
%%% G(x,y) = ---------- * exp ([----{(----) 2+(----) 2}+2*pi*i*(Ux+Vy)])
% 2*pi*sx*sy 2 sx sy
if isa(I,'double')~=1
I = double(I);
end
for x = -fix(Sx):fix(Sx)
for y = -fix(Sy):fix(Sy)
G(fix(Sx)+x+1,fix(Sy)+y+1) = (1/(2*pi*Sx*Sy))*exp(-.5*((x/Sx)^2+(y/Sy)^2)+2*pi*i*(U*x+V*y));
end
end
Imgabout = conv2(I,double(imag(G)),'same'); % 卷积
Regabout = conv2(I,double(real(G)),'same'); % 卷积
gabout = uint8(sqrt(Imgabout.*Imgabout + Regabout.*Regabout)); % 输出滤波图像