-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperations.py
164 lines (129 loc) · 4.32 KB
/
operations.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
import math
max_intensity = 255
def convert_to_greyscale(img):
b, g, r = cv.split(img)
rows, cols = b.shape
img_out = np.zeros((rows, cols))
for i in range(rows):
for j in range(cols):
intensity = int((int(b[i][j]) + int(g[i][j]) + int(g[i][j])) / 3)
img_out[i, j] = intensity
return img_out
def plot_histogram(img):
img = convert_to_greyscale(img)
hist = [0] * 256
rows, cols = img.shape
for i in range(rows):
for j in range(cols):
intensity = int(img[i][j])
hist[intensity] = hist[intensity] + 1
x = range(len(hist))
plt.bar(x, hist)
plt.show()
def equalize(img):
hist = [0] * 256
rows, cols, channel = img.shape
for i in range(rows):
for j in range(cols):
intensity = int(round((img[i][j][0] + img[i][j][1] + img[i][j][2]) / 3))
hist[intensity] = hist[intensity] + 1
cumulative = [0] * len(hist)
for i in range(len(hist)):
if i == 0:
cumulative[i] = hist[i]
else:
cumulative[i] = cumulative[i - 1] + hist[i]
total = cumulative[len(cumulative) - 1]
new_hist = [0] * len(cumulative)
for i in range(len(cumulative)):
grey_level = round((cumulative[i] / total) * max_intensity)
new_hist[grey_level] = new_hist[grey_level] + hist[i]
x = range(len(hist))
plt.plot(x, new_hist)
plt.show()
def negative_image(img):
rows, cols, channel = img.shape
new_img = np.zeros((rows,cols,channel))
for i in range(rows):
for j in range(cols):
new_img[i,j,0] = max_intensity - img[i,j,0]
new_img[i,j,1] = max_intensity - img[i,j,1]
new_img[i,j,0] = max_intensity - img[i,j,2]
if new_img[i,j,0] < 0:
new_img[i,j,0] = 0
if new_img[i,j,1] < 0:
new_img[i,j,1] = 0
if new_img[i,j,2] < 0:
new_img[i,j,2] = 0
return new_img
def log_transform(img, c):
img = convert_to_greyscale(img)
rows, cols = img.shape
new_img = np.zeros((rows, cols))
for i in range(rows):
for j in range(cols):
intensity = round(c * math.log(1 + img[i, j]))
if intensity > 255:
intensity = 255
new_img[i, j] = intensity
return new_img
def power_transform(img, c, g):
img = convert_to_greyscale(img)
rows, cols = img.shape
new_img = np.zeros((rows, cols))
minn = 99999999999999
maxx = 0
for i in range(rows):
for j in range(cols):
s = (c * img[i][j]) ** g
if (s > maxx):
maxx = s
if (s < minn):
minn = s
for i in range(rows):
for j in range(cols):
s = (c * img[i][j]) ** g
s = (s - minn) / (maxx - minn)
s = s * 255
new_img[i, j] = s
return new_img
def contrast_stretch(img):
rows, cols, channels = img.shape
m_min = 256
m_max = -1
for i in range(rows):
for j in range(cols):
intensity = (img[i][j][0] + img[i][j][1] + img[i][j][2]) / 3
if intensity > m_max:
m_max = intensity
if intensity < m_min:
m_min = intensity
new_img = np.zeros((rows, cols, channels))
for i in range(rows):
for j in range(cols):
for c in range(channels):
intensity = round(((img[i, j, c] - m_min) / (m_max - m_min)) * 255)
if intensity < 0:
intensity = 0
new_img[i, j, c] = intensity
return new_img
# assuming 8 bit image
def bit_plane_slice(img, bit_plane):
rows, cols, channels = img.shape
img_size = (rows, cols)
bit_size = img_size + (8,)
b, g, r = cv.split(img)
b_bits = np.unpackbits(b).reshape(bit_size)
g_bits = np.unpackbits(g).reshape(bit_size)
r_bits = np.unpackbits(r).reshape(bit_size)
if bit_plane is not None:
b_bits[:, :, int(bit_plane)] = 0
g_bits[:, :, int(bit_plane)] = 0
r_bits[:, :, int(bit_plane)] = 0
b_aug = np.packbits(b_bits).reshape(img_size)
g_aug = np.packbits(g_bits).reshape(img_size)
r_aug = np.packbits(r_bits).reshape(img_size)
return cv.merge((b_aug, g_aug, r_aug))