博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
基于OpenCV库的Gabor滤波器的实现
阅读量:6820 次
发布时间:2019-06-26

本文共 6387 字,大约阅读时间需要 21 分钟。

参考公式:

   

Gabor函数的再次理解 - 毽子 - 毽子不会落的博客

/**********************************************************

Author XJ Shi.    FileName: Gabor.h

Define the class head to Gabor Filter

@ 2011.1.2

***********************************************************/

#ifndef _GABOR_H
#define _GABOR_H
#include <stdio.h>
#include <iostream>
//#include <stdafx.h>
#include <cv.h>

#define PI 3.14159

#define GAMMA  0.5   //The default value of γ,which is the spatial aspect ratio (sigma_x/sigma_y)
#define RATIO_S2L 0.56  //The default value of σ/λ
class Gabor {

public:

 
 //@construct
 Gabor(float dLambda, float dTheta,float dRatio_S2L = RATIO_S2L, float dGamma = GAMMA,float dPhi = 0);
 
//@abolish
 ~Gabor();

 //@init

 void init(float dLambda,float dTheta, float dPhi,float dGamma = GAMMA);
 
//@init
 void init(float dSigma, float dTheta, float dPhi);
 
//@init
 void init();

    //@To find whether the Gabor kernel is created
 bool is_kernel(){ return bKernel; }
 
//@To find whether the parameters is inited
 bool is_init() { return bInit; }
 
//@To find whether the parameters inited is enough
 bool is_param() { return bParam; }
 
//@Get the kernel in matrix form
 CvMat* get_Mat() { return pGaborfilter; }
 
//@Get the kernel in image form
 IplImage* get_Imge();
 
//@Do the filtering operation to input image with Gabor kernel
 IplImage* do_filter(const IplImage *src);

protected:

 bool bParam;           //bool , if the parameters inited are enough
 bool bKernel;          //bool
 bool bInit;           //bool
 float Lambda;        //Wavelength of the cosine factor, which represent the central frequency of Gabor filter
 float Theta;         //Orientation of the Gabor function, the axis x'
 float Sigma;          // The standard deviation of x, and for y , it is Sigma/Gamma;
 float Gamma;        // The spatial aspect ratio
 float Phi;           //The phase offset of Gabor
 CvSize GaborWindow;    //The width of  window
 CvMat *pGaborfilter;  //The kernel of Gabor filter

private:

    void create_kernel();
};

#endif

 

/*************************************************************************************************

Author: XJ Shi.  FileName: Gabor.cpp

The member function in class Gabor

@2010.1.3

**************************************************************************************************/
#include <iostream>
#include <cv.h>
#include <highgui.h>
#include <Gabor.h>
#include <cstdlib>

Gabor::Gabor(float dLambda, float dTheta, float dRatio_S2L, float dGamma, float dPhi)

{
   Lambda = dLambda;
   Theta = dTheta;
   Sigma = dLambda*dRatio_S2L;
   Gamma = dGamma;
   Phi = dPhi;
   pGaborfilter = NULL;
   bParam = 1;
}

Gabor::~Gabor()

{
 cvReleaseMat(&pGaborfilter);
}
void Gabor::init()
{
  float dtmp;
  int itmp;
 if(is_param() == 0)
 {
  printf("The parameters are not enough!");
 }
 else
 {
  dtmp = sqrt(48*pow(Sigma,2)+1);
  itmp = cvRound(dtmp);
  if(itmp%2 == 0)
   itmp ++;
  GaborWindow.height = GaborWindow.width = 16;
  bInit = 1;

  create_kernel();

 }
}

void Gabor::init(float dSigma, float dTheta, float dPhi)

{
 float dtmp;
 int itmp;

 Sigma = dSigma;

 Theta = dTheta;
 Phi = dPhi;
 Gamma = GAMMA;
 Lambda = Sigma/RATIO_S2L;
 bParam = 1;
  
 dtmp = sqrt(24*pow(Sigma,2));
 itmp = cvRound(dtmp);
 if(itmp%2 == 0)
  itmp ++;
 GaborWindow.height = GaborWindow.width = itmp;
 bInit = 1; 

 create_kernel();

}

void Gabor::init(float dLambda, float dTheta, float dPhi, float dGamma)

{
 float dtmp;
 int itmp;
   
 Lambda = dLambda;
 Theta  = dTheta;
 Phi    = dPhi;
 Gamma  = dGamma;
    Sigma  = Lambda * RATIO_S2L;
    bParam = 1;

 dtmp = sqrt(24*pow(Sigma,2));

 itmp = cvRound(dtmp);
 if(itmp%2 == 0)
  itmp ++;
 GaborWindow.height = GaborWindow.width = itmp;
 bInit = 1; 

 create_kernel();

}

