-
Notifications
You must be signed in to change notification settings - Fork 0
/
spatial_filtering.py
52 lines (40 loc) · 1.75 KB
/
spatial_filtering.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
41
42
43
44
45
46
47
48
49
50
51
52
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
def plot(subplot, image, title):
plt.subplot(subplot)
plt.imshow(image, cmap='gray')
plt.title(title)
plt.axis(False)
def average_filter(grayscale_image, kernel_size):
kernel = np.ones((kernel_size, kernel_size)) / (kernel_size ** 2)
return cv.filter2D(grayscale_image, -1, kernel)
def laplacian_filter(grayscale_image):
kernel = np.array([[1, 1, 1], [1, -8, 1], [1, 1, 1]])
return cv.filter2D(grayscale_image, cv.CV_64F, kernel)
def rescale_laplacian(grayscale_image):
grayscale_image = grayscale_image - np.min(grayscale_image)
return ((grayscale_image / np.max(grayscale_image)) * 255).astype(np.uint8)
def smoothen():
image = cv.imread('images/NoisyImage.png', cv.IMREAD_GRAYSCALE)
avg_image_7x7 = average_filter(image, 7)
gaussian_blur_11x11 = cv.GaussianBlur(image, (11, 11), 0)
gaussian_blur_17x17 = cv.GaussianBlur(image, (17, 17), 0)
plot(221, image, 'Orginal Image')
plot(222, avg_image_7x7, 'Averaging with 7x7 kernel')
plot(223, gaussian_blur_11x11, 'Averaging with 11x11 Gaussian kernel')
plot(224, gaussian_blur_17x17, 'Averaging with 17x17 Gaussian kernel')
plt.show()
def sharpen():
image = cv.imread('images/NoisyImage.png', cv.IMREAD_GRAYSCALE)
image = cv.GaussianBlur(image, (17, 17), 0)
laplacian = laplacian_filter(image)
scaled_laplacian = rescale_laplacian(laplacian)
sharpened_image = cv.addWeighted(image, 1.5, scaled_laplacian, -0.5, 0)
plot(221, image, 'Original Image')
plot(222, np.clip(laplacian, 0, 255), 'Laplacian Image')
plot(223, scaled_laplacian, 'Scaled Laplacian Image')
plot(224, sharpened_image, 'Sharpened Image')
plt.show()
# smoothen()
# sharpen()