|
| 1 | +#This script implements convolution over images. |
| 2 | +#This can be used to apply filters to images. |
| 3 | + |
| 4 | +#Imports |
| 5 | + |
| 6 | +import numpy as np |
| 7 | +from PIL import Image |
| 8 | +import cv2 |
| 9 | + |
| 10 | +#size of image |
| 11 | +width = 300 |
| 12 | +height = 360 |
| 13 | + |
| 14 | +#kernels or image filters |
| 15 | +#This is to blur the image, experiment with different values, each of the streams can have different values |
| 16 | +'''b_kernel = np.array([0.11 , 0.11 , 0.11 , 0.11 , 0.11 , 0.11 , 0.11 , 0.11 , 0.11]).reshape(3,3) |
| 17 | +r_kernel = np.array([0.11 , 0.11 , 0.11 , 0.11 , 0.11 , 0.11 , 0.11 , 0.11 , 0.11]).reshape(3,3) |
| 18 | +g_kernel = np.array([0.11 , 0.11 , 0.11 , 0.11 , 0.11 , 0.11 , 0.11 , 0.11 , 0.11]).reshape(3,3) |
| 19 | +''' |
| 20 | +print("Enter 9 values for each of the streams") |
| 21 | +b_kernel = np.array(list(map(int, input("Enter the kernel to be applied to the blue stream").split()))).reshape(3,3) |
| 22 | +r_kernel = np.array(list(map(int, input("Enter the kernel to be applied to the red stream").split()))).reshape(3,3) |
| 23 | +g_kernel = np.array(list(map(int, input("Enter the kernel to be applied to the green stream").split()))).reshape(3,3) |
| 24 | + |
| 25 | +#Image is read and resized |
| 26 | +img = cv2.imread('path to image') |
| 27 | +img = cv2.resize(img, (width, height)) |
| 28 | + |
| 29 | +#The pixels are extracted from the image |
| 30 | +pixels = [] |
| 31 | +for i in range(height): |
| 32 | + for j in range(width): |
| 33 | + pixels.append(img[i, j]) |
| 34 | + #The RGB streams are stored in different arrays to facilitate the process of convolution |
| 35 | + b = [] |
| 36 | + g = [] |
| 37 | + r = [] |
| 38 | + for i in pixels: |
| 39 | + b.append(i[0]) |
| 40 | + g.append(i[1]) |
| 41 | + r.append(i[2]) |
| 42 | + |
| 43 | +#Each of the streams are resized |
| 44 | +r = np.array(r).reshape(height, width) |
| 45 | +b = np.array(b).reshape(height, width) |
| 46 | +g = np.array(g).reshape(height, width) |
| 47 | + |
| 48 | +#function to apply convolution or multiply kernels |
| 49 | +def convolute(arr, height, width, kernel): |
| 50 | + convoluted_matrix = [] |
| 51 | + for i in range(1, height-2): |
| 52 | + for j in range(1, width-2): |
| 53 | + temp = arr[i:i+3, j:j+3] |
| 54 | + prod = np.multiply(temp, kernel) |
| 55 | + convoluted_matrix.append(np.sum(prod)) |
| 56 | + |
| 57 | + convoluted_matrix = (np.array(convoluted_matrix).reshape(height-3, width-3)) |
| 58 | + return(convoluted_matrix) |
| 59 | + |
| 60 | +r_convol = convolute(r, height, width, r_kernel) |
| 61 | +g_convol = convolute(g, height, width, g_kernel) |
| 62 | +b_convol = convolute(b, height, width, b_kernel) |
| 63 | + |
| 64 | +width = width - 3 |
| 65 | +height = height - 3 |
| 66 | +combine = np.zeros([height, width, 3], dtype=np.uint8) |
| 67 | +for i in range(height): |
| 68 | + for j in range(width): |
| 69 | + combine[i, j] = [int(r_convol[i][j]), int(g_convol[i][j]), int(b_convol[i][j])] |
| 70 | + |
| 71 | +#The 3 matrices or streams are combined, and stored |
| 72 | +img = Image.fromarray(combine) |
| 73 | +img.save('Funkyfilter.jpg') |
0 commit comments