-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssignment2.py
142 lines (123 loc) · 4.73 KB
/
Assignment2.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
141
142
import sys
import os
import numpy as np
import skimage.io as skio
import matplotlib.pyplot as plt
import skimage as ski
def identityfunc(originalImage):
rows = originalImage.shape[0]
cols = originalImage.shape[1]
newIamge = np.zeros(originalImage.shape)
for x in range(0, rows-1):
for y in range(0, cols-1):
newIamge[x, y] = originalImage[x, y]
return newIamge
def negativefunc(originalImage):
rows = originalImage.shape[0]
cols = originalImage.shape[1]
newIamge = np.zeros(originalImage.shape)
for x in range(0, rows-1):
for y in range(0, cols-1):
newIamge[x, y] = (256-1)-originalImage[x, y]
return newIamge
def threshholdingfunc(originalImage, limit):
rows = originalImage.shape[0]
cols = originalImage.shape[1]
newIamge = np.zeros(originalImage.shape)
for x in range(0, rows-1):
for y in range(0, cols-1):
if (originalImage[x, y] >= limit):
newIamge[x, y] = 1
else:
newIamge[x, y] = 0
return newIamge
def imageScallingfunc(originalImage, a):
rows = originalImage.shape[0]
cols = originalImage.shape[1]
newIamge = np.zeros(originalImage.shape)
for x in range(0, rows-1):
for y in range(0, cols-1):
if (a * originalImage[x, y]) >= 255:
newIamge[x, y] = 255
else:
newIamge[x, y] = (a * originalImage[x, y])
return newIamge
def logTransformationfunc(originalImage, c):
rows = originalImage.shape[0]
cols = originalImage.shape[1]
newIamge = np.zeros(originalImage.shape)
for x in range(0, rows-1):
for y in range(0, cols-1):
newIamge[x, y] = c * np.log((originalImage[x, y] + 1))
return newIamge
def antiLogTransformationfunc(originalImage, c):
rows = originalImage.shape[0]
cols = originalImage.shape[1]
newIamge = np.zeros(originalImage.shape)
for x in range(0, rows-1):
for y in range(0, cols-1):
newIamge[x, y] = (np.exp(originalImage[x, y]) ** (1/c)) - 1
return newIamge
def powerLaw(originalImage, c, gamma):
originalImage = ski.img_as_float(originalImage)
rows = originalImage.shape[0]
cols = originalImage.shape[1]
newIamge = np.zeros(originalImage.shape)
for x in range(0, rows-1):
for y in range(0, cols-1):
newIamge[x, y] = c * (originalImage[x, y] ** gamma)
return newIamge
def inversePowerLaw(originalImage, c, gamma):
originalImage = ski.img_as_float(originalImage)
rows = originalImage.shape[0]
cols = originalImage.shape[1]
newIamge = np.zeros(originalImage.shape)
for x in range(0, rows-1):
for y in range(0, cols-1):
newIamge[x, y] = c * (originalImage[x, y] ** (1/gamma))
return newIamge
def contrastStrechingHelp(originalImage, a1, a2, a3, r1, r2):
if (originalImage > -1) and (originalImage < r1):
return originalImage * a1
elif (originalImage >= r1) and (originalImage <= r2):
return originalImage * a2
else:
if (originalImage * a3) < 256:
return originalImage * a3
else:
return 255
def contrastStreching(originalImage, a1, a2, a3, r1, r2):
rows = originalImage.shape[0]
cols = originalImage.shape[1]
newIamge = np.zeros(originalImage.shape)
for x in range(0, rows-1):
for y in range(0, cols-1):
newIamge[x, y] = contrastStrechingHelp(originalImage[x, y], a1, a2, a3, r1, r2)
return newIamge
def graylevelslicingHelp(originalImage, a, r1, r2, mode):
if (originalImage >= r1) and (originalImage <= r2):
return originalImage * a
else:
return originalImage * mode
def grayLevelSlicing(originalImage, a, r1, r2, mode): # Here mode decides whether to off all the other bits or just let them as it is.
rows = originalImage.shape[0]
cols = originalImage.shape[1]
newIamge = np.zeros(originalImage.shape)
for x in range(0, rows-1):
for y in range(0, cols-1):
newIamge[x, y] = graylevelslicingHelp(originalImage[x, y], a, r1, r2, mode)
return newIamge
image = skio.imread("moon.TIF", as_gray=True)
image1 = np.asarray(image)
#image2 = negativefunc(image1)
#image2 = identityfunc(image1)
#image2 = threshholdingfunc(image1, 200)
#image2 = threshholdingfunc(image1, 10)
#image2 = imageScallingfunc(image1, 1.1)
#image2 = logTransformationfunc(image1, 1)
#image2 = powerLaw(image1, 1, 3.0)
#image2 = inversePowerLaw(image1, 1, 0.6)
#image2 = contrastStreching(image1, 0.7, 1.1, 1.4, 100, 180)
#image2 = grayLevelSlicing(image1,1.5, 100, 150, 1)
# plt.imshow(image2, cmap="gray")
# plt.show()