-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorHistogramEqualization.py
More file actions
65 lines (57 loc) · 2.45 KB
/
ColorHistogramEqualization.py
File metadata and controls
65 lines (57 loc) · 2.45 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
import numpy as np
import cv2
from matplotlib import pyplot as plt
def ShowImgWithMatplotlib(colorImg, title, pos):
imgRGB=colorImg[:, :, ::-1]
ax=plt.subplot(3, 4, pos)
plt.imshow(imgRGB)
plt.title(title)
plt.axis('off')
def ShowHistWithMatplotlibRgb(hist, title, pos, color):
ax=plt.subplot(3, 4, pos)
plt.xlabel("bins")
plt.ylabel("number of pixels")
plt.xlim([0, 256])
for(h, c) in zip(hist, color):
plt.plot(h, color=c)
def HistColorImg(img):
histr=[]
histr.append(cv2.calcHist([img], [0], None, [256], [0, 256]))
histr.append(cv2.calcHist([img], [1], None, [256], [0, 256]))
histr.append(cv2.calcHist([img], [2], None, [256], [0, 256]))
return histr
def EqualizeHistColor(img):
channels=cv2.split(img)
eqChannels=[]
for ch in channels:
eqChannels.append(cv2.equalizeHist(ch))
eqImage=cv2.merge(eqChannels)
return eqImage
plt.figure(figsize=(18, 14))
plt.suptitle("Color histogram equalization with cv2.equalizeHist() - not a good approach", fontsize=14, fontweight='bold')
image=cv2.imread('images/lenna.png')
histColor=HistColorImg(image)
imageEq=EqualizeHistColor(image)
histImageEq=HistColorImg(imageEq)
M=np.ones(image.shape, dtype="uint8") * 15
addedImage=cv2.add(image, M)
histColorAddedImage=HistColorImg(addedImage)
addedImageEq=EqualizeHistColor(addedImage)
histAddedImageEq=HistColorImg(addedImageEq)
subtractedImage=cv2.subtract(image, M)
histColorSubtractedImage=HistColorImg(subtractedImage)
subtractedImageEq=EqualizeHistColor(subtractedImage)
histSubtractedImageEq=HistColorImg(subtractedImageEq)
ShowImgWithMatplotlib(image, "image", 1)
ShowHistWithMatplotlibRgb(histColor, "color histogram", 2, ['b', 'g', 'r'])
ShowImgWithMatplotlib(addedImage, "image lighter", 5)
ShowHistWithMatplotlibRgb(histColorAddedImage, "color histogram", 6, ['b', 'g', 'r'])
ShowImgWithMatplotlib(subtractedImage, "image darker", 9)
ShowHistWithMatplotlibRgb(histColorSubtractedImage, "color histogram", 10, ['b', 'g', 'r'])
ShowImgWithMatplotlib(imageEq, "image equalized", 3)
ShowHistWithMatplotlibRgb(histImageEq, "color histogram equalized", 4, ['b', 'g', 'r'])
ShowImgWithMatplotlib(addedImageEq, "image lighter equalized", 7)
ShowHistWithMatplotlibRgb(histAddedImageEq, "color histogram equalized", 8, ['b', 'g', 'r'])
ShowImgWithMatplotlib(subtractedImageEq, "image darker equalized", 11)
ShowHistWithMatplotlibRgb(histSubtractedImageEq, "color histogram equalized", 12, ['b', 'g', 'r'])
plt.show()