forked from zhongzhuoyao/HCCR-GoogLeNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgabor.cpp
90 lines (81 loc) · 2.49 KB
/
gabor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "gabor.h"
cv::Mat getMyGabor(int width, int height, int U, int V, double Kmax, double f,
double sigma, int ktype, const string kernel_name)
{
int half_width = width / 2;
int half_height = height / 2;
double Qu = PI*U/8;
double sqsigma = sigma*sigma;
double Kv = Kmax/pow(f,V);
double postmean = exp(-sqsigma/2);
cv::Mat kernel_re(width, height, ktype);
cv::Mat kernel_im(width, height, ktype);
cv::Mat kernel_mag(width, height, ktype);
double tmp1, tmp2, tmp3;
for(int j = -half_height; j <= half_height; j++){
for(int i = -half_width; i <= half_width; i++){
tmp1 = exp(-(Kv*Kv*(j*j+i*i))/(2*sqsigma));
tmp2 = cos(Kv*cos(Qu)*i + Kv*sin(Qu)*j) - postmean;
tmp3 = sin(Kv*cos(Qu)*i + Kv*sin(Qu)*j);
kernel_re.at<float>(j+half_height, i+half_width) =
(float)(Kv*Kv*tmp1*tmp2/sqsigma);
kernel_im.at<float>(j+half_height, i+half_width) =
(float)(Kv*Kv*tmp1*tmp3/sqsigma);
}
}
magnitude(kernel_re, kernel_im, kernel_mag);
if(kernel_name.compare("real") == 0)
return kernel_re;
else if(kernel_name.compare("imag") == 0)
return kernel_im;
else{
printf("Invalid kernel name!\n");
return kernel_mag;
}
}
void gabor_filter(cv::Mat& img,vector<cv::Mat> &featureMaps)
{
//cv::Mat img input character image
const int kernel_size = 7; // should be odd
// variables for gabor filter
double Kmax = PI/2;
double f = sqrt(2.0);
double sigma = 2*PI;
int U = 0;
int V = 0;
int GaborH = kernel_size;
int GaborW = kernel_size;
int UStart = 0, UEnd = 8;
int VStart = 1, VEnd = 2;
// variables for filter2D
cv::Point archor(-1,-1);
int ddepth = CV_32F;//CV_64F
//double delta = 0;
//eight orientation in terms of one frequnecy
for(V = VStart; V < VEnd; V++){
for(U = UStart; U < UEnd; U++){
cv::Mat kernel_re, kernel_im;
cv::Mat dst_re, dst_im, dst_mag;
kernel_re = getMyGabor(GaborW, GaborH, U, V,
Kmax, f, sigma, CV_32F, "real");
kernel_im = getMyGabor(GaborW, GaborH, U, V,
Kmax, f, sigma, CV_32F, "imag");
// flip kernel
// Gabor kernel is symmetric, so do not need flip
filter2D(img, dst_re, ddepth, kernel_re);
filter2D(img, dst_im, ddepth, kernel_im);
dst_mag.create(img.rows, img.cols, CV_32FC1);
magnitude(cv::Mat(dst_re),cv::Mat(dst_im),
dst_mag);
//normalize gabor kernel
cv::normalize(dst_mag, dst_mag, 0, 255, CV_MINMAX);
dst_mag.convertTo(dst_mag,CV_8U);
featureMaps.push_back(dst_mag);
kernel_re.release();
kernel_im.release();
dst_re.release();
dst_im.release();
dst_mag.release();
}
}
}