-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHistogramCustomVisualization.py
More file actions
67 lines (59 loc) · 2.32 KB
/
HistogramCustomVisualization.py
File metadata and controls
67 lines (59 loc) · 2.32 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
import numpy as np
import cv2
from matplotlib import pyplot as plt
def ShowImgWithMatplotlib(colorImg, title, pos):
imgRGB=colorImg[:, :, ::-1]
ax=plt.subplot(2, 3, pos)
plt.imshow(imgRGB)
plt.title(title)
plt.axis('off')
def ShowHistWithMatplotlibRGB(hist, title, pos, color):
ax=plt.subplot(2, 3, pos)
plt.title(title)
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 ShowHistWithMatplotlibGray(hist, title, pos, color):
ax=plt.subplot(2, 3, pos)
plt.title(title)
plt.xlabel("bins")
plt.ylabel("number of pixels")
plt.xlim([0, 256])
plt.plot(hist, color=color)
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 PlotHist(histItems, color):
offsetDown=10
offsetUp=10
xValues=np.arange(256).reshape(256, 1)
canvas=np.ones((300, 256, 3), dtype="uint8")*255
for histItem, col in zip(histItems, color):
cv2.normalize(histItem, histItem, 0+offsetDown, 300-offsetUp, cv2.NORM_MINMAX)
around=np.around(histItem)
hist=np.int32(around)
pts=np.column_stack((xValues, hist))
cv2.polylines(canvas, [pts], False, col, 2)
cv2.rectangle(canvas, (0, 0), (255, 298), (0, 0, 0), 1)
res=np.flipud(canvas)
return res
plt.figure(figsize=(16, 10))
plt.suptitle("Custom visualization of histograms", fontsize=14, fontweight='bold')
image=cv2.imread('images/lenna-mod.png')
grayImage=cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
hist=cv2.calcHist([grayImage], [0], None, [256], [0, 256])
histColor=HistColorImg(image)
grayPlot=PlotHist([hist], [(255, 0, 255)])
colorPlot=PlotHist(histColor, [(255, 0, 0), (0, 255, 0), (0, 0, 255)])
ShowImgWithMatplotlib(cv2.cvtColor(grayImage, cv2.COLOR_GRAY2BGR), "gray", 1)
ShowImgWithMatplotlib(image, "image", 4)
ShowHistWithMatplotlibGray(hist, "grayscale histogram (matplotlib)", 2, 'm')
ShowHistWithMatplotlibRGB(histColor, "color histogram (matplotlib)", 3, ['b', 'g', 'r'])
ShowImgWithMatplotlib(grayPlot, "grayscale histogram (custom)", 5)
ShowImgWithMatplotlib(colorPlot, "color histogram (custom)", 6)
plt.show()