Convolutions and Filters¶

A followed tutorial (with tweaks) from: https://youtu.be/x_VrgWTKkiM

Convolutions/filters are applied to images to enhance features, before pooling reduces the image size whilst still maintaining them.

Import Libraries¶

In [1]:
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt

Import Image and Resize¶

In [2]:
image = Image.open("JimPicture.jpg")
In [3]:
new_size = (100, 100)
resized_image = image.resize(new_size)
In [4]:
plt.imshow(resized_image)
plt.show()

Convert to Black and White, and to a 2D Array¶

In [5]:
bw_image = resized_image.convert("L")
In [6]:
plt.imshow(bw_image)
plt.show()
In [7]:
i = np.array(bw_image)
In [8]:
print(i)
[[255 255 255 ... 255 255 255]
 [255 255 255 ... 255 255 255]
 [255 255 255 ... 255 255 255]
 ...
 [ 19  12  23 ... 236 255 255]
 [ 52  13  20 ... 119 198 255]
 [ 69   5  16 ... 147  77 218]]

Create Filter (convolution) and Apply To Image¶

Convolutions are filters that highlight features from an image

In [9]:
filter = [ [-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]
weight = 1

# If your filter values don't sum to 0 or 1, then it's good practice to normalize them using a weight variable
In [10]:
i_transformed = np.copy(i)
size_x = i_transformed.shape[0]
size_y = i_transformed.shape[1]
In [11]:
for x in range(1,size_x-1):
  for y in range(1,size_y-1):
      output_pixel = 0.0
      output_pixel = output_pixel + (i[x - 1, y-1] * filter[0][0])
      output_pixel = output_pixel + (i[x, y-1] * filter[0][1])
      output_pixel = output_pixel + (i[x + 1, y-1] * filter[0][2])
      output_pixel = output_pixel + (i[x-1, y] * filter[1][0])
      output_pixel = output_pixel + (i[x, y] * filter[1][1])
      output_pixel = output_pixel + (i[x+1, y] * filter[1][2])
      output_pixel = output_pixel + (i[x-1, y+1] * filter[2][0])
      output_pixel = output_pixel + (i[x, y+1] * filter[2][1])
      output_pixel = output_pixel + (i[x+1, y+1] * filter[2][2])
      output_pixel = output_pixel * weight
      if(output_pixel<0):
        output_pixel=0
      if(output_pixel>255):
        output_pixel=255
      i_transformed[x, y] = output_pixel
In [12]:
plt.imshow(i_transformed)
plt.show()  

Max Pooling¶

Pooling layers reduce the overall amount of information in an image while maintaining the features that are detected as present.

In [13]:
new_x = int(size_x/2)
new_y = int(size_y/2)
newImage = np.zeros((new_x, new_y))
for x in range(0, size_x, 2):
  for y in range(0, size_y, 2):
    pixels = []
    pixels.append(i_transformed[x, y])
    pixels.append(i_transformed[x+1, y])
    pixels.append(i_transformed[x, y+1])
    pixels.append(i_transformed[x+1, y+1])
    pixels.sort(reverse=True)
    newImage[int(x/2),int(y/2)] = pixels[0]
In [14]:
plt.imshow(newImage)
plt.show()