-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day9.py
142 lines (109 loc) · 4.06 KB
/
Day9.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np
# Median Filtering
img=cv.imread('cameraman.jpg')
girl=cv.imread('girl.jpg')
def addSaltAndPepperNoise(img):
s_and_p = np.random.rand(img.shape[0], img.shape[1])
# if we consider 5% salt and pepper noise, we'd like to have
# 2.5% salt and 2.5% pepper. thus:
salt = s_and_p > .975
pepper = s_and_p < .025
# in order to add some noise, we should turn off black (pepper) locations and
# turn on white (white) locations.
channel_2 = np.atleast_1d(img[:, :, 1])
noisy = np.zeros_like(channel_2)
for i in range(channel_2.shape[0] * channel_2.shape[1]):
if salt.ravel()[i] == 1:
noisy.ravel()[i] = 255
elif pepper.ravel()[i] == 1:
noisy.ravel()[i] = 0
else:
noisy.ravel()[i] = channel_2.ravel()[i]
return noisy
def RemoveMedianFiltering(img):
# apply median filter with size 3
Median3 = cv.medianBlur(img, 3)
Median5 = cv.medianBlur(img, 5)
# Display the results
fig = plt.figure(figsize=(14, 14), dpi=80, facecolor='w', edgecolor='k')
plt.subplot(131), plt.xticks([]), plt.yticks([])
plt.title("Salt And Pepper Noise")
plt.imshow(img, cmap='gray'), plt.grid(False)
plt.subplot(132), plt.xticks([]), plt.yticks([])
plt.title("3x3")
plt.imshow(Median3, cmap='gray'), plt.grid(False)
plt.subplot(133), plt.xticks([]), plt.yticks([])
plt.title("5x5")
plt.imshow(Median5, cmap='gray'), plt.grid(False)
plt.show()
# NoisyImage=addSaltAndPepperNoise(img)
# RemoveMedianFiltering(NoisyImage)
# For Image clean Image
# RemoveMedianFiltering(girl)
# For Noisy Image
def GaussianFiltering(ref):
mean = 0
sigma = 20.0
gauss_noise = np.random.normal(mean, sigma, (ref.shape[0], ref.shape[1]))
# Convert RGB image to Grayscale image using cvtColor()
gray = cv.cvtColor(ref, cv.COLOR_BGR2GRAY)
# Add gaussian noise to the image
g_noisy = gray + gauss_noise # Gaussian noisy image
# Showing gray image, noise image, and noisy image
fig = plt.figure(figsize=(14, 14), dpi=80, facecolor='w', edgecolor='k')
plt.subplot(131), plt.xticks([]), plt.yticks([])
plt.imshow(gray, cmap='gray'), plt.grid(False)
plt.subplot(132), plt.xticks([]), plt.yticks([])
plt.imshow(gauss_noise, cmap='gray'), plt.grid(False)
plt.subplot(133), plt.xticks([]), plt.yticks([])
plt.imshow(g_noisy, cmap='gray'), plt.grid(False)
plt.show()
# GaussianFiltering(img)
# For Bright we use log
# For Dark we use antilog
# For Same we use identiy
# Image Arithmetics
import matplotlib.pyplot as plt
import numpy as np
def ImageArithmetics():
skull = cv.imread('Skull.PNG')
skullMask = cv.imread('skullMask.PNG')
# plt.figure(figsize=(10, 10))
# plt.subplot(131), plt.imshow(skull, cmap='gray')
# plt.subplot(132), plt.imshow(skullMask - 20, cmap='gray')
# plt.show()
plt.figure(figsize=(10, 10))
plt.subplot(131), plt.imshow(skullMask - skull, cmap='gray')
plt.subplot(132), plt.imshow(-(skullMask - skull + 128), cmap='gray')
plt.subplot(133), plt.imshow(skullMask - skull + 128, cmap='gray')
plt.show()
# ImageArithmetics()
def TungstenShading():
Tungsten = cv.imread('Tungsten.PNG')
Shading = cv.imread('shading.PNG')
plt.figure(figsize=(10, 10))
plt.subplot(131), plt.imshow(Tungsten, cmap='gray')
plt.title("Original")
plt.subplot(132), plt.imshow(np.multiply(Tungsten, 1 / Shading), cmap='gray')
plt.title("Shaded Image")
plt.subplot(133), plt.imshow(Shading, cmap='gray')
plt.title("Shade")
s=np.multiply(Tungsten, 1 / Shading)
print(s)
plt.show()
# TungstenShading()
fox = cv.imread('fox.jpg')
huji = cv.imread('huji.png')
plt.figure(figsize=(10, 10))
plt.subplot(131), plt.imshow(fox, cmap='gray')
plt.title("Original")
plt.subplot(132), plt.imshow(np.multiply(fox, 1 / huji), cmap='gray')
plt.title("Shaded Image")
plt.subplot(133), plt.imshow(huji, cmap='gray')
plt.title("Shade")
s=np.multiply(fox, 1 / huji)
print(s)
cv.imshow("sad",np.multiply(fox, 1 / huji))
cv.waitKey(0)