Gaussian Blur Questions

I am writing a Gaussian filter, and my goal is to bring the Gaussian blur filter in Photoshop as close as possible. This is my first image processing job. Some problems / questions I have ...

Further blurring the image with my filter darkens it, while Photoshop seems to make it easier.

Deviation value ("sigma" in my code) Using Im is r / 3, which leads to a Gaussian curve approaching approximately 0.0001 in the matrix ... is there a better way to determine this value?

How does Photoshop (or most people) handle image 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;

    }
}
+3
source share
2 answers

, . , , 32, , , 223. 0 255, . 0.0 1.0: (value-32)/(223-32). , .

. , ; , 0.0 .

+8

, , , , . , , , , . , 0 , 0 .

+1

Source: https://habr.com/ru/post/1796428/


All Articles