-
Notifications
You must be signed in to change notification settings - Fork 2
/
set2q7.py
40 lines (38 loc) · 1012 Bytes
/
set2q7.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import numpy as np
import imageio
from matplotlib import pyplot as plt
def convol(im,kernel):
ws,ws1=kernel.shape;
pd=int((ws-1)/2);
start=ws-pd-1;
row,col=im.shape
f=np.pad(im,[(pd,pd)],'reflect')
r,c=f.shape;
out=np.zeros([row,col])
for i in range(start,r-pd):
for j in range(start,c-pd):
window=f[(i-pd):(i+pd+1),(j-pd):(j+pd+1)];
m=np.multiply(kernel,window)
su=np.sum(np.ravel(m))
out[i-start,j-start]=su;
return(out)
img = imageio.imread('lena_RGB.tif')
m,n,o=img.shape
out=np.zeros([m,n,o])
R=img[:,:,0]
G=img[:,:,1]
B=img[:,:,2]
ws=7
kernel = np.ones((ws,ws))/(ws*ws)
R1=convol(R,kernel)
G1=convol(G,kernel)
B1=convol(B,kernel)
out[:,:,0]=R1
out[:,:,1]=G1
out[:,:,2]=B1
plt.imshow(np.uint8(out))
plt.subplot(121);plt.imshow(img);plt.title('Orginal Image');
plt.xticks([]), plt.yticks([])
plt.subplot(122);plt.imshow(np.uint8(out));plt.title('Smoothened Image');
plt.xticks([]), plt.yticks([])
plt.show()