You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
1.9 KiB
68 lines
1.9 KiB
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <time.h>
|
|
#define BUF_SIZE 3
|
|
#define WRAPPED(i) ((i) % BUF_SIZE)
|
|
|
|
void applySeparableGaussianBlur(float src[5][5], float dst[5][5], int h, int w, float kx[3], float ky[3]) {
|
|
float buf[BUF_SIZE][5];
|
|
|
|
|
|
int i;
|
|
for (i = 1; i < h-1; i++) {
|
|
int bufIndex = WRAPPED(i);
|
|
int j;
|
|
for (j = 1; j < w-1; j++) {
|
|
buf[bufIndex][j] = kx[0] * (src[i-1][j] + src[i+1][j]) +
|
|
kx[1] * (src[i][j-1] + src[i][j+1]) +
|
|
kx[2] * (src[i-1][j-1] + src[i-1][j+1] + src[i+1][j-1] + src[i+1][j+1]);
|
|
}
|
|
|
|
buf[bufIndex][0] = buf[bufIndex][1];
|
|
buf[bufIndex][w-1] = buf[bufIndex][w-2];
|
|
}
|
|
|
|
|
|
int j;
|
|
for ( i = 1; i < h-1; i++) {
|
|
for ( j = 1; j < w-1; j++) {
|
|
dst[i][j] = ky[0] * (buf[WRAPPED(i-1)][j] + buf[WRAPPED(i+1)][j]) +
|
|
ky[1] * (buf[WRAPPED(i)][j-1] + buf[WRAPPED(i)][j+1]) +
|
|
ky[2] * (buf[WRAPPED(i-1)][j-1] + buf[WRAPPED(i-1)][j+1] +
|
|
buf[WRAPPED(i+1)][j-1] + buf[WRAPPED(i+1)][j+1]);
|
|
}
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
float src[5][5] = {
|
|
{0, 0, 0, 0, 0},
|
|
{0, 1, 1, 1, 0},
|
|
{0, 1, 50, 1, 0},
|
|
{0, 1, 1, 1, 0},
|
|
{0, 0, 0, 0, 0}
|
|
};
|
|
float dst[5][5] = {0};
|
|
float kx[3] = {1/16, 2/16, 1/16};
|
|
float ky[3] = {1, 4, 1};
|
|
|
|
clock_t start = clock();
|
|
|
|
applySeparableGaussianBlur(src, dst, 5, 5, kx, ky);
|
|
|
|
clock_t end = clock();
|
|
double time_spent = (double)(end - start) / CLOCKS_PER_SEC;
|
|
|
|
printf("Result Matrix:\n");
|
|
int i,j;
|
|
for (i = 1; i < 4; i++) {
|
|
for (j = 1; j < 4; j++) {
|
|
printf("%.2f ", dst[i][j]);
|
|
}
|
|
printf("\n");
|
|
}
|
|
printf("Time spent: %lf seconds\n", time_spent);
|
|
|
|
return 0;
|
|
}
|