Leonard Capacete
Leonard Capacete

Reputation: 1188

How to smooth an image with a 3x3 kernel

I am trying to smooth an image, by looping through its pixels, calculating the average of a 3x3 patch and then applying the average to all 9 pixels in this patch.

Code:

    import matplotlib.pyplot as plt
    import numpy as np
    import cv2 as cv
    from PIL import Image


    # 1. Load Image
    name = 'ebay.png'
    img = cv.imread(name) #import image

    h, w = img.shape[:2]


    # 2. Smooth with kernel size 3

    for y in range(0, w, 3):
        for x in range(0, h, 3):     
            px1 = img[x][y] #0/0
            px2 = img[x][y+1] #0/1
            px3 = img[x][y+2] #0/2
            px4 = img[x+1][y] #1/0
            px5 = img[x+1][y+1] #1/1
            px6 = img[x+1][y+2] #1/2
            px7 = img[x+2][y] #2/0
            px8 = img[x+2][y+1] #2/1
            px9 = img[x+2][y+2] #2/2

            average = np.average(px1 + px2 + px3 + px4 + px5 + px6 + px7 + px8 + px9)    

            img[x][y] =  average  #0/0
            img[x][y+1] = average   #0/1
            img[x][y+2] = average   #0/2
            img[x+1][y] = average   #1/0
            img[x+1][y+1] = average   #1/1
            img[x+1][y+2] = average  #1/2
            img[x+2][y] = average  #2/0
            img[x+2][y+1] = average   #2/1
            img[x+2][y+2] = average  #2/2

# 3. Transform the resulting image into pgm format and save result        

new_image = Image.fromarray(img)
new_image.save('new.png')

# 4. Show image

new_image.show()

However this just makes my new image just very pixely and not smooth at all. 

I am assuming that I am doing something wrong here:

average = np.average(px1 + px2 + px3 + px4 + px5 + px6 + px7 + px8 + px9)

since, when I am using just px5 as an average the new image looks much better (but still not very smoothy). Please see image below:

Original Image: Original Image

What my code is doing right now: What my code is doing right now...

Result when I am using px5 as average: Result when I am using px5 as average

Result when adding all px's and dividing by 9: enter image description here

Upvotes: 3

Views: 4305

Answers (2)

Leonard Capacete
Leonard Capacete

Reputation: 1188

So I had two issues here, which I was able to figure out thanks to @Ernie Yang and @Cris Luengo. Thank you very much for your help!

1) The issue of my average calculation was that it was overflowing I was summing up the pixel values. That is why the result was looking weird since it was wrapping around. So I had to change:

average = np.average(px1 + px2 + px3 + px4 + px5 + px6 + px7 + px8 + px9)

to:

average = px1/9. + px2/9. + px3/9. + px4/9. + px5/9. + px6/9. + px7/9. + px8/9. + px9/9.

2) However, this did not smooth my image, since I was just assigning the average value to all 9 pixels within the patch. So this caused the picture to be pixelated and not smoothed. Therefore I had to write the result of the average only to the middle pixel, not to all 3x3 pixels in the neighborhood. I also had to write it to a separate output image. You cannot do this operation in place, as it will affect the result for the later pixels.

Correct code sample:

    import matplotlib.pyplot as plt
    import numpy as np
    import cv2 as cv
    from PIL import Image
    import scipy.ndimage as ndimage
    from scipy.ndimage.filters import gaussian_filter


    # 1. Load Image
    name = 'ebay.png'
    img = cv.imread(name) #import image


    h, w = img.shape[:2]
    smoothedImage = cv.imread(name) #initialize second image


    # 2. Smooth with with kernel size 3

    for y in range(0, w-2):
        for x in range(0, h-2):     
            px1 = img[x][y] #0/0
            px2 = img[x][y+1] #0/1
            px3 = img[x][y+2] #0/2
            px4 = img[x+1][y] #1/0
            px5 = img[x+1][y+1] #1/1
            px6 = img[x+1][y+2] #1/2
            px7 = img[x+2][y] #2/0
            px8 = img[x+2][y+1] #2/1
            px9 = img[x+2][y+2] #2/2

            average = px1/9. + px2/9. + px3/9. + px4/9. + px5/9. + px6/9. + px7/9. + px8/9. + px9/9.

            smoothedImage[x+1][y+1] = average   #1/1

    # 3. Transform the resulting image into pgm format and save result        

    new_image = Image.fromarray(smoothedImage)
    new_image.save('new.png')

    # 4. Show image

    new_image.show()

Original Image: enter image description here

Smoothed Image: enter image description here

Edit:

Hey folks, I am back from my afternoon nap. I had quite a few interesting thoughts, here is my improved code now:

import matplotlib.pyplot as plt
import numpy as np
import cv2 as cv
from PIL import Image
import scipy.ndimage as ndimage
from scipy.ndimage.filters import gaussian_filter


# 1. Load Image
name = 'ebay.png'
img = cv.imread(name) #import image
h, w = img.shape[:2]
kernel = 5
radius = (kernel-1)/2

img2 = np.zeros((h, w, 3), dtype = 'uint8') #new image to paint on

    def pxIsInImgRange(x, y):
        if (0<=x) and (x < w): 
                if (0<=y) and (y < h):
                    return True
        return False

    # 2. Smoothing the shit out

    for x in range (-radius, w+radius):
        for y in range (-radius, h+radius):

            if pxIsInImgRange(x,y): 

                    px = 0

                    for vx2 in range (-radius, radius+1):
                        for vy2 in range (-radius, radius+1):
                            x2 = x + vx2
                            y2 = y + vy2
                            if pxIsInImgRange(x2,y2):

                                px = px + (img[y2][x2]/float((kernel*kernel)))
                            else:
                                px = px + 0 


                    img2[y][x] = px

    # 3. Save image                

    new_image = Image.fromarray(img2)
    new_image.save('new.png')

    # 4. Show image

    new_image.show()

New result with kernel of 5:

enter image description here

Upvotes: 4

Ernie Yang
Ernie Yang

Reputation: 114

average = np.average(px1 + px2 + px3 + px4 + px5 + px6 + px7 + px8 + px9)

is acutally summing over the values not averaging those,

average = (px1 + px2 + px3 + px4 + px5 + px6 + px7 + px8 + px9)/9

should give you what you want.

Also for doing such task, scipy.signal.convolve2d is the best tool. See doc and example below.

https://www.google.com/search?q=scipy.signal.convolve2d&oq=scipy.signal.convolve2d&aqs=chrome..69i57j69i60&sourceid=chrome&ie=UTF-8

Upvotes: 2

Related Questions