Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/samples/python/hist.py
16337 views
1
#!/usr/bin/env python
2
3
''' This is a sample for histogram plotting for RGB images and grayscale images for better understanding of colour distribution
4
5
Benefit : Learn how to draw histogram of images
6
Get familier with cv.calcHist, cv.equalizeHist,cv.normalize and some drawing functions
7
8
Level : Beginner or Intermediate
9
10
Functions : 1) hist_curve : returns histogram of an image drawn as curves
11
2) hist_lines : return histogram of an image drawn as bins ( only for grayscale images )
12
13
Usage : python hist.py <image_file>
14
15
Abid Rahman 3/14/12 debug Gary Bradski
16
'''
17
18
# Python 2/3 compatibility
19
from __future__ import print_function
20
21
import cv2 as cv
22
import numpy as np
23
24
bins = np.arange(256).reshape(256,1)
25
26
def hist_curve(im):
27
h = np.zeros((300,256,3))
28
if len(im.shape) == 2:
29
color = [(255,255,255)]
30
elif im.shape[2] == 3:
31
color = [ (255,0,0),(0,255,0),(0,0,255) ]
32
for ch, col in enumerate(color):
33
hist_item = cv.calcHist([im],[ch],None,[256],[0,256])
34
cv.normalize(hist_item,hist_item,0,255,cv.NORM_MINMAX)
35
hist=np.int32(np.around(hist_item))
36
pts = np.int32(np.column_stack((bins,hist)))
37
cv.polylines(h,[pts],False,col)
38
y=np.flipud(h)
39
return y
40
41
def hist_lines(im):
42
h = np.zeros((300,256,3))
43
if len(im.shape)!=2:
44
print("hist_lines applicable only for grayscale images")
45
#print("so converting image to grayscale for representation"
46
im = cv.cvtColor(im,cv.COLOR_BGR2GRAY)
47
hist_item = cv.calcHist([im],[0],None,[256],[0,256])
48
cv.normalize(hist_item,hist_item,0,255,cv.NORM_MINMAX)
49
hist=np.int32(np.around(hist_item))
50
for x,y in enumerate(hist):
51
cv.line(h,(x,0),(x,y),(255,255,255))
52
y = np.flipud(h)
53
return y
54
55
56
if __name__ == '__main__':
57
58
import sys
59
60
if len(sys.argv)>1:
61
fname = sys.argv[1]
62
else :
63
fname = '../data/lena.jpg'
64
print("usage : python hist.py <image_file>")
65
66
im = cv.imread(fname)
67
68
if im is None:
69
print('Failed to load image file:', fname)
70
sys.exit(1)
71
72
gray = cv.cvtColor(im,cv.COLOR_BGR2GRAY)
73
74
75
print(''' Histogram plotting \n
76
Keymap :\n
77
a - show histogram for color image in curve mode \n
78
b - show histogram in bin mode \n
79
c - show equalized histogram (always in bin mode) \n
80
d - show histogram for color image in curve mode \n
81
e - show histogram for a normalized image in curve mode \n
82
Esc - exit \n
83
''')
84
85
cv.imshow('image',im)
86
while True:
87
k = cv.waitKey(0)
88
if k == ord('a'):
89
curve = hist_curve(im)
90
cv.imshow('histogram',curve)
91
cv.imshow('image',im)
92
print('a')
93
elif k == ord('b'):
94
print('b')
95
lines = hist_lines(im)
96
cv.imshow('histogram',lines)
97
cv.imshow('image',gray)
98
elif k == ord('c'):
99
print('c')
100
equ = cv.equalizeHist(gray)
101
lines = hist_lines(equ)
102
cv.imshow('histogram',lines)
103
cv.imshow('image',equ)
104
elif k == ord('d'):
105
print('d')
106
curve = hist_curve(gray)
107
cv.imshow('histogram',curve)
108
cv.imshow('image',gray)
109
elif k == ord('e'):
110
print('e')
111
norm = cv.normalize(gray, gray, alpha = 0,beta = 255,norm_type = cv.NORM_MINMAX)
112
lines = hist_lines(norm)
113
cv.imshow('histogram',lines)
114
cv.imshow('image',norm)
115
elif k == 27:
116
print('ESC')
117
cv.destroyAllWindows()
118
break
119
cv.destroyAllWindows()
120
121