parent
f6fe59461a
commit
a90b76b446
@ -0,0 +1,80 @@
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include<stdio.h>
|
||||
|
||||
#define KERNEL_SIZE 5
|
||||
#define IMAGE_SIZE 3
|
||||
|
||||
void applyRowConvolution(int input[IMAGE_SIZE][IMAGE_SIZE], int temp[IMAGE_SIZE][IMAGE_SIZE], float row_kernel[KERNEL_SIZE]) {
|
||||
float sum = 0.0;
|
||||
for (int i = 0; i < IMAGE_SIZE; i++) {
|
||||
for (int j = 1; j < IMAGE_SIZE - 1; j++) {
|
||||
float blurred_value = 0.0;
|
||||
for (int ki = 0; ki < KERNEL_SIZE; ki++) {
|
||||
int x = i;
|
||||
int y = j - KERNEL_SIZE / 2 + ki;
|
||||
blurred_value += input[x][y] * row_kernel[ki];
|
||||
}
|
||||
temp[i][j] = (int)(blurred_value / sum);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void applyColumnConvolution(int temp[IMAGE_SIZE][IMAGE_SIZE], int output[IMAGE_SIZE][IMAGE_SIZE], float col_kernel[KERNEL_SIZE]) {
|
||||
float sum = 0.0;
|
||||
for (int i = 1; i < IMAGE_SIZE - 1; i++) {
|
||||
for (int j = 0; j < IMAGE_SIZE; j++) {
|
||||
float blurred_value = 0.0;
|
||||
for (int kj = 0; kj < KERNEL_SIZE; kj++) {
|
||||
int x = i - KERNEL_SIZE / 2 + kj;
|
||||
int y = j;
|
||||
blurred_value += temp[x][y] * col_kernel[kj];
|
||||
}
|
||||
output[i][j] = (int)(blurred_value / sum);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void applySeparableGaussianBlur(int input[IMAGE_SIZE][IMAGE_SIZE], int output[IMAGE_SIZE][IMAGE_SIZE]) {
|
||||
float row_kernel[KERNEL_SIZE] = {1, 4, 6, 4, 1};
|
||||
float col_kernel[KERNEL_SIZE] = {1, 4, 6, 4, 1};
|
||||
float sum = 256.0;
|
||||
|
||||
int temp[IMAGE_SIZE][IMAGE_SIZE] = {{0}};
|
||||
applyRowConvolution(input, temp, row_kernel);
|
||||
applyColumnConvolution(temp, output, col_kernel);
|
||||
}
|
||||
|
||||
int main() {
|
||||
int input[IMAGE_SIZE][IMAGE_SIZE] = {
|
||||
{10, 20, 30},
|
||||
{40, 50, 60},
|
||||
{70, 80, 90}
|
||||
};
|
||||
int output[IMAGE_SIZE][IMAGE_SIZE] = {{0}};
|
||||
|
||||
clock_t start = clock();
|
||||
applySeparableGaussianBlur(input, output);
|
||||
clock_t end = clock();
|
||||
|
||||
printf("Input Matrix:\n");
|
||||
for (int i = 0; i < IMAGE_SIZE; i++) {
|
||||
for (int j = 0; j < IMAGE_SIZE; j++) {
|
||||
printf("%d ", input[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
printf("Output Matrix:\n");
|
||||
for (int i = 0; i < IMAGE_SIZE; i++) {
|
||||
for (int j = 0; j < IMAGE_SIZE; j++) {
|
||||
printf("%d ", output[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
double time_spent = (double)(end - start) / CLOCKS_PER_SEC;
|
||||
printf("Time spent: %f seconds\n", time_spent);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in new issue