高斯模糊问题

时间:2011-03-05 01:41:41

标签: image-processing photoshop blur gaussian

我正在写一个高斯滤波器,我的目标是尽可能地匹配photoshop中的高斯模糊滤波器。这是我的第一次图像处理工作。我遇到的一些问题是......

使用我的滤镜进一步模糊图像使其变暗,而photoshop似乎会使其变亮。

我使用的偏差值(我的代码中为“sigma”)是r / 3,这导致高斯曲线在矩阵内接近约0.0001 ...有更好的方法来确定这个值吗?

Photoshop(或大多数人)如何处理此类模糊的图像边框?

int matrixDimension = (radius*2)+1;
float sigma = radius/3;
float twoSigmaSquared = 2*pow(sigma, 2);
float oneOverSquareRootOfTwoPiSigmaSquared = 1/(sqrt(M_PI*twoSigmaSquared));

float kernel[matrixDimension];

int index = 0;
for (int offset = -radius; offset <= radius; offset++) {

    float xSquared = pow(offset, 2);
    float exponent = -(xSquared/twoSigmaSquared);
    float eToThePower = pow(M_E, exponent);
    float multFactor = oneOverSquareRootOfTwoPiSigmaSquared*eToThePower;

    kernel[index] = multFactor;

    index++;
}

//Normalize the kernel such that all its values will add to 1
float sum = 0;
for (int i = 0; i < matrixDimension; i++) {
    sum += kernel[i];
}
for (int i = 0; i < matrixDimension; i++) {
    kernel[i] = kernel[i]/sum;
}

//Blur horizontally
for (int row = 0; row < imageHeight; row++) {
    for (int column = 0; column < imageWidth; column++) {

        int currentPixel = (row*imageWidth)+column;

        int sum1 = 0;
        int sum2 = 0;
        int sum3 = 0;
        int sum4 = 0;

        int index = 0;
        for (int offset = -radius; offset <= radius; offset++) {
            if (!(column+offset < 0) && !(column+offset > imageWidth-1)) {

                int firstByteOfPixelWereLookingAtInSrcData = (currentPixel+offset)*4;

                int in1 = srcData[firstByteOfPixelWereLookingAtInSrcData];
                int in2 = srcData[firstByteOfPixelWereLookingAtInSrcData+1];
                int in3 = srcData[firstByteOfPixelWereLookingAtInSrcData+2];
                int in4 = srcData[firstByteOfPixelWereLookingAtInSrcData+3];

                sum1 += (int)(in1 * kernel[index]);
                sum2 += (int)(in2 * kernel[index]);
                sum3 += (int)(in3 * kernel[index]);
                sum4 += (int)(in4 * kernel[index]);
            }

            index++;
        }

        int currentPixelInData = currentPixel*4;

        destData[currentPixelInData] = sum1;
        destData[currentPixelInData+1] = sum2;
        destData[currentPixelInData+2] = sum3;
        destData[currentPixelInData+3] = sum4;

    }
}

//Blur vertically
for (int row = 0; row < imageHeight; row++) {
    for (int column = 0; column < imageWidth; column++) {

        int currentPixel = (row*imageWidth)+column;

        int sum1 = 0;
        int sum2 = 0;
        int sum3 = 0;
        int sum4 = 0;

        int index = 0;
        for (int offset = -radius; offset <= radius; offset++) {
            if (!(row+offset < 0) && !(row+offset > imageHeight-1)) {

                int firstByteOfPixelWereLookingAtInSrcData = (currentPixel+(offset*imageWidth))*4;

                int in1 = destData[firstByteOfPixelWereLookingAtInSrcData];
                int in2 = destData[firstByteOfPixelWereLookingAtInSrcData+1];
                int in3 = destData[firstByteOfPixelWereLookingAtInSrcData+2];
                int in4 = destData[firstByteOfPixelWereLookingAtInSrcData+3];

                sum1 += (int)(in1 * kernel[index]);
                sum2 += (int)(in2 * kernel[index]);
                sum3 += (int)(in3 * kernel[index]);
                sum4 += (int)(in4 * kernel[index]);
            }

            index++;
        }

        int currentPixelInData = currentPixel*4;

        finalData[currentPixelInData] = sum1;
        finalData[currentPixelInData+1] = sum2;
        finalData[currentPixelInData+2] = sum3;
        finalData[currentPixelInData+3] = sum4;

    }
}

2 个答案:

答案 0 :(得分:8)

要对滤波器进行反向工程,您需要找到其脉冲响应。在非常暗的值(例如32)的背景上,放置一个近似白色的像素,比如说223.您不想使用0和255,因为某些过滤器会尝试创建超出起始值的值。在此图像上运行过滤器,并获取输出值并将它们从0.0扩展到1.0:(value-32)/(223-32)。现在您拥有模拟过滤器所需的确切权重。

有很多方法可以处理图像边缘。我建议采用滤波器权重并对它们求和,然后将结果除以该总和;如果您试图超越边缘,请对该像素的像素值和滤镜权重使用0.0。

答案 1 :(得分:1)

边界条件有时取决于您正在做什么以及您正在使用什么类型的数据,但我认为对于通用图像处理,最好的做法是扩展值在图像边缘之外的边界处。当然不是字面意思,但如果过滤器试图读取图像边界外的像素,则用图像边缘上最近像素的值替换。这与将行夹在0和高度之间以及列在0和宽度之间的情况完全相同。