-
Notifications
You must be signed in to change notification settings - Fork 1
/
Mean_Filter.py
46 lines (32 loc) · 844 Bytes
/
Mean_Filter.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Read the image
img = cv2.imread('cameraman.jpg')
# Generate Gaussian noise
noise = np.zeros(img.shape, img.dtype)
cv2.randn(noise, 0, 150) # mean = 0, standard deviation = 150
# Add the noise to the image
noisy_img = cv2.add(img, noise)
# Define the mean filter
w = np.ones((3, 3)) / 9
# Apply the mean filter to the noisy image
gd = cv2.filter2D(noisy_img, -1, w)
# Display the images
plt.figure(figsize=(15, 8))
plt.subplot(1, 3, 1)
plt.imshow(img)
plt.title('Original image')
plt.axis('off')
plt.subplot(1, 3, 2)
plt.imshow(noisy_img)
plt.title('Image with Gaussian noise')
plt.axis('off')
plt.subplot(1, 3, 3)
plt.imshow(gd)
plt.title('After applying the mean filter to the noisy image')
plt.axis('off')
plt.show()