首页 > 解决方案 > 不知道我的高斯滤波器出了什么问题

问题描述

我正在尝试实现一个程序来检测图像的边界,我正在采取的第一步是应用高斯滤波器来去除噪声。问题是我正在应用的滤镜只会使图像变暗并且不会像预期的那样模糊它。

这是我目前拥有的代码:

void gaussFilter(C_Image &imagen, float desviation) {
    float kernel[5][5];
    float sumaKernel = 0;
    float s = desviation * desviation;

    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 5; j++)
        {
            float xaxis = (1 / sqrt(2 * M_PI * s)) * pow(exp(-pow(i, 2)) / (2 * s), 1);
            float yaxis = (1 / sqrt(2 * M_PI * s)) * pow(exp(-pow(j, 2)) / (2 * s), 1);
            float gaussianFilter = xaxis * yaxis;
            kernel[i][j] = gaussianFilter; 
            sumaKernel += kernel[i][j];
        }
    }

    cout << "Suma kernel: " << sumaKernel << endl;

    //Normaizamos el kernel;

    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 5; j++)
        {
            kernel[i][j] /= sumaKernel;
        }
    }

    C_Image::IndexT rowN, colN, colorsN;
    C_Image::BMPFileInfo("barbara_gray.bmp", rowN, colN, colorsN);

    //filterCreation(kernel, 10);
    for (int x = 0; x < 5; x++)
    {
        for (int y = 0; y < 5; y++)
        {
            if (y != 4) {
                printf("%f|", kernel[x][y]);
            }
            else
            {
                printf("%f|\n", kernel[x][y]);
            }
        }
    }

    cout << "Numero de columnas: " << colN << "\nNumero de filas: " << rowN << "\nColores de la paleta: " << colorsN << endl;
    float suma;

    for (int x = 1; x <= imagen.LastRow(); x++)
    {
        for (int y = 1; y <= imagen.LastCol(); y++)
        { 
            suma = 0;

            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    suma += imagen(x, y) * kernel[i][j];

                }
            }
                    imagen(x, y) = suma;

        }
    }
}

这是我的主要内容:

int main(int argc, char** argv)
{
    char const* path = "barbara_gray.bmp";

    C_Image imagen;
    imagen.ReadBMP(path);
    gaussFilter(imagen, 10);
    char const* destinationPath = "result_image.bmp";
    imagen.WriteBMP(destinationPath);
} 

C_Image 是一个给定的类。

标签: c++

解决方案


推荐阅读