# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 2
#     language: python
#     name: python2
# ---

# ### Applying spatial filtering for blurring/noise reduction 

import cv2
from PIL import Image
import numpy as np

img = cv2.imread('bimage3.jpg', 2)
print img.shape
Image.fromarray(img)

arrayfilter = np.ones((9,9)) * 1/9
[height, width] = img.shape
newimg          = np.zeros(img.shape, np.uint8)

arraytemp       = np.zeros((9,9), np.int16)
[a,b]           = arraytemp.shape
b               = (a-1) / 2
# Refer to slide 15
aaxis           = np.linspace(-b,b,a).astype(np.int8)
baxis           = np.linspace(-b,b,a).astype(np.int8)
aaxis

# +
for y in range(height):
    for x in range(width):
        
        for s in aaxis:
            for t in baxis:
                
                if (y+s) < 0 and (x+t) < 0:
                    temp = img[y][x]
                elif (y+s) >= height and (x+t) >= width:
                    temp = img[y][x]
                elif (y+s) < 0 and (x+t) < width:
                    temp = img[y][x+t]
                elif (x+t) < 0 and (y+s) < height:
                    temp = img[y+s][x]
                elif (y+s) >= height and (x+t) < width:
                    temp = img[y][x+t]
                elif (x+t) >= width and (y+s) < height:
                    temp = img[y+s][x]                    
                else:
                    temp = img[y+s][x+t]
                    
                arraytemp[s+b][t+b] = arrayfilter[s+b][t+b] * temp
        
        newimg[y][x] = arraytemp.sum()
    print y                
                
        
# -

Image.fromarray(newimg)





StarcoderdataJupyterScriptsDedupFiltered