如何使高斯模糊正常工作?
How to make Gaussian Blur work properly?
我正在编写一个带有 3x3 矩阵(内核)的高斯模糊滤镜,并使用 QImage
作为外部库。内核是按必须计算的。内核初始化和计算后,我将 unsigned int
矩阵带入程序:
typedef std::vector<std::vector<unsigned int> > uintMatrix; // For copying pixels value
在 main.cpp 中初始化如下:
uintMatrix tmpPxMat(image.width(); std::vector<unsigned int> (image.height(), 0));
我用它作为模糊像素值的临时存储。
然后,我用我的算法对图像进行模糊处理:
// Blur Pixels
for(int x = 0; x < image.width(); x++)
for(int y = 0; y < image.height(); y++) {
// To avoid edge cases don't process them, I'll fix it soon
if(x == 0 || x == image.height()-1 || y == 0 || y == image.width()-1)
continue;
// 3x3 Matrix
tmpPxMat[x][y] += kernel[0][0] * image.pixel(x-1, y-1) +
kernel[0][1] * image.pixel(x, y-1) +
kernel[0][2] * image.pixel(x+1, y-1);
tmpPxMat[x][y] += kernel[1][0] * image.pixel(x-1, y) +
kernel[1][1] * image.pixel(x, y) +
kernel[1][2] * image.pixel(x+1, y);
tmpPxMat[x][y] += kernel[2][0] * image.pixel(x-1, y+1) +
kernel[2][1] * image.pixel(x, y+1) +
kernel[2][2] * image.pixel(x+1, y+1);
}
然后,我将tmpPxMat[][]
的结果复制到原图:
// Copy blurred values to the image
for(int x = 0; x < image.width(); x++)
for(int y = 0; y < image.height(); y++)
image.setPixel(x, y, tmpPxMat[x][y]);
并保存:
image.save("/home/john/Pictures/blurred", "PNG", 100);
但最后我没有得到我想要的结果。这是我得到的:
之前/之后:
抱歉,问题描述太长了,但我已经尽量压缩了。
我假设 uintMatrix
是 32 位整数的二维数组,并且您已将红色、绿色和蓝色通道打包到其中。
如果是这样,那是你的问题。您需要单独模糊每个通道。
我正在编写一个带有 3x3 矩阵(内核)的高斯模糊滤镜,并使用 QImage
作为外部库。内核是按必须计算的。内核初始化和计算后,我将 unsigned int
矩阵带入程序:
typedef std::vector<std::vector<unsigned int> > uintMatrix; // For copying pixels value
在 main.cpp 中初始化如下:
uintMatrix tmpPxMat(image.width(); std::vector<unsigned int> (image.height(), 0));
我用它作为模糊像素值的临时存储。
然后,我用我的算法对图像进行模糊处理:
// Blur Pixels
for(int x = 0; x < image.width(); x++)
for(int y = 0; y < image.height(); y++) {
// To avoid edge cases don't process them, I'll fix it soon
if(x == 0 || x == image.height()-1 || y == 0 || y == image.width()-1)
continue;
// 3x3 Matrix
tmpPxMat[x][y] += kernel[0][0] * image.pixel(x-1, y-1) +
kernel[0][1] * image.pixel(x, y-1) +
kernel[0][2] * image.pixel(x+1, y-1);
tmpPxMat[x][y] += kernel[1][0] * image.pixel(x-1, y) +
kernel[1][1] * image.pixel(x, y) +
kernel[1][2] * image.pixel(x+1, y);
tmpPxMat[x][y] += kernel[2][0] * image.pixel(x-1, y+1) +
kernel[2][1] * image.pixel(x, y+1) +
kernel[2][2] * image.pixel(x+1, y+1);
}
然后,我将tmpPxMat[][]
的结果复制到原图:
// Copy blurred values to the image
for(int x = 0; x < image.width(); x++)
for(int y = 0; y < image.height(); y++)
image.setPixel(x, y, tmpPxMat[x][y]);
并保存:
image.save("/home/john/Pictures/blurred", "PNG", 100);
但最后我没有得到我想要的结果。这是我得到的:
之前/之后:
抱歉,问题描述太长了,但我已经尽量压缩了。
我假设 uintMatrix
是 32 位整数的二维数组,并且您已将红色、绿色和蓝色通道打包到其中。
如果是这样,那是你的问题。您需要单独模糊每个通道。