Gabor滤波器

来源:互联网 发布:淘宝网店流量的重要性 编辑:程序博客网 时间:2024/04/25 20:35

Gabor的核函数参考的wiki


使用实数Real的公式计算核函数代码:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. Mat getGaborFilter(float lambda, float theta,   
  2.     float sigma2,float gamma,   
  3.     float psi = 0.0f){  
  4.         if(abs(lambda-0.0f)<1e-6){  
  5.             lambda = 1.0f;  
  6.         }   
  7.         float sigma_x = sigma2;  
  8.         float sigma_y = sigma2/(gamma*gamma);  
  9.         int nstds = 3;  
  10.         float sqrt_sigma_x = sqrt(sigma_x);  
  11.         float sqrt_sigma_y = sqrt(sigma_y);  
  12.         int xmax = max(abs(nstds*sqrt_sigma_x*cos(theta)),abs(nstds*sqrt_sigma_y*sin(theta)));  
  13.         int ymax = max(abs(nstds*sqrt_sigma_x*sin(theta)),abs(nstds*sqrt_sigma_y*cos(theta)));  
  14.         int half_filter_size = xmax>ymax ? xmax:ymax;  
  15.         int filter_size = 2*half_filter_size+1;  
  16.         Mat gaber = Mat::zeros(filter_size,filter_size,CV_32F);  
  17.         for(int i=0;i<filter_size;i++){  
  18.             float* f = gaber.ptr<float>(i);  
  19.             for(int j=0;j<filter_size;j++){  
  20.                 int x = j-half_filter_size;  
  21.                 int y = i-half_filter_size;  
  22.                 float x_theta=x*cos(theta)+y*sin(theta);  
  23.                 float y_theta=-x*sin(theta)+y*cos(theta);  
  24.                 f[x] = exp(-.5*(x_theta*x_theta/sigma_x+y_theta*y_theta/sigma_y));  
  25.                 f[x] = f[x]*cos(2*PI*x_theta/lambda+psi);  
  26.             };  
  27.         }  
  28.         return gaber;  
  29. }  

使用得到的Gabor核对一副图像进行卷积的函数:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. Mat gaborFilter(Mat& img, Mat& filter){  
  2.     int half_filter_size = (max(filter.rows,filter.cols)-1)/2;  
  3.     Mat filtered_img(img.rows,img.cols,CV_32F);  
  4.     for(int i=0;i<img.rows;i++){  
  5.         uchar* img_p = img.ptr<uchar>(i);  
  6.         float* img_f = filtered_img.ptr<float>(i);  
  7.         for(int j=0;j<img.cols;j++){  
  8.             float filter_value = 0.0f;  
  9.             for(int fi=0;fi<filter.rows;fi++){  
  10.                 float* f = filter.ptr<float>(fi);  
  11.                 int img_i = i+fi-half_filter_size;  
  12.                 img_i = img_i < 0 ? 0 : img_i;  
  13.                 img_i = img_i >= img.rows ? (img.rows-1) : img_i;  
  14.                 uchar* p = img.ptr<uchar>(img_i);  
  15.                 for(int fj=0;fj<filter.cols;fj++){  
  16.                     int img_j = j+fj-half_filter_size;  
  17.                     img_j = img_j < 0 ? 0 : img_j;  
  18.                     img_j = (img_j >= img.cols) ? (img.cols-1) : img_j;  
  19.                     float tmp = (float)p[img_j]*f[fj];  
  20.                     filter_value += tmp;  
  21.                 }  
  22.             }  
  23.             img_f[j] = filter_value;  
  24.         }  
  25.     }  
  26.     return filtered_img;  
  27. }  

对一幅图使用如下核卷积:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. Mat gaber = getGaborFilter(0.3,0,4,2);  
效果如下:


Gabor算子卷积之后得到很多负值(不知道有没有问题),后面的图是归一化之后显示出来的。

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. Mat normalizeFilterShow(Mat gaber){  
  2.     Mat gaber_show = Mat::zeros(gaber.rows,gaber.cols,CV_8UC1);  
  3.     float gaber_max = FLT_MIN;  
  4.     float gaber_min = FLT_MAX;  
  5.     for(int i=0;i<gaber.rows;i++){  
  6.         float* f = gaber.ptr<float>(i);  
  7.         for(int j=0;j<gaber.cols;j++){  
  8.             if(f[j]>gaber_max){  
  9.                 gaber_max = f[j];  
  10.             }  
  11.             if(f[j]<gaber_min){  
  12.                 gaber_min = f[j];  
  13.             }  
  14.         }  
  15.     }  
  16.     float gaber_max_min = gaber_max-gaber_min;  
  17.     for(int i=0;i<gaber_show.rows;i++){  
  18.         uchar* p = gaber_show.ptr<uchar>(i);  
  19.         float* f = gaber.ptr<float>(i);  
  20.         for(int j=0;j<gaber_show.cols;j++){  
  21.             if(gaber_max_min!=0.0f){  
  22.                 float tmp = (f[j]-gaber_min)*255.0f/gaber_max_min;  
  23.                 p[j] = (uchar)tmp;  
  24.             }  
  25.             else{  
  26.                 p[j] = 255;  
  27.             }  
  28.         }  
  29.     }  
  30.     return gaber_show;  
  31. }  

(转载请注明作者和出处:http://blog.csdn.net/xiaowei_cqu 未经允许请勿用于商业用途)

0 0
原创粉丝点击