Skip to content

Commit 9c0a8fe

Browse files
committed
Histogram Computation and Display
1 parent 8e01834 commit 9c0a8fe

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
''' example_7_1.py
2+
3+
Referred to example 7.1 in the book "Learning OpenCV: Computer Vision with the OpenCV Library"
4+
5+
Example 7-1. Histogram computation and display
6+
7+
usage: python example_7_1.py <image>
8+
9+
Converted by Abid.K --mail me at [email protected]
10+
'''
11+
################################################################################################
12+
13+
import cv,sys
14+
15+
if len(sys.argv)==2 and (cv.LoadImage(sys.argv[1])!=0):
16+
src=cv.LoadImage(sys.argv[1])
17+
hsv=cv.CreateImage(cv.GetSize(src),8,3)
18+
19+
cv.CvtColor(src,hsv,cv.CV_BGR2HSV)
20+
h_plane=cv.CreateImage(cv.GetSize(src),8,1)
21+
s_plane=cv.CreateImage(cv.GetSize(src),8,1)
22+
v_plane=cv.CreateImage(cv.GetSize(src),8,1)
23+
24+
planes=[h_plane,s_plane]
25+
26+
cv.CvtPixToPlane(hsv,h_plane,s_plane,v_plane,None)
27+
28+
h_bins=30
29+
s_bins=32
30+
31+
# Build the histogram and compute its contents.
32+
33+
hist=cv.CreateHist([h_bins,s_bins],cv.CV_HIST_ARRAY,[(0,180),(0,255)],1)
34+
cv.CalcHist(planes,hist,0,None)
35+
cv.NormalizeHist(hist,1.0)
36+
37+
# Create an image to use to visualize our histogram.
38+
39+
scale=10
40+
hist_img=cv.CreateImage((h_bins*scale,s_bins*scale),8,3)
41+
cv.Zero(hist_img)
42+
43+
max_value=0
44+
(minvalue,maxvalue,minidx,maxidx)=cv.GetMinMaxHistValue(hist)
45+
46+
for h in range(h_bins):
47+
for s in range(s_bins):
48+
bin_val=cv.QueryHistValue_2D(hist,h,s)
49+
intensity=cv.Round(bin_val*255/maxvalue)
50+
cv.Rectangle(hist_img,(h*scale,s*scale),((h+1)*scale-1,(s+1)*scale-1),(intensity,intensity,intensity),cv.CV_FILLED)
51+
print intensity
52+
53+
cv.ShowImage("src",src)
54+
cv.ShowImage("hsv",hist_img)
55+
cv.WaitKey(0)
56+
57+
else:
58+
print "usage : python example_7_1.py <image>"
59+

0 commit comments

Comments
 (0)