void Gabor::create_kernel()
{
 float tmp1,tmp2,xtmp,ytmp,re;
    int i,j,x,y;
   
 if(is_init() == 0)
  printf("The parameters haven't been initialed!");
 
 
 else{

 pGaborfilter = cvCreateMat(GaborWindow.height,GaborWindow.width,CV_32FC1);

    for(i= 0; i< GaborWindow.height; i++)
  for(j = 0; j< GaborWindow.width; j++)
  {
    x = j - GaborWindow.width/2;
    y = i - GaborWindow.height/2;

          xtmp = (float)x*cos(Theta) - (float)y*sin(Theta);

    ytmp = (float)x*sin(Theta) + (float)y*cos(Theta);
   
    tmp1 = exp(-(pow(xtmp,2)+pow(ytmp*Gamma,2))/(2*pow(Sigma,2)));
    tmp2 = cos(2*PI*xtmp/Lambda + Phi);
   // int p=sizeof(float);
    re   = tmp1*tmp2;
          cvSetReal2D((CvMat*)pGaborfilter,i,j,re);
  
  }
  bKernel = 1;
 }
}

IplImage* Gabor::get_Imge()

{
   if(is_kernel() == 0)
    printf("The filter hasn't bee created!");

   else{

    IplImage *pImg = cvCreateImage(GaborWindow,IPL_DEPTH_32F,1);

    IplImage *pImgU8 = cvCreateImage(GaborWindow,IPL_DEPTH_8U,1);
       CvMat * pMat = cvCreateMat(GaborWindow.height,GaborWindow.width,CV_32FC1);
   
    cvCopy(pGaborfilter,pImg);

    //pImg->imageData = (char *)pMat->data;

       cvNormalize((IplImage*)pImg, (IplImage*)pImg,0,255,CV_MINMAX,NULL);

       cvConvertScaleAbs(pImg,pImgU8,1,0);

    return pImgU8;

   }
}

IplImage * Gabor::do_filter(const IplImage *src)

{
    if(is_kernel()==false)
  printf("The Gabor Kernel has not been created!");
 else{
    
  IplImage *pDestImage = cvCreateImage(cvSize(src->width,src->height),IPL_DEPTH_8U,1);
       // IplImage * pGaborImage = get_Imge();
 // CvMat GaborKernel = cvMat(pGaborImage->height,pGaborImage->width,CV_8U,pGaborImage->imageData);
     IplImage *tmpImg = cvCloneImage(src);
     IplImage *tmpGrayImg = cvCreateImage(cvSize(src->width,src->height),IPL_DEPTH_8U,1);
  
  if(tmpImg->nChannels != 1)
   cvCvtColor(tmpImg,tmpGrayImg,CV_BGR2GRAY);
  else
  {
   cvReleaseImage(&tmpGrayImg);
   tmpGrayImg = tmpImg;
  }
  CvMat * pGaborKernel = get_Mat();
  
  cvFilter2D(tmpGrayImg,pDestImage,pGaborKernel,cvPoint((GaborWindow.width-1)/2,(GaborWindow.height-1)/2));
      
  cvReleaseImage(&tmpImg);

  return  pDestImage;       

 }
}

 

测试代码如下::

/*******************************************************************

FileName: test.cpp
The main function is in the file.
2010.1.3
***********************************************************************/

#include <iostream>

#include <Gabor.h>
#include "cv.h"
#include "highgui.h"
#include <ctype.h>

 

void main()

{
 
  Gabor filter1(2.74,PI*3/4,RATIO_S2L,GAMMA,0);
  filter1.init();
  IplImage* pGaborImg =  filter1.get_Imge();
 
  IplImage* inputImg = cvLoadImage("F://12.jpg",-1);
 
  cvNamedWindow("InputImage",CV_WINDOW_AUTOSIZE);
  cvShowImage("InputImage",inputImg);

  cvNamedWindow("Gabor",CV_WINDOW_AUTOSIZE);

  cvShowImage("Gabor",pGaborImg);

  IplImage* outImage = filter1.do_filter(inputImg);

 
  cvNamedWindow("OutputImage",CV_WINDOW_AUTOSIZE);
  cvShowImage("OutputImage",outImage);
  int key;
 
  key = cvWaitKey(0);
 
  cvSaveImage("F:\\GaborKernel.jpg",pGaborImg);
  //cvReleaseImage(&pGaborImg);
  cvSaveImage("F:\\FilteredImage.jpg",outImage); 

}

 

结果:

Gabor图像:

 

 

基于OpenCV库的Gabor滤波器的实现 - 毽子 - 毽子不会落的博客

转载于:https://www.cnblogs.com/retrieval/archive/2012/04/18/2455401.html

你可能感兴趣的文章
Android startActivity 隐式调用, 启动其他Activity过程
查看>>
webSocket 入门demo
查看>>
输入框样式定义学习笔记
查看>>
eclipse 快捷键
查看>>
ioc 注解的流程
查看>>
nexus私服无法下载jar包
查看>>
HTML 速查列表
查看>>
javascript 设计模式之单体(Singleton)模式
查看>>
如何禁止手机浏览器上下滚动?
查看>>
SpringBoot学习之路:13.SpringBoot使用Lombok简化JavaBean代码
查看>>
常考面试题及汇总答案
查看>>
easyUI-DataGrid加载数据和分页
查看>>
js Blob对象实现文件下载功能
查看>>
IT自学资源
查看>>
jQuery零基础入门——(七)事件
查看>>
Android 之窗口小部件详解--App Widget
查看>>
Java并发知识点总结
查看>>
避免基本类型数组转换列表陷阱(Arrays.asList())
查看>>
Linux 与 BSD 有什么不同?
查看>>
Xcode工程文件的格式说明[翻译]
查看>>