高斯模糊图像处理c++

Gaussian Blur image processing c++

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

我对 post 图片的声誉太低,所以不知道如何向您充分展示正在发生的事情,但是,我可以 post gyazo link 图片:

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;

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

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

我已经使用 OpenCV 处理 read/save 图像,并得到了预期的结果。这是一个片段:

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);

这里是输入和输出图像:

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