-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImgUtils.py
More file actions
154 lines (124 loc) · 4.54 KB
/
ImgUtils.py
File metadata and controls
154 lines (124 loc) · 4.54 KB
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
import numpy as np
from PIL import Image
np.seterr(over='ignore')
class ImgUtils:
@staticmethod
def histogramEqualization(img: np.ndarray):
shape = img.shape
pixel_depth = 2**(img.dtype.itemsize * 8)
# no_channel = shape[-1]
# print(f"no_channels : {no_channel}")
img_copy = np.copy(img)
# separete each color channel
b, g, r = img_copy[:, :, 0], img_copy[:, :, 1], img_copy[:, :,
2]
# count each pixel value in each color channel
r_hist = np.bincount(r.flatten())
# print(F"r_hist.shape {r_hist.shape}")
# print(F"r_hist.shape {r_hist[255]}")
# print(F"r_hist.shape {(r == 255).sum()}")
g_hist = np.bincount(g.flatten())
b_hist = np.bincount(b.flatten())
r_sum = 0
g_sum = 0
b_sum = 0
r_cdf = [0] * pixel_depth
g_cdf = [0] * pixel_depth
b_cdf = [0] * pixel_depth
# compute CDF over each channel
for i in range(pixel_depth):
r_sum = r_sum + r_hist[i]
r_cdf[i] = r_sum
g_sum = g_sum + g_hist[i]
g_cdf[i] = g_sum
b_sum = b_sum + b_hist[i]
b_cdf[i] = b_sum
# print(f"r_hist {(r_hist[-1])}")
# print(f"r_cdf {(r_cdf[-1])}")
# print(f"g_cdf {(g_cdf[-1])}")
r_cdf_min = r_cdf[0]
g_cdf_min = g_cdf[0]
b_cdf_min = b_cdf[0]
# print(f"b_cdf_min {(b_cdf[0])}")
# print(f"b_cdf_min {(b_cdf_min)}")
r_fin = [0] * pixel_depth
g_fin = [0] * pixel_depth
b_fin = [0] * pixel_depth
tot_pixels = (shape[0] * shape[1])
# compute histogram equalization
for i in range(pixel_depth):
r_fin[i] = round(
((r_cdf[i] - r_cdf_min) /
(tot_pixels-r_cdf_min)) * (pixel_depth-1))
g_fin[i] = round(
((g_cdf[i] - g_cdf_min) /
(tot_pixels-g_cdf_min)) * (pixel_depth-1))
b_fin[i] = round(
((b_cdf[i] - b_cdf_min) /
(tot_pixels-b_cdf_min)) * (pixel_depth-1))
# map old pixel vals
# print(f"old r: {r}")
# print(f"old r: {r.shape}")
for i, pixel in np.ndenumerate(r):
r[i] = r_fin[r[i]]
g[i] = g_fin[g[i]]
b[i] = b_fin[b[i]]
# print(r_hist[0:10])
# print(r_cdf[0:10])
# print(r_fin[0:10])
# print(f"new r {r}")
# print(f"new r {r.shape}")
img_copy[:, :, 0] = b
img_copy[:, :, 1] = g
img_copy[:, :, 2] = r
# print(f"b_hist : {b_hist}")
# print(f"g_hist : {g_hist}")
# print(f"r_hist : {r_hist}")
print(img_copy.shape)
return img_copy
@staticmethod
def hybrid(img: np.ndarray, img2: np.ndarray) -> np.ndarray:
# check if dimensions matche
shape = img.shape
if (shape == img2.shape):
hybrid = np.zeros(shape, dtype=np.uint8)
for i, pixel in np.ndenumerate(img):
hybrid[i] = pixel + img2[i]
else:
raise Exception(f"input image must match size: {shape} ")
return hybrid
@staticmethod
def globalThresholding(img: np.ndarray, threshold: int) -> np.ndarray:
img_copy = np.copy(img)
pixel_depth = 2**(img.dtype.itemsize * 8)
print(f'pixel_depth: {pixel_depth}')
print(f'shape: {img.shape}')
if (0 <= threshold <= pixel_depth - 1):
for i, pixel in np.ndenumerate(img_copy):
img_copy[i] = (pixel_depth -
1) if pixel >= threshold else 0
else:
raise Exception(
f"Threshold valuse must be in range: [0,{pixel_depth - 1}]"
)
return img_copy
if __name__ == '__main__':
def imgReader(imgPath: str):
im = Image.open(imgPath)
im.show()
return np.asarray(im, dtype=np.uint8)
im1 = imgReader("./img1.jpg")
print(f"im1 shape: {im1.shape}")
# print(f"im1 type: {type(im1)}")
# im2 = imgReader("./img2.jpg")
# print(f"im2 shape: {im2.shape}")
# print(f"im2 type: {type(im2)}")
# im0 = np.zeros(im1.shape)
imUtil = ImgUtils(im1)
equalized = imUtil.histogramEqualization()
# thresholded = imUtil.globalThresholding(250)
# hybrid = imUtil.hybrid(im2)
# print(f"hybrid shape: {hybrid.shape}")
# print(f"hybrid type: {type(hybrid)}")
im3 = Image.fromarray(equalized.astype(np.uint8))
im3.show()