c++ - 不知道我的高斯滤波器出了什么问题
问题描述
我正在尝试实现一个程序来检测图像的边界,我正在采取的第一步是应用高斯滤波器来去除噪声。问题是我正在应用的滤镜只会使图像变暗并且不会像预期的那样模糊它。
这是我目前拥有的代码:
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 是一个给定的类。
解决方案
推荐阅读
- angular7 - 垫分页器问题
- python - Django:在模型的字段上设置 unique_together 以及字段条件
- oracle - 获取总数
- swiftui - 如何在swiftUI中登录页面后隐藏导航栏
- laravel - 插入数据时尝试获取非对象错误的属性“stock_name”
- javascript - 选择选项时调用 javascript 函数但在选项本身内部需要它
- javascript - 将 SVG 图标用作手风琴菜单中的单击图标时如何转换/反转 SVG 图标
- automation - BrowserStack-是否可以自动化测试`
- android - 删除android中折叠工具栏布局内进度条中的滚动效果
- php - 如何在单个 foreach 中使用两个数组