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.

70 lines
1.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <stdio.h>
#include <ctime>
#include <stdlib.h>
#define H 5
#define W 5
#define IDX(n) ((n)%3)
void applySeparableGaussianBlur(float src[H][W], float dst[H][W], int h, int w, float kx[3], float ky[3])
{
float buf[3][3]={0};
for(int k = 0; k< 2; ++k)
{
for (int j = 1; j < w - 1; ++j)
{
buf[k][j] = src[k][j - 1] * kx[0] + src[k][j] * kx[1] + src[k][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];
dst[i][j] = buf[IDX(i - 1)][j] * ky[0] + buf[IDX(i)][j] * ky[1] + buf[IDX(i + 1)][j] * ky[2];
}
}
}
int main()
{
float inputImage[H][W]=
{
{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[H][W]=
{
{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}
};
clock_t start = clock();
applySeparableGaussianBlur(inputImage, dst, H, W, kx, ky);
clock_t end = clock();
double time_spent = double(end - start) / CLOCKS_PER_SEC;
printf("运行时间:%lf秒\n不考虑边界dst矩阵结果为\n",time_spent);
for(int i=0;i<H;i++)
{
for(int j=0;j<W;j++)
{
printf("%f ",dst[i][j]);
}
printf("\n");
}
}