图像分析:Gabor滤波器解析与编程

我现在的研究方向主要在于图像分析这一块,下面借整理过去学习的技术资料重新复习一遍。

Blogger: LinJM 微博( LinJM-机器视觉

在图像分析里面,Gabor滤波器是应用非常广泛的一种工具,也算是我本科第一次接触图像分析领域时用到的一种工具,刚开始学Gabor的时候,因为找不到门路,看到它的几种形式特别伤脑筋,不知道这几种之间到底是个什么关系,为它的复杂形式纠结了好久。后来随着学习的深入,发现Gabor其实是一种很“傻瓜式的万用工具”,可以应用在很多场合,就是参数设置麻烦了点。在应用的时候,只要把它当成一个滤波器来对待就行,当然,若是有时间那是最好去理解一下它公式的含义。







三、Gabor滤波器程序实现
Matlab实现:
[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. function [G,gabout] = gaborfilter1(I,Sx,Sy,f,theta)  
  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     x' ^      y' ^               
  7. %%% G(x,y,theta,f) =  exp ([----{(----) 2+(----) 2}])*cos(2*pi*f*x'); cos代表实部  
  8. %                                   2     sx'       sy'  
  9. %%% x' = x*cos(theta)+y*sin(theta);  
  10. %%% y' = y*cos(theta)-x*sin(theta);  
  11. %% Describtion :  
  12. %% I : Input image  
  13. %% Sx & Sy : Variances along x and y-axes respectively 方差  
  14. %% f : The frequency of the sinusoidal function  
  15. %% theta : The orientation of Gabor filter  
  16. %% G : The output filter as described above  
  17. %% gabout : The output filtered image  
  18. % %%isa判断输入参量是否为指定类型的对象  
  19. if isa(I,'double')~=1   
  20.     I = double(I);  
  21. end  
  22. %%%%Sx,Sy在公式里分别表示Guass函数沿着x,y轴的标准差,相当于其他的gabor函数中的sigma.   
  23. %%同时也用Sx,Sy指定了gabor滤波器的大小。(滤波器矩阵的大小)  
  24. %%这里没有考虑到相位偏移.fix(n)是取小于n的整数(往零的方向靠)  
  25. for x = -fix(Sx):fix(Sx)  
  26.     for y = -fix(Sy):fix(Sy)  
  27.         xPrime = x * cos(theta) + y * sin(theta);  
  28.         yPrime = y * cos(theta) - x * sin(theta);  
  29.         G(fix(Sx)+x+1,fix(Sy)+y+1) = exp(-.5*((xPrime/Sx)^2+(yPrime/Sy)^2))*cos(2*pi*f*xPrime);  
  30.     end  
  31. end  
  32. Imgabout = conv2(I,double(imag(G)),'same');  
  33. Regabout = conv2(I,double(real(G)),'same');  
  34. gabout = sqrt(Imgabout.*Imgabout + Regabout.*Regabout);  



OpenCV中的实现:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include "precomp.hpp"  
  2.   
  3. /* 
  4.  Gabor filters and such. To be greatly extended to have full texture analysis. 
  5.  For the formulas and the explanation of the parameters see: 
  6.  http://en.wikipedia.org/wiki/Gabor_filter 
  7. */  
  8.   
  9. cv::Mat cv::getGaborKernel( Size ksize, double sigma, double theta,  
  10.                             double lambd, double gamma, double psi, int ktype )  
  11. {  
  12.     double sigma_x = sigma;  
  13.     double sigma_y = sigma/gamma;  
  14.     int nstds = 3;  
  15.     int xmin, xmax, ymin, ymax;  
  16.     double c = cos(theta), s = sin(theta);  
  17.   
  18.     if( ksize.width > 0 )  
  19.         xmax = ksize.width/2;  
  20.     else  
  21.         // cvRound返回和参数最接近的整数值;fabs返回浮点数的绝对值  
  22.         xmax = cvRound(std::max(fabs(nstds*sigma_x*c), fabs(nstds*sigma_y*s)));  
  23.   
  24.     if( ksize.height > 0 )  
  25.         ymax = ksize.height/2;  
  26.     else  
  27.         ymax = cvRound(std::max(fabs(nstds*sigma_x*s), fabs(nstds*sigma_y*c)));  
  28.   
  29.     xmin = -xmax;  
  30.     ymin = -ymax;  
  31.       
  32.     //CV_Assert()若括号中的表达式值为false,则返回一个错误信息。  
  33.     CV_Assert( ktype == CV_32F || ktype == CV_64F );  
  34.       
  35.     //初始化kernel矩阵  
  36.     Mat kernel(ymax - ymin + 1, xmax - xmin + 1, ktype);  
  37.   
  38.     double scale = 1;  
  39.     double ex = -0.5/(sigma_x*sigma_x);  
  40.     double ey = -0.5/(sigma_y*sigma_y);  
  41.     double cscale = CV_PI*2/lambd;  
  42.   
  43.     forint y = ymin; y <= ymax; y++ )  
  44.         forint x = xmin; x <= xmax; x++ )  
  45.         {  
  46.             double xr = x*c + y*s;  
  47.             double yr = -x*s + y*c;  
  48.             // real gabor  
  49.             double v = scale*exp(ex*xr*xr + ey*yr*yr)*cos(cscale*xr + psi);  
  50.             if( ktype == CV_32F )  
  51.                 kernel.at<float>(ymax - y, xmax - x) = (float)v;  
  52.             else  
  53.                 kernel.at<double>(ymax - y, xmax - x) = v;  
  54.         }  
  55.   
  56.     return kernel;  
  57. }  

原文地址:http://blog.csdn.net/linj_m/article/details/9897439

还有一部分opencv的学习笔记

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值