高斯模糊图像处理c ++

时间:2017-02-12 10:32:35

标签: c++ image blur gaussian

在尝试为图像实现高斯模糊后,我遇到了输出图像看起来像原始图像的多个模糊版本(输入图像)的问题

我发布图片的声誉太低,所以不知道如何全面展示你发生了什么,但我可以发布一个gyazo链接到图像:

https://gyazo.com/38fbe1abd442a3167747760866584655 - 原创, https://gyazo.com/471693c49917d3d3e243ee4156f4fe12 - 输出

以下是一些代码:

int kernel[3][3] = { 1, 2, 1,
                   2, 4, 2,
                   1, 2, 1 };

void guassian_blur2D(unsigned char * arr, unsigned char * result, int width, int height)
{
    for (int row = 0; row < height; row++) 
    {
        for (int col = 0; col < width; col++) 
        {
            for (int k = 0; k < 3; k++) 
            {
                result[3 * row * width + 3 * col + k] = accessPixel(arr, col, row, k, width, height);
            }
        }
    }
}

int accessPixel(unsigned char * arr, int col, int row, int k, int width, int height) 
{
    int sum = 0;
    int sumKernel = 0;

    for (int j = -1; j <= 1; j++) 
    {
        for (int i = -1; i <= 1; i++) 
        {
            if ((row + j) >= 0 && (row + j) < height && (col + i) >= 0 && (col + i) < width) 
            {
                int color = arr[(row + j) * 3 * width + (col + i) * 3 + k];
                sum += color * kernel[i + 1][j + 1];
                sumKernel += kernel[i + 1][j + 1];
            }
        }
    }

    return sum / sumKernel;
}

图像已保存:

guassian_blur2D(inputBuffer, outputBuffer, width, height);

//Save the processed image
outputImage.convertToType(FREE_IMAGE_TYPE::FIT_BITMAP);
outputImage.convertTo24Bits();
outputImage.save("appleBlur.png");
cout << "Blur Complete" << endl;

任何帮助都会很棒,如果这也有助于我尝试将图像存储为灰度级,以便不保存任何颜色。

1 个答案:

答案 0 :(得分:2)

看起来问题不在您的模糊代码中,并且与保存或访问图像数据有关。

我使用OpenCV来读取/保存图像,并获得了预期的结果。这是一个片段:

cv::Mat3b img = cv::imread("path_to_img.png");
cv::Mat3b out = img.clone();

guassian_blur2D(img.data, out.data, img.cols, img.rows);

cv::imshow("img", img);
cv::imshow("out", out);
cv::waitKey(0);

以下是输入和输出图像: input output

模糊不是很明显(由于高图像分辨率和小内核),但如果仔细观察 - 它看起来是正确的。

相关问题