【发布时间】:2015-09-18 17:59:31
【问题描述】:
我正在编写一个带有 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);
但最后我没有得到我正在等待的结果。这是我得到的:
之前/之后:
很抱歉问题描述太长,但我已尽可能压缩它。
【问题讨论】:
-
你确定投射没有问题吗?高斯内核应该是一个浮点矩阵,卷积的结果是一个保存为 int 的浮点数。
-
纹理中图像的颜色是如何表示的?
-
您的输出图像是彩色的,但您的处理似乎并未处理三个颜色数据 (RGB) 通道,所以您是否对彩色图像使用灰度处理?
-
@Miki,我的内核是浮点矩阵,但临时存储不是。我已根据需要将其更改为浮动,但最后 - 我使用 setPixel() 函数,它接受 uint,我可以在计算值时避免强制转换,但可以't 将这些值设置为像素时。铸件应该很小,能这么伤画面吗?
-
这只是一个猜测.. 可能是错误的! @MarkSetchell 的问题似乎更合适
标签: c++ image-processing gaussian