编辑代码

#include <stdio.h>
float kernel[3][3]={
    {1.0/16,2.0/16,1.0/16},
    {2.0/16,4.0/16,2.0/16},
    {1.0/16,2.0/16,1.0/16},
};
#define width 10
#define height 10
void appGaussianFilter(float image[width][height])
{
    float result[width][height]={0};
    for(int x=1,x<width-1;x++)
    {
        for(int y=1;y<height-1;y++)
        {
            float sum=0.0;
            for(int i=-1;i<=1;i++)
            {
                for(int j=-1;j<=1;j++)
                    sum+=image[x+i][y+j]*kernel[i+1][j+1];
            }
            result[x][y]=sum;
        }
    }
    for(int x=1;x<width-1;x++)
    {
        for(int y=1;y<height-1;y++)
            image[x][y]=result[x][y];
    }
}
int main()
{
    float image[width][height]={
        {128,150,145,156,134,128,120,115,122,128},
        {140,124,165,106,114,138,121,175,132,120},
        {118,170,195,128,110,128,130,137,131,103},
        {128,150,145,156,134,128,120,115,122,128},
        {128,150,145,156,134,128,120,115,122,128},
        {128,150,145,156,134,128,120,115,122,128},
        {100,95,105,111,114,109,101,111,112,101},
        {92,95,97,100,84,81,82,90,88,100},
        {87,88,90,85,82,79,75,77,80,82},
        {80,75,71,72,71,72,72,75,71,80}
    };
    appGaussianFilter(image);
    printf("Filtered image:\n");
    for(int x=0;x<width;x++)
    {
        for(int y=0;y<height;y++)
            printf("%2.f",image[x][y]);
        printf("\n");
    }
    return 0;
}