尝试在C中实现高斯滤波器 [英] Trying to implement Gaussian Filter in C

查看:78
本文介绍了尝试在C中实现高斯滤波器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在C语言中实现高斯滤波器.我的输出布局总是出错,我尝试在for循环中处理行和列,但是没有用.输出布局应如下所示:

I am trying to implement the Gaussian Filter in C. My output layout keeps coming out wrong, I tried playing with the rows and columns in my for loops but it didn't work. The output layout should look like this:

0.0161464   0.0294206   0.0359344   0.0294206   0.0161464   
0.0294206   0.0536078   0.0654768   0.0536078   0.0294206   
0.0359344   0.0654768   0.0799735   0.0654768   0.0359344   
0.0294206   0.0536078   0.0654768   0.0536078   0.0294206   
0.0161464   0.0294206   0.0359344   0.0294206   0.0161464 

(这只是高斯滤波器布局的一个示例).

(This is just an example of of a Gaussian filter layout).

这是我在程序中得到的输出布局:

Here the output layout I am getting in my program:

0.114986 0.101475 0.069743 0.037331 0.015562
0.101475 0.089551 0.061548 0.032944 0.013733
0.069743 0.061548 0.042301 0.022642 0.009439
0.037331 0.032944 0.022642 0.012119 0.005052 
0.015562 0.013733 0.009439 0.005052 0.002106

这是我程序的代码段:

for (i = 0; i < smooth_kernel_size; i++) {
    for (j = -0; j < smooth_kernel_size; j++) {
        gauss[i][j] = K * exp(((pow((i), 2) + pow((j), 2)) / ((2 * pow(sigma, 2)))) * (-1));
        sum += gauss[i][j]; 
    }
}
for (i = 0; i < smooth_kernel_size; i++) {
    for (j = 0; j < smooth_kernel_size; j++) {
        gauss[i][j] /= sum;
    }
}
for (i = 0; i < smooth_kernel_size; i++) {
    for (j = 0; j < smooth_kernel_size; j++) {
        printf("%f ", gauss[i][j]);
    }
    printf("\n");
}

将感谢您的任何建议!

推荐答案

您的计算不正确:过滤器应以原点为中心.这是一个更正的版本:

Your computation is incorrect: the filter should be centered on the origin. Here is a corrected version:

#include <math.h>
#include <stdio.h>

#define smooth_kernel_size 5
#define sigma 1.0
#define K  1

int main() {
    double gauss[smooth_kernel_size][smooth_kernel_size];
    double sum = 0;
    int i, j;

    for (i = 0; i < smooth_kernel_size; i++) {
        for (j = 0; j < smooth_kernel_size; j++) {
            double x = i - (smooth_kernel_size - 1) / 2.0;
            double y = j - (smooth_kernel_size - 1) / 2.0;
            gauss[i][j] = K * exp(((pow(x, 2) + pow(y, 2)) / ((2 * pow(sigma, 2)))) * (-1));
            sum += gauss[i][j];
        }
    }
    for (i = 0; i < smooth_kernel_size; i++) {
        for (j = 0; j < smooth_kernel_size; j++) {
            gauss[i][j] /= sum;
        }
    }
    for (i = 0; i < smooth_kernel_size; i++) {
        for (j = 0; j < smooth_kernel_size; j++) {
            printf("%f ", gauss[i][j]);
        }
        printf("\n");
    }
    return 0;
}

输出:

0.002969 0.013306 0.021938 0.013306 0.002969
0.013306 0.059634 0.098320 0.059634 0.013306
0.021938 0.098320 0.162103 0.098320 0.021938
0.013306 0.059634 0.098320 0.059634 0.013306
0.002969 0.013306 0.021938 0.013306 0.002969

还要注意,可以简化主表达式:

Note also that the main expression can be simplified:

    gauss[i][j] = K * exp(-(x * x + y * y) / (2 * sigma * sigma));

这篇关于尝试在C中实现高斯滤波器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