开发者

Gaussian Blur Questions

开发者 https://www.devze.com 2023-02-14 18:46 出处:网络
I\'m writing a gaussian filter, and my goal is to match the gaussian blur filter in photoshop as closely as possible. This is my first image processing endeavor. Some problems/questions I have are...

I'm writing a gaussian filter, and my goal is to match the gaussian blur filter in photoshop as closely as possible. This is my first image processing endeavor. Some problems/questions I have are...

Further blurring an image with my filter darkens it, while photoshop’s seems to lighten it.

The deviation value (“sigma,” in my code) I’m using is r/3, which results in the gaussian curve having approached about 0.0001 within the matrix...is there a better way to determine this value?

How does photoshop (or most people) handle i开发者_运维知识库mage borders for this type of blur?

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;

    }
}


To reverse engineer a filter, you need to find its impulse response. On a background of a very dark value, say 32, place a nearly white pixel, say 223. You don't want to use 0 and 255 because some filters will try to create values beyond the starting values. Run the filter on this image, and take the output values and stretch them from 0.0 to 1.0: (value-32)/(223-32). Now you have the exact weights needed to emulate the filter.

There are lots of ways to treat the image edges. I would suggest taking the filter weights and summing them, then dividing the result by that sum; if you're trying to go beyond the edge, use 0.0 for both the pixel value and the filter weight on that pixel.


Boundary conditions sometimes depend on exactly what you're doing and what kind of data you're working with, but I think for general purpose image manipulation the best thing to do is to extend the values at the borders beyond the edges of the image. Not literally of course, but if the filter tries to read a pixel that's outside the image borders, you substitute the value of the nearest pixel on the edge of the image. Which is really the same as just clamping the row to be between 0 and height, and the column to be between 0 and width.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号