-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistequalizecolor.py
70 lines (57 loc) · 1.69 KB
/
histequalizecolor.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
import numpy as np
import cv2
import matplotlib.pyplot as plt
def equalizeHistogram(img, L):
m,n = img.shape
hist,bins = np.histogram(img.flatten(),L,[0,L])
cdf = hist.cumsum()
cdfmin = np.min(cdf[np.nonzero(cdf)])
cdf_normalized = (cdf-cdfmin).astype(float)/((m*n)-cdfmin).astype(float)
cdf_normalized[cdf_normalized < 0] = 0
heq = cdf_normalized*(L-1)
return heq
if __name__ == '__main__':
img = cv2.imread('name_image.jpg')
L = 256
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
v = hsv[:,:,2]
heq = equalizeHistogram(v, L)
veq = heq[v]
hsv[:,:,2] = veq
rgb = cv2.cvtColor(hsv, cv2.COLOR_HSV2RGB)
plt.figure(figsize=(15,15))
plt.subplot(1,2,1)
plt.title('Original image')
plt.imshow(img[...,::-1])
plt.axis('off')
plt.subplot(1,2,2)
plt.axis('off')
plt.title('Equalized image')
plt.imshow(rgb)
plt.figure(figsize=(15,3))
plt.subplot(1,3,1)
plt.title('Original Histogram (Red)')
plt.hist(img[:,:,2].flatten(),L,[0,L], color = 'r')#image, bins, range
plt.xlim([0,256])
plt.subplot(1,3,2)
plt.xlim([0,256])
plt.title('Original Histogram (Green)')
plt.hist(img[:,:,1].flatten(),L,[0,L], color = 'g')
plt.subplot(1,3,3)
plt.xlim([0,256])
plt.title('Original Histogram (Blue)')
plt.hist(img[:,:,0].flatten(),L,[0,L], color = 'b')
plt.figure(figsize=(15,3))
plt.subplot(1,3,1)
plt.title('Histogram Equalized (Red)')
plt.hist(rgb[:,:,0].flatten(),L,[0,L], color = 'r')#image, bins, range
plt.xlim([0,256])
plt.subplot(1,3,2)
plt.xlim([0,256])
plt.title('Histogram Equalized (Green)')
plt.hist(rgb[:,:,1].flatten(),L,[0,L], color = 'g')
plt.subplot(1,3,3)
plt.xlim([0,256])
plt.title('Histogram Equalized (Blue)')
plt.hist(rgb[:,:,2].flatten(),L,[0,L], color = 'b')
plt.show()