42 #ifndef __GABOR_FILTER_HPP__
43 #define __GABOR_FILTER_HPP__
49 #define VM_MAX(x, y) (((x) > (y)) ? (x) : (y))
51 template <
class T1,
class T2>
52 static RES_T _computeGaborFilterConvolution(T1 *bufferIn,
int W,
int H,
53 double sigma,
double theta,
54 double lambda,
double psi,
55 double gamma, T2 *bufferOut)
67 double sigma_x = sigma;
68 double sigma_y = sigma * gamma;
71 dXmax = VM_MAX(std::abs(nstds * sigma_x * cos(theta)),
72 std::abs(nstds * sigma_y * sin(theta)));
73 dYmax = VM_MAX(std::abs(nstds * sigma_x * sin(theta)),
74 std::abs(nstds * sigma_y * cos(theta)));
75 Xmax = (int) VM_MAX(1, std::ceil(dXmax));
76 Ymax = (int) VM_MAX(1, std::ceil(dYmax));
80 double *x_theta =
new double[dx * dy];
81 double *y_theta =
new double[dx * dy];
84 for (i = 0; i < dx; i++)
85 for (j = 0; j < dy; j++) {
87 (i - dx / 2) * cos(theta) + (j - dy / 2) * sin(theta);
89 -(i - dx / 2) * sin(theta) + (j - dy / 2) * cos(theta);
92 double *gabor =
new double[dx * dy];
93 for (i = 0; i < dx; i++) {
94 for (j = 0; j < dy; j++) {
98 std::exp(-0.5 * ((x_theta[pos] * x_theta[pos]) / (sigma_x * sigma_x) +
99 (y_theta[pos] * y_theta[pos]) / (sigma_y * sigma_y))) *
100 cos(2 * 3.14159 / lambda * x_theta[pos] + psi);
112 for (j = 0; j < H; j++)
113 for (i = 0; i < W; i++) {
115 for (k = -dx / 2; k <= dx / 2; k++)
116 for (l = -dy / 2; l <= dy / 2; l++) {
124 D += gabor[k + dx / 2 + (l + dx / 2) * dx] * bufferIn[I + J * W];
126 bufferOut[i + j * W] = (T2) D;
138 static void t_createGaborKernel(T1 *gabor,
double sigma,
double theta,
139 double lambda,
double psi,
double gamma)
141 int i, j, Xmax, Ymax, nstds = 3, dx, dy;
147 double sigma_x = sigma;
148 double sigma_y = sigma * gamma;
151 dXmax = VM_MAX(std::fabs(nstds * sigma_x * cos(theta)),
152 std::fabs(nstds * sigma_y * sin(theta)));
153 dYmax = VM_MAX(std::fabs(nstds * sigma_x * sin(theta)),
154 std::fabs(nstds * sigma_y * cos(theta)));
155 Xmax = (int) VM_MAX(1, std::ceil(dXmax));
156 Ymax = (int) VM_MAX(1, std::ceil(dYmax));
160 double *x_theta =
new double[dx * dy];
161 double *y_theta =
new double[dx * dy];
164 for (i = 0; i < dx; i++)
165 for (j = 0; j < dy; j++) {
166 x_theta[i + j * dx] =
167 (i - dx / 2) * cos(theta) + (j - dy / 2) * sin(theta);
168 y_theta[i + j * dx] =
169 -(i - dx / 2) * sin(theta) + (j - dy / 2) * cos(theta);
172 for (j = 0; j < dy; j++)
173 for (i = 0; i < dx; i++)
175 (T1)
std::exp(-0.5 * ((x_theta[i + j * dx] * x_theta[i + j * dx]) /
176 (sigma_x * sigma_x) +
177 (y_theta[i + j * dx] * y_theta[i + j * dx]) /
178 (sigma_y * sigma_y))) *
179 cos(2 * 3.14159 / lambda * x_theta[i + j * dx] + psi);
189 double theta,
double lambda,
double psi,
192 ASSERT_ALLOCATED(&imIn, &imOut);
193 ASSERT_SAME_SIZE(&imIn, &imOut);
205 typename ImDtTypes<T>::lineType bufferIn = imIn.
getPixels();
206 typename ImDtTypes<T>::lineType bufferOut = imOut.getPixels();
208 _computeGaborFilterConvolution(bufferIn, S[0], S[1], sigma, theta, lambda,
209 psi, gamma, bufferOut);
219 double theta,
double lambda,
double psi,
220 double gamma,
double Min,
double Max,
223 ASSERT_ALLOCATED(&imIn, &imOut, &imGabor);
224 ASSERT_SAME_SIZE(&imIn, &imOut, &imGabor);
241 typename ImDtTypes<T>::lineType bufferGabor = imGabor.
getPixels();
242 typename ImDtTypes<T>::lineType bufferOut = imOut.getPixels();
248 T dtMax = imOut.getDataTypeMax();
249 for (
int i = W * H - 1; i >= 0; i--) {
250 bufferOut[i] = (T)((bufferGabor[i] - Min) / (Max - Min) * dtMax);
260 double theta,
double lambda,
261 double psi,
double gamma,
double *Min,
265 ASSERT_ALLOCATED(&imIn, &imOut, &imGabor);
266 ASSERT_SAME_SIZE(&imIn, &imOut, &imGabor);
283 typename ImDtTypes<T>::lineType bufferGabor = imGabor.
getPixels();
284 typename ImDtTypes<T>::lineType bufferOut = imOut.getPixels();
290 *Min = bufferGabor[0];
291 *Max = bufferGabor[0];
292 for (
int i = W * H - 1; i > 0; i--) {
293 if (*Min > bufferGabor[i])
294 *Min = bufferGabor[i];
295 if (*Max < bufferGabor[i])
296 *Max = bufferGabor[i];
299 T dtMax = imOut.getDataTypeMax();
300 for (
int i = W * H - 1; i >= 0; i--) {
301 bufferOut[i] = (T)((bufferGabor[i] - *Min) / (*Max - *Min) * dtMax);
void getSize(size_t *w, size_t *h, size_t *d) const
Get image size.
Definition: DBaseImage.h:118
Definition: DBaseImage.h:386
Main Image class.
Definition: DImage.hpp:57
lineType getPixels() const
Get the pixels as a 1D array.
Definition: DImage.hpp:105
RES_T gaborFilterConvolution(const Image< T > &imIn, double sigma, double theta, double lambda, double psi, double gamma, Image< T > &imOut)
gaborFilterConvolution Gabor Filter
Definition: filterGabor.hpp:188
RES_T gaborFilterConvolutionNorm(const Image< T > &imIn, double sigma, double theta, double lambda, double psi, double gamma, double Min, double Max, Image< T > &imOut, Image< T > &imGabor)
gaborFilterConvolutionNorm Gabor Filter (normalized between Min and Max)
Definition: filterGabor.hpp:218
RES_T gaborFilterConvolutionNormAuto(const Image< T > &imIn, double sigma, double theta, double lambda, double psi, double gamma, double *Min, double *Max, Image< T > &imOut, Image< T > &imGabor)
gaborFilterConvolutionNormAuto Gabor Filter (automatically normalized)
Definition: filterGabor.hpp:259
RES_T exp(const Image< T1 > &imIn, Image< T2 > &imOut, int base=0)
exp() - exponential of an image
Definition: DImageArith.hpp:881