[英]Gaussian Blur image processing c++
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: 我的声誉太低而无法发布图像,所以不知道如何全面向您展示正在发生的事情,但是,我可以将gyazo链接发布到图像:
https://gyazo.com/38fbe1abd442a3167747760866584655 - Original, https://gyazo.com/471693c49917d3d3e243ee4156f4fe12 - Output https://gyazo.com/38fbe1abd442a3167747760866584655-原始, https ://gyazo.com/471693c49917d3d3e243ee4156f4fe12-输出
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. 我已经使用OpenCV读取/保存图像,并得到了预期的结果。 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. 模糊不是很明显(由于图像分辨率高和内核小),但是如果仔细看,它看起来是正确的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.