自己项目中用到了Gabor滤波器,于是自己借助Opencv图像库,实现了Gabor滤波器的特征提取,用类的形式将其封装,希望对大家有用>0<。
Gabor参数主要是5个尺度,8个方向,共40个卷积核。本人图片大小为36*48大小。下采样为10*11,故特征维数为:10*11*5*40=4400维。针对特征维数过大,可以采用PCA或LDA降维,这里就不展现了。
Gabor.h
#ifndef _GABOR_H
#define _GABOR_H
#include <opencv2/opencv.hpp>
#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>
using namespace cv;
using namespace std;
#define Gabor_num 40
#define U 8
#define V 5
class Gabor
{
public:
Gabor():m_kmax(CV_PI/2),m_f(sqrt(2.0)),m_sigma(CV_PI),ds_w(10),ds_h(11),ke_w(31),ke_h(31),raT(0.9){};
void MakeAllGaborKernal();
void MakeGaborKernal(int ke_h,int ke_w,int u,int v,Mat &GaborReal,Mat &GaborImg);
Mat Gabor_T_Fast1(Mat &src,int ds_h,int ds_w,Mat kel_GR[Gabor_num],Mat kel_GI[Gabor_num]);
int Gabor_T_Fast2(Mat &src,int ds_h,int ds_w,Mat kel_GR[Gabor_num],Mat kel_GI[Gabor_num],double* feature);
Mat Create_GaborF(Mat& src);
int getGaborMeanAndVarianceFeature(Mat &src,double* feature);
private:
double m_kmax;
double m_f;
double m_sigma;
int ds_w;
int ds_h;
int ke_w;
int ke_h;
double raT;
Mat GaborReal[Gabor_num];
Mat GaborImg[Gabor_num];
};
#endif
Gabor.cpp
#include "Gabor.h"
enum convtype
{
full,
same,
valid
};
/*
*@函数名: conv2
*@功能: 卷积运算
*@参数: const Mat &img 原图片
const Mat ikernel 卷积核
convtype type 卷积类型
*@返回值: Mat 矩阵卷积后的结果
*@作者: WiseClown
*@日期: 2016-1-7
*---------------------------------------
*时间 修改人 修改者
*---------------------------------------
**/
Mat conv2(const Mat &img, const Mat ikernel, convtype type)
{
Mat dest;
Mat kernel;
flip(ikernel,kernel,-1);
Mat source = img;
if(type