1. 方式一
Sx,Sy在公式裏分別表示Guass函數沿着x,y軸的標準差,相當於其他的gabor函數中的 sigma. 同時也用Sx,Sy指定了gabor濾波器的大小。(濾波器矩陣的大小)
這裏沒有考慮到相位偏移.
- %%%%%%%VERSION 2
- %%ANOTHER DESCRIBTION OF GABOR FILTER
-
- %The Gabor filter is basically a Gaussian (with variances sx and sy along x and y-axes respectively)
- %modulated by a complex sinusoid (with centre frequencies U and V along x and y-axes respectively)
- %described by the following equation
- %%
- % -1 x' ^ y' ^
- %%% G(x,y,theta,f) = exp ([----{(----) 2+(----) 2}])*cos(2*pi*f*x');
- % 2 sx' sy'
- %%% x' = x*cos(theta)+y*sin(theta);
- %%% y' = y*cos(theta)-x*sin(theta);
- %% Describtion :
- %% I : Input image
- %% Sx & Sy : Variances along x and y-axes respectively
- %% f : The frequency of the sinusoidal function
- %% theta : The orientation of Gabor filter
- %% G : The output filter as described above
- %% gabout : The output filtered image
- %% Author : Ahmad poursaberi e-mail : [email protected]
- %% Faulty of Engineering, Electrical&Computer Department,Tehran
- %% University,Iran,June 2004
- function [G,gabout] = gaborfilter1(I,Sx,Sy,f,theta)
- if isa(I,'double')~=1
- I = double(I);
- end
- for x = -fix(Sx):fix(Sx)
- for y = -fix(Sy):fix(Sy)
- xPrime = x * cos(theta) + y * sin(theta);
- yPrime = y * cos(theta) - x * sin(theta);
- G(fix(Sx)+x+1,fix(Sy)+y+1) = exp(-.5*((xPrime/Sx)^2+(yPrime/Sy)^2))*cos(2*pi*f*xPrime);
- end
- end
- Imgabout = conv2(I,double(imag(G)),'same');
- Regabout = conv2(I,double(real(G)),'same');
- gabout = sqrt(Imgabout.*Imgabout + Regabout.*Regabout);
這個gaobor函數生成的gabor濾波器的圖像如圖:
不同的參數也會導致不同的結果。
2. 方式二
這個gabor濾波器的實現增加了尺度和方向變換,其他的參數以及意義都和上面的一樣。
- %%%%%%%VERSION 1
-
- %The Gabor filter is basically a Gaussian (with variances sx and sy along x and y-axes respectively)
- %modulated by a complex sinusoid (with centre frequencies U and V along x and y-axes respectively)
- %described by the following equation
- %%
- % 1 -1 x ^ y ^
- %%% G(x,y) = ---------- * exp ([----{(----) 2+(----) 2}+2*pi*i*(Ux+Vy)])
- % 2*pi*sx*sy 2 sx sy
-
- %% Describtion :
-
- %% I : Input image
- %% Sx & Sy : Variances along x and y-axes respectively
- %% U & V : Centre frequencies along x and y-axes respectively
-
- %% G : The output filter as described above
- %% gabout : The output filtered image
-
- %% Author : Ahmad poursaberi e-mail : [email protected]
- %% Faulty of Engineering, Electrical&Computer Department,Tehran
- %% University,Iran,June 2004
-
- function [G,gabout] = gaborfilter(I,Sx,Sy,U,V);
-
- 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));
調用代碼:
- ori=imread('C:\Users\watkins\Pictures\cartoon.jpg');
- grayimg=rgb2gray(ori);
- gim=im2double(grayimg);
-
- Sx=32;
- Sy=32;
- f=sqrt(8);
- theta=pi/2;
- u=4;
- v=4;
- %[G,gabout] = gaborfilter1(gim,Sx,Sy,f,theta);
- [G,gabout] = gaborfilter1(gim,Sx,Sy,u,v);
-
- imshow(real(G));
- %imshow(real(gabout));
濾波器圖片:
3. 方式三
- %%%%%%%VERSION 3
- %%ANOTHER DESCRIBTION OF GABOR FILTER
-
- %The Gabor filter is basically a Gaussian (with variances sx and sy along x and y-axes respectively)
- %modulated by a complex sinusoid (with centre frequencies U and V along x and y-axes respectively)
- %described by the following equation
- %%
- % 1 -1 x ^ y ^
- %%% Gi(x,y) = ---------- * exp ([----{(----) 2+(----) 2}])*Mi(x,y,f);
- % 2*pi*sx*sy 2 sx sy
- %%% i =1,2
- %%% M1(x,y,f) = cos[2*pi*f*sqrt(x^2+y^2)];
- %%% M2(x,y,f) = cos[2*pi*f*(x*cos(theta) + y*sin(theta)];
-
- %% Describtion :
-
- %% I : Input image
- %% Sx & Sy : Variances along x and y-axes respectively
- %% f : The frequency of the sinusoidal function
- %% theta : The orientation of Gabor filter
-
- %% G1 & G2 : The output filters as described above
- %% gabout1 & gabout2 : The output filtered images
-
- %% Author : Ahmad poursaberi e-mail : [email protected]
- %% Faulty of Engineering, Electrical&Computer Department,Tehran
- %% University,Iran,June 2004
-
- function [G1,G2,gabout1,gabout2] = gaborfilter2(I,Sx,Sy,f,theta)
-
- if isa(I,'double')~=1
- I = double(I);
- end
-
- for x = -fix(Sx):fix(Sx)
- for y = -fix(Sy):fix(Sy)
- M1 = cos(2*pi*f*sqrt(x^2+y^2));
- M2 = cos(2*pi*f*(x*cos(theta)+y*sin(theta)));
- G1(fix(Sx)+x+1,fix(Sy)+y+1) = (1/(2*pi*Sx*Sy)) * exp(-.5*((x/Sx)^2+(y/Sy)^2))*M1;
- G2(fix(Sx)+x+1,fix(Sy)+y+1) = (1/(2*pi*Sx*Sy)) * exp(-.5*((x/Sx)^2+(y/Sy)^2))*M2;
- end
- end
-
- Imgabout1 = conv2(I,double(imag(G1)),'same');
- Regabout1 = conv2(I,double(real(G1)),'same');
-
- Imgabout2 = conv2(I,double(imag(G2)),'same');
- Regabout2 = conv2(I,double(real(G2)),'same');
-
- gabout1 = sqrt(Imgabout1.*Imgabout1 + Regabout1.*Regabout1);
- gabout2 = sqrt(Imgabout2.*Imgabout2 + Regabout2.*Regabout2);
調用代碼:
- ori=imread('C:\Users\watkins\Pictures\cartoon.jpg');
- grayimg=rgb2gray(ori);
- gim=im2double(grayimg);
-
- Sx=16;
- Sy=16;
- f=sqrt(2);
- theta=pi/2;
- u=8;
- v=0;
- %[G,gabout] = gaborfilter1(gim,Sx,Sy,f,theta);
- %[G,gabout] = gaborfilter1(gim,Sx,Sy,u,v);
- [G1,G2,gabout1,gabout2] = gaborfilter2(gim,Sx,Sy,f,theta);
-
- R=real(G2);
- k=127.5/max(max(abs(R)));
- imshow(uint8(k*R+127.5));
- %imshow(real(G2));
- %imshow(abs(real(gabout)));
生成的濾波器圖片: