图像gabor特征提取 c代码
时间: 2023-05-12 19:03:14 浏览: 68
以下是一个简单的图像Gabor特征提取的C代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.14159265358979323846
void gaborFilter(double **input, double **output, int width, int height, double lambda, double theta, double sigma, double gamma)
{
int x, y, i, j;
double x_theta, y_theta, x_theta2, y_theta2, tmp1, tmp2, tmp3, tmp4, sum;
double **realFilter = (double **)malloc(sizeof(double *) * height);
double **imagFilter = (double **)malloc(sizeof(double *) * height);
for (i = 0; i < height; i++)
{
realFilter[i] = (double *)malloc(sizeof(double) * width);
imagFilter[i] = (double *)malloc(sizeof(double) * width);
}
double sigma_x = sigma;
double sigma_y = sigma / gamma;
double halfWidth = (double)(width - 1) / 2;
double halfHeight = (double)(height - 1) / 2;
for (y = 0; y < height; y++)
{
for (x = 0; x < width; x++)
{
x_theta = (x - halfWidth) * cos(theta) + (y - halfHeight) * sin(theta);
y_theta = -(x - halfWidth) * sin(theta) + (y - halfHeight) * cos(theta);
x_theta2 = x_theta * x_theta;
y_theta2 = y_theta * y_theta;
tmp1 = exp(-(x_theta2 + y_theta2) / (2 * sigma_x * sigma_y));
tmp2 = cos(2 * PI * x_theta / lambda);
realFilter[y][x] = tmp1 * tmp2;
imagFilter[y][x] = tmp1 * sin(2 * PI * x_theta / lambda);
}
}
for (y = 0; y < height; y++)
{
for (x = 0; x < width; x++)
{
sum = 0;
for (i = 0; i < height; i++)
{
for (j = 0; j < width; j++)
{
tmp3 = input[i][j] * realFilter[(i + y) % height][(j + x) % width];
tmp4 = input[i][j] * imagFilter[(i + y) % height][(j + x) % width];
sum += sqrt(tmp3 * tmp3 + tmp4 * tmp4);
}
}
output[y][x] = sum;
}
}
for (i = 0; i < height; i++)
{
free(realFilter[i]);
free(imagFilter[i]);
}
free(realFilter);
free(imagFilter);
}
int main()
{
int width = 256;
int height = 256;
double **input = (double **)malloc(sizeof(double *) * height);
double **output = (double **)malloc(sizeof(double *) * height);
int i, j;
for (i = 0; i < height; i++)
{
input[i] = (double *)malloc(sizeof(double) * width);
output[i] = (double *)malloc(sizeof(double) * width);
}
for (i = 0; i < height; i++)
{
for (j = 0; j < width; j++)
{
input[i][j] = (double)rand() / RAND_MAX;
}
}
double lambda = 10;
double theta = PI / 4;
double sigma = 5;
double gamma = 0.5;
gaborFilter(input, output, width, height, lambda, theta, sigma, gamma);
for (i = 0; i < height; i++)
{
free(input[i]);
free(output[i]);
}
free(input);
free(output);
return 0;
}
```