You are on page 1of 5

4/26/2019 P02_M0517004_AHMAD SABILIL MAROMI

In [23]:

from skimage import data, io, filters


from skimage.morphology import disk
from scipy.signal import convolve2d as conv2d

import matplotlib.pyplot as plt


import numpy as np
%matplotlib inline

In [3]:

img = data.camera()
io.imshow(img, cmap='gray')
Out[3]:

<matplotlib.image.AxesImage at 0x7fb836341748>

localhost:8888/notebooks/P02_M0517004_AHMAD SABILIL MAROMI.ipynb 1/5


4/26/2019 P02_M0517004_AHMAD SABILIL MAROMI

In [7]:

sobel_kernel = np.array([
[-1,0,1],
[-2,0,2],
[-1,0,1]
])

img_sobel = conv2d(img, sobel_kernel, mode='same')


img_sobel = np.clip(img_sobel, 0, 255)
io.imshow(img_sobel, cmap='gray', vmin=0, vmax=255)
Out[7]:

<matplotlib.image.AxesImage at 0x7fb834a174a8>

In [10]:

size = 15
mean_kernel = np.ones((size,size)) / (size*size)
img_mean = conv2d(img, mean_kernel, mode='same')
img_mean = np.clip(img_mean, 0, 255)
io.imshow(img_mean, cmap='gray')
Out[10]:

<matplotlib.image.AxesImage at 0x7fb83479d278>

localhost:8888/notebooks/P02_M0517004_AHMAD SABILIL MAROMI.ipynb 2/5


4/26/2019 P02_M0517004_AHMAD SABILIL MAROMI

In [20]:

laplacian_kernel = np.array([
[0, 1, 0],
[1,-4, 1],
[0, 1, 0]
])*-1

laplacian_img = conv2d(img, laplacian_kernel, mode='same')


laplacian_img = np.clip(laplacian_img, 0, 255)
alpha = 5

enhanced_img = img + alpha * laplacian_img


enhanced_img = np.clip(enhanced_img, 0, 255)

io.imshow(enhanced_img, cmap='gray')
Out[20]:

<matplotlib.image.AxesImage at 0x7fb834195fd0>

localhost:8888/notebooks/P02_M0517004_AHMAD SABILIL MAROMI.ipynb 3/5


4/26/2019 P02_M0517004_AHMAD SABILIL MAROMI

In [22]:

h, w = img.shape
w_noise = np.random.random((h, w))
b_noise = np.random.random((h, w))

w_noise = (w_noise < 0.1) * 9999


b_noise = (b_noise < 0.1) * -9999

noisy_img = img + w_noise + b_noise


noisy_img = np.clip(noisy_img, 0, 255)

io.imshow(noisy_img, cmap='gray')
Out[22]:

<matplotlib.image.AxesImage at 0x7fb83404d9e8>

In [26]:

clean_img = filters.median(noisy_img, disk(30))


io.imshow(clean_img, cmap='gray')
Out[26]:

<matplotlib.image.AxesImage at 0x7fb82fdb2c50>

localhost:8888/notebooks/P02_M0517004_AHMAD SABILIL MAROMI.ipynb 4/5


4/26/2019 P02_M0517004_AHMAD SABILIL MAROMI

localhost:8888/notebooks/P02_M0517004_AHMAD SABILIL MAROMI.ipynb 5/5

You might also like