-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhist_eq.py
More file actions
46 lines (43 loc) · 1.23 KB
/
hist_eq.py
File metadata and controls
46 lines (43 loc) · 1.23 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
'''
Equalizes histogram of an image with k levels. Takes image and number of levels as parameters.
Returns the new image.
'''
import numpy as np
import imutil
def hist_eq(im, levels):
h, w = im.shape
tot_pixs = h * w
im_hist = np.zeros((levels))
#count number of pixels for each level
for i in range(0,levels):
im_hist[i] = np.count_nonzero(im == i)
#find pdf
pdf = np.zeros((levels))
for i in range(0,levels):
pdf[i] = im_hist[i]/tot_pixs
#find cdf
cdf = np.zeros((levels))
cdf[0] = pdf[0]
for i in range(1, levels):
cdf[i] = pdf[i] + cdf[i-1]
im2 = np.zeros((h,w))
#transform
for j in range(0,h):
for k in range(0,w):
im2[j, k] = int(round((levels-1) * cdf[im[j, k]]))
return np.uint8(im2)
"""
Utility for hist_eq, handles the cases for gray and RGB images
Returns the new image
"""
def hist_eq_util(im, levels):
#detect if image is gray or rgb, do appropriate operations
im_out = im.copy()
if imutil.is_gray(im):
im = imutil.correct_gray(im)
im_out = hist_eq(im, levels)
else:
im_out = imutil.rgb2ycbcr(im)
im_out[:,:,0] = hist_eq(im_out[:,:,0], levels)
im_out = imutil.ycbcr2rgb(im_out)
return im_out