ISP Matab Gabor濾波器

1. 方式一

 

Sx,Sy在公式裏分別表示Guass函數沿着x,y軸的標準差,相當於其他的gabor函數中的 sigma. 同時也用Sx,Sy指定了gabor濾波器的大小。(濾波器矩陣的大小)

這裏沒有考慮到相位偏移.

 

  1. %%%%%%%VERSION 2
  2. %%ANOTHER DESCRIBTION OF GABOR FILTER
  3. %The Gabor filter is basically a Gaussian (with variances sx and sy along x and y-axes respectively)
  4. %modulated by a complex sinusoid (with centre frequencies U and V along x and y-axes respectively)
  5. %described by the following equation
  6. %%
  7. % -1 x' ^ y' ^
  8. %%% G(x,y,theta,f) = exp ([----{(----) 2+(----) 2}])*cos(2*pi*f*x');
  9. % 2 sx' sy'
  10. %%% x' = x*cos(theta)+y*sin(theta);
  11. %%% y' = y*cos(theta)-x*sin(theta);
  12. %% Describtion :
  13. %% I : Input image
  14. %% Sx & Sy : Variances along x and y-axes respectively
  15. %% f : The frequency of the sinusoidal function
  16. %% theta : The orientation of Gabor filter
  17. %% G : The output filter as described above
  18. %% gabout : The output filtered image
  19. %% Author : Ahmad poursaberi e-mail : [email protected]
  20. %% Faulty of Engineering, Electrical&Computer Department,Tehran
  21. %% University,Iran,June 2004
  22. function [G,gabout] = gaborfilter1(I,Sx,Sy,f,theta)
  23. if isa(I,'double')~=1
  24. I = double(I);
  25. end
  26. for x = -fix(Sx):fix(Sx)
  27. for y = -fix(Sy):fix(Sy)
  28. xPrime = x * cos(theta) + y * sin(theta);
  29. yPrime = y * cos(theta) - x * sin(theta);
  30. G(fix(Sx)+x+1,fix(Sy)+y+1) = exp(-.5*((xPrime/Sx)^2+(yPrime/Sy)^2))*cos(2*pi*f*xPrime);
  31. end
  32. end
  33. Imgabout = conv2(I,double(imag(G)),'same');
  34. Regabout = conv2(I,double(real(G)),'same');
  35. gabout = sqrt(Imgabout.*Imgabout + Regabout.*Regabout);


這個gaobor函數生成的gabor濾波器的圖像如圖:

不同的參數也會導致不同的結果。

 

2. 方式二

 

這個gabor濾波器的實現增加了尺度和方向變換,其他的參數以及意義都和上面的一樣。

 

  1. %%%%%%%VERSION 1
  2. %The Gabor filter is basically a Gaussian (with variances sx and sy along x and y-axes respectively)
  3. %modulated by a complex sinusoid (with centre frequencies U and V along x and y-axes respectively)
  4. %described by the following equation
  5. %%
  6. % 1 -1 x ^ y ^
  7. %%% G(x,y) = ---------- * exp ([----{(----) 2+(----) 2}+2*pi*i*(Ux+Vy)])
  8. % 2*pi*sx*sy 2 sx sy
  9. %% Describtion :
  10. %% I : Input image
  11. %% Sx & Sy : Variances along x and y-axes respectively
  12. %% U & V : Centre frequencies along x and y-axes respectively
  13. %% G : The output filter as described above
  14. %% gabout : The output filtered image
  15. %% Author : Ahmad poursaberi e-mail : [email protected]
  16. %% Faulty of Engineering, Electrical&Computer Department,Tehran
  17. %% University,Iran,June 2004
  18. function [G,gabout] = gaborfilter(I,Sx,Sy,U,V);
  19. if isa(I,'double')~=1
  20. I = double(I);
  21. end
  22. for x = -fix(Sx):fix(Sx)
  23. for y = -fix(Sy):fix(Sy)
  24. 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));
  25. end
  26. end
  27. Imgabout = conv2(I,double(imag(G)),'same');
  28. Regabout = conv2(I,double(real(G)),'same');
  29. gabout = uint8(sqrt(Imgabout.*Imgabout + Regabout.*Regabout));


調用代碼:

  1. ori=imread('C:\Users\watkins\Pictures\cartoon.jpg');
  2. grayimg=rgb2gray(ori);
  3. gim=im2double(grayimg);
  4. Sx=32;
  5. Sy=32;
  6. f=sqrt(8);
  7. theta=pi/2;
  8. u=4;
  9. v=4;
  10. %[G,gabout] = gaborfilter1(gim,Sx,Sy,f,theta);
  11. [G,gabout] = gaborfilter1(gim,Sx,Sy,u,v);
  12. imshow(real(G));
  13. %imshow(real(gabout));


 

濾波器圖片:

 

3. 方式三

 

  1. %%%%%%%VERSION 3
  2. %%ANOTHER DESCRIBTION OF GABOR FILTER
  3. %The Gabor filter is basically a Gaussian (with variances sx and sy along x and y-axes respectively)
  4. %modulated by a complex sinusoid (with centre frequencies U and V along x and y-axes respectively)
  5. %described by the following equation
  6. %%
  7. % 1 -1 x ^ y ^
  8. %%% Gi(x,y) = ---------- * exp ([----{(----) 2+(----) 2}])*Mi(x,y,f);
  9. % 2*pi*sx*sy 2 sx sy
  10. %%% i =1,2
  11. %%% M1(x,y,f) = cos[2*pi*f*sqrt(x^2+y^2)];
  12. %%% M2(x,y,f) = cos[2*pi*f*(x*cos(theta) + y*sin(theta)];
  13. %% Describtion :
  14. %% I : Input image
  15. %% Sx & Sy : Variances along x and y-axes respectively
  16. %% f : The frequency of the sinusoidal function
  17. %% theta : The orientation of Gabor filter
  18. %% G1 & G2 : The output filters as described above
  19. %% gabout1 & gabout2 : The output filtered images
  20. %% Author : Ahmad poursaberi e-mail : [email protected]
  21. %% Faulty of Engineering, Electrical&Computer Department,Tehran
  22. %% University,Iran,June 2004
  23. function [G1,G2,gabout1,gabout2] = gaborfilter2(I,Sx,Sy,f,theta)
  24. if isa(I,'double')~=1
  25. I = double(I);
  26. end
  27. for x = -fix(Sx):fix(Sx)
  28. for y = -fix(Sy):fix(Sy)
  29. M1 = cos(2*pi*f*sqrt(x^2+y^2));
  30. M2 = cos(2*pi*f*(x*cos(theta)+y*sin(theta)));
  31. G1(fix(Sx)+x+1,fix(Sy)+y+1) = (1/(2*pi*Sx*Sy)) * exp(-.5*((x/Sx)^2+(y/Sy)^2))*M1;
  32. G2(fix(Sx)+x+1,fix(Sy)+y+1) = (1/(2*pi*Sx*Sy)) * exp(-.5*((x/Sx)^2+(y/Sy)^2))*M2;
  33. end
  34. end
  35. Imgabout1 = conv2(I,double(imag(G1)),'same');
  36. Regabout1 = conv2(I,double(real(G1)),'same');
  37. Imgabout2 = conv2(I,double(imag(G2)),'same');
  38. Regabout2 = conv2(I,double(real(G2)),'same');
  39. gabout1 = sqrt(Imgabout1.*Imgabout1 + Regabout1.*Regabout1);
  40. gabout2 = sqrt(Imgabout2.*Imgabout2 + Regabout2.*Regabout2);


 

調用代碼:

  1. ori=imread('C:\Users\watkins\Pictures\cartoon.jpg');
  2. grayimg=rgb2gray(ori);
  3. gim=im2double(grayimg);
  4. Sx=16;
  5. Sy=16;
  6. f=sqrt(2);
  7. theta=pi/2;
  8. u=8;
  9. v=0;
  10. %[G,gabout] = gaborfilter1(gim,Sx,Sy,f,theta);
  11. %[G,gabout] = gaborfilter1(gim,Sx,Sy,u,v);
  12. [G1,G2,gabout1,gabout2] = gaborfilter2(gim,Sx,Sy,f,theta);
  13. R=real(G2);
  14. k=127.5/max(max(abs(R)));
  15. imshow(uint8(k*R+127.5));
  16. %imshow(real(G2));
  17. %imshow(abs(real(gabout)));


 

生成的濾波器圖片:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章