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.
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
image = Image.open("JimPicture.jpg")
new_size = (100, 100)
resized_image = image.resize(new_size)
plt.imshow(resized_image)
plt.show()
bw_image = resized_image.convert("L")
plt.imshow(bw_image)
plt.show()
i = np.array(bw_image)
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]]
Convolutions are filters that highlight features from an image
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
i_transformed = np.copy(i)
size_x = i_transformed.shape[0]
size_y = i_transformed.shape[1]
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
plt.imshow(i_transformed)
plt.show()
Pooling layers reduce the overall amount of information in an image while maintaining the features that are detected as present.
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]
plt.imshow(newImage)
plt.show()