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.
64 lines
1.5 KiB
64 lines
1.5 KiB
#include <stdio.h>
|
|
#include <ctime>
|
|
#include <stdlib.h>
|
|
|
|
#define H 5
|
|
#define W 500
|
|
|
|
float kx[3]={0.25,0.5,0.25};
|
|
float ky[3]={0.25,0.5,0.25};
|
|
void applySeparableGaussianBlur(float src[H][W], float dst[H][W], int h, int w, float kx[3], float ky[3])
|
|
{
|
|
float buf[H][W-1]={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 = 2; i < h ; 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];
|
|
dst[i-1][j] = buf[i-2][j]*ky[0] + buf[i-1][j]*ky[1] + buf[i][j]*ky[2];
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
int main()
|
|
{
|
|
float inputImage[H][W]={0};
|
|
float dst[H][W]={0};
|
|
for(int i=0;i<H;i++)
|
|
{
|
|
for(int j=0;j<W;j++)
|
|
{
|
|
inputImage[i][j]=W*i+j+1;
|
|
dst[i][j]=W*i+j+1;
|
|
}
|
|
}
|
|
|
|
|
|
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",time_spent);
|
|
for(int i=0;i<H;i++)
|
|
{
|
|
for(int j=0;j<W;j++)
|
|
{
|
|
printf("%f ",dst[i][j]);
|
|
}
|
|
printf("\n");
|
|
}
|
|
|
|
}
|
|
|