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.

46 lines
1.3 KiB

#include <stdio.h>
#include <time.h>
void applyGaussianBlur(float src[5][5], float dst[5][5], int h, int w,float kx[3],float ky[3]){
float buf[3][3]={0};
#define IDX(n)((n)%3)
for(int i=0;i<2;++i){
for(int j=1;j<w-1;++j){
buf[i][j]=src[i][j-1]*kx[0]+src[i][j]*kx[1]+src[i][j+1]*kx[2];
}
}
for(int i=1;i<h-1;++i){
for(int j=1;j<w-1;++j){
buf[IDX(i+1)][j]=src[i+1][j-1]*kx[0]+src[i+1][j]*kx[1]+src[i+1][j+1]*kx[2];
}
for(int j=1;j<w-1;++j){
dst[i][j]=buf[IDX(i-1)][j]*ky[0]+buf[IDX(i+1)][j]*ky[1]+buf[IDX(i+1)][j]*ky[2];
}
}
}
int main() {
float input[5][5] = {
{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}
};
float kx[3]={0.25,0.5,0.25};
float ky[3]={0.25,0.5,0.25};
float dst[5][5];
clock_t start_time = clock();
applyGaussianBlur(input,dst,5,5,kx,ky);
clock_t end_time = clock();
double time_taken = (double)(end_time - start_time) / CLOCKS_PER_SEC;
printf("Gaussian Blur applied to the image matrix:\n");
for (int i = 0; i < 5; i++) {
for (int j = 0; j <5; j++) {
printf("%.2f ", dst[i][j]);
}
printf("\n");
}
printf("Time taken: %f seconds\n", time_taken);
return 0;
}