Home > Back-end >  Do I need to normalize image after blurring? (if the sum of gaussian filter is not 1)
Do I need to normalize image after blurring? (if the sum of gaussian filter is not 1)

Time:07-19

hi I smooth grayscale image using gaussian blur. And I wonder do i have to normalize after gaussian blur.

when i make 3x3 gaussian kernel, the kernel's value is [[0.05856 0.09656 0.05856] [0.09656 0.1592 0.09656] [0.05856 0.09656 0.05856]] and the sum of gaussian filter is 0.78.

I think if the sum of gaussian filter is 1, then i dont have to do normalization. but is this case, the sum is not 1.

so, does the normalization is necessary? or can I just clip the value to the range0~255

< python code >

def Gaussian_kernel(kernel_size=3, sig=1):
    gaussian = np.empty((kernel_size,kernel_size), dtype=np.float32)

    index = kernel_size//2  #center = (0,0) gaussian filter
    for i in range(-index , index 1):
        for j in range(-index, index 1):
            t1 = 1/(2*np.pi*sig**2)
            t2 = np.exp(-(i**2  j**2)/(2*sig**2))
            
            gaussian[i index][j index] = t1*t2

    return gaussian

def Gaussianblur(image,kernel,sig):
    H = image.shape[0]
    W = image.shape[1]
    blurImage = np.empty((H-kernel 1,W-kernel 1), dtype=np.uint16) 
    #stride=1, padding=0

    gau_kernel=Gaussian_kernel(kernel,sig)

    gaumax=1
    for i in range(H-kernel 1):
        for j in range(W-kernel 1):
            Gau=0
            for x in range(kernel):
                for y in range(kernel):
                    Gau  = gau_kernel[x][y] * image[i x][j y]

            blurImage[i][j] = Gau
            if Gau > gaumax : gaumax=Gau

    #normalization
    for i in range(H-kernel 1):
        for j in range(W-kernel 1):
            blurImage[i][j] *=255.0/gaumax
    #clip
    #blurImage=np.clip(blurImage,0,255)
    return blurImage

CodePudding user response:

Actually, we always normalize the Gaussian kernel so that the sum of gaussian filter is 1. For kernel with sum>1, the result will be brighter, vice versa.

CodePudding user response:

The brightness in your image has been multiplied by 0.78, it is 78% as bright as the input image. You need to divide by 0.78 to get the original brightness back. This is not the same thing as normalizing the image though.

Blurring kernels must sum to 1 to avoid a change in brightness.

  • Related