Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.

Commit d873a66

Browse files
authored
Merge pull request #165 from Namyalg/Image_filters
Image filters using 3-D convolution
2 parents 967a55b + e93737c commit d873a66

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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')
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
## Image filters using convolution ##
3+
4+
- Convolution is a technique widely used in image processing . Convolution is a mathematical operation on two functions that produces a third function expressing how the shape of one is modified by the other. (As defined in Wikipedia).
5+
6+
- Though this technique is widely used in image processing, in my implementation it can be used as an image filter.
7+
8+
- Images are made of pixels, colored images have 3 streams (red, green, blue), these can be visualised as a matrix which is 3 dimensional
9+
- Each of the red, blue and green are streams, stacked together to form a cube
10+
11+
- A kernel or a smaller matrix, in this case a 3X3 matrix is used to perform convolution.
12+
13+
- The kernel is used to perform dot product with the image. The resultant is an image which has different properties.
14+
15+
- This script written in Python, implements convolution over colored images.
16+
17+
- The end result is the application of filters to images.
18+
19+
- I have used tensorflow, numpy and the image processing library.
20+
21+
- The images attached show an image before and after blurring.
22+
23+
![Image](./images/before.png)
24+
25+
- This is before applying convolution.
26+
27+
![Image](./images/after.png)
28+
29+
- This is after applying convolution.
30+
Loading
Loading

0 commit comments

Comments
 (0)