本文介绍了高斯模糊图像处理C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我的声誉太低,无法发布图像,所以不知道如何充分展示正在发生的事情,但是,我可以将gyazo链接发布到图像:

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

after trying to implement a Gaussian blur for an image i have ran into a problem where the output image looks like multiple blurred versions of the original image (input image)

I have too low of a reputation to post images so have no idea how to fully show you what is happening however, i can post a gyazo link to the image:

https://gyazo.com/38fbe1abd442a3167747760866584655 - Original,https://gyazo.com/471693c49917d3d3e243ee4156f4fe12 - Output

Here is some code:

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

Image is saved:

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;

Any help would be great, if this also helps i am trying to store the image as a grey-scale so that no colour is saved.

解决方案

Looks like the problem is not within your blurring code, and is related to saving or accessing image data.

I have used OpenCV to read/save images, and got expected result. Here's a snippet:

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

And here are input and output images:

The blur is not very noticeable (due to high image resolution and small kernel), but if you look carefully - it looks correct.

这篇关于高斯模糊图像处理C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 07:19