-
Notifications
You must be signed in to change notification settings - Fork 12
/
16_Smoothing_Images.py
55 lines (40 loc) · 1.71 KB
/
16_Smoothing_Images.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
53
54
55
"""
Smoothing images
Homogeneous filter is the most simple filter, each output pixel is the mean of its kernal neighbors.
Image blurring:
- Low Pass Filters(LPF): Help in removing noises, blurring the images.
- Averaging Filter
Edge detection:
- High Pass Filters(HPF): Help in finding the edges in the images
"""
# Import the packages
import cv2
import numpy as np
import matplotlib.pyplot as plt
# img = cv2.imread('./images/lena.jpg')
img = cv2.imread('./images/Noise_salt_and_pepper.png')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# Create the kernel
kernel = np.ones((5, 5), np.float32) / 25
# 2D Convolutional Filter
dst = cv2.filter2D(img, -1, kernel)
# Averaging filter
blur = cv2.blur(img, (5, 5))
# Gaussian filter
gaussian_blur = cv2.GaussianBlur(img, (5, 5), 0)
# If you want, you can create a Gaussian kernel with thefunction,
print("Gaussian Kernel is: ", cv2.getGaussianKernel((10), 0))
# Median filter: replacing each pixel's value with the median of its neighboring pixels. This method is great when dealing with "salt and pepper noise".
median = cv2.medianBlur(img, 5)
# Bilateral filter: a non-linear, edge-preserving, and noise-reducing smoothing filter for images.
# It is highly effective at noise removal while preserving edges. But the operation is slower compared to other filters.
bilateralFilter = cv2.bilateralFilter(img, 9, 75, 75)
titles = ['image', '2D Convolution', 'blur',
'GaussianBlur', 'median', 'bilateralFilter']
images = [img, dst, blur, gaussian_blur, median, bilateralFilter]
for i in range(6):
plt.subplot(2, 3, i+1), plt.imshow(images[i], 'gray')
plt.title(titles[i])
plt.xticks([]), plt.yticks([])
plt.show()