我现在的研究方向主要在于图像分析这一块,下面借整理过去学习的技术资料重新复习一遍。
在图像分析里面,Gabor滤波器是应用非常广泛的一种工具,也算是我本科第一次接触图像分析领域时用到的一种工具,刚开始学Gabor的时候,因为找不到门路,看到它的几种形式特别伤脑筋,不知道这几种之间到底是个什么关系,为它的复杂形式纠结了好久。后来随着学习的深入,发现Gabor其实是一种很“傻瓜式的万用工具”,可以应用在很多场合,就是参数设置麻烦了点。在应用的时候,只要把它当成一个滤波器来对待就行,当然,若是有时间那是最好去理解一下它公式的含义。
三、Gabor滤波器程序实现
Matlab实现:
- function [G,gabout] = gaborfilter1(I,Sx,Sy,f,theta)
- %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'); cos代表实部
- % 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
- % %%isa判断输入参量是否为指定类型的对象
- if isa(I,'double')~=1
- I = double(I);
- end
- %%%%Sx,Sy在公式里分别表示Guass函数沿着x,y轴的标准差,相当于其他的gabor函数中的sigma.
- %%同时也用Sx,Sy指定了gabor滤波器的大小。(滤波器矩阵的大小)
- %%这里没有考虑到相位偏移.fix(n)是取小于n的整数(往零的方向靠)
- 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);
OpenCV中的实现:
- #include "precomp.hpp"
- /*
- Gabor filters and such. To be greatly extended to have full texture analysis.
- For the formulas and the explanation of the parameters see:
- http://en.wikipedia.org/wiki/Gabor_filter
- */
- cv::Mat cv::getGaborKernel( Size ksize, double sigma, double theta,
- double lambd, double gamma, double psi, int ktype )
- {
- double sigma_x = sigma;
- double sigma_y = sigma/gamma;
- int nstds = 3;
- int xmin, xmax, ymin, ymax;
- double c = cos(theta), s = sin(theta);
- if( ksize.width > 0 )
- xmax = ksize.width/2;
- else
- // cvRound返回和参数最接近的整数值;fabs返回浮点数的绝对值
- xmax = cvRound(std::max(fabs(nstds*sigma_x*c), fabs(nstds*sigma_y*s)));
- if( ksize.height > 0 )
- ymax = ksize.height/2;
- else
- ymax = cvRound(std::max(fabs(nstds*sigma_x*s), fabs(nstds*sigma_y*c)));
- xmin = -xmax;
- ymin = -ymax;
- //CV_Assert()若括号中的表达式值为false,则返回一个错误信息。
- CV_Assert( ktype == CV_32F || ktype == CV_64F );
- //初始化kernel矩阵
- Mat kernel(ymax - ymin + 1, xmax - xmin + 1, ktype);
- double scale = 1;
- double ex = -0.5/(sigma_x*sigma_x);
- double ey = -0.5/(sigma_y*sigma_y);
- double cscale = CV_PI*2/lambd;
- for( int y = ymin; y <= ymax; y++ )
- for( int x = xmin; x <= xmax; x++ )
- {
- double xr = x*c + y*s;
- double yr = -x*s + y*c;
- // real gabor
- double v = scale*exp(ex*xr*xr + ey*yr*yr)*cos(cscale*xr + psi);
- if( ktype == CV_32F )
- kernel.at<float>(ymax - y, xmax - x) = (float)v;
- else
- kernel.at<double>(ymax - y, xmax - x) = v;
- }
- return kernel;
- }
原文地址:http://blog.csdn.net/linj_m/article/details/9897439
还有一部分opencv的学习笔记