Skip to content

Instantly share code, notes, and snippets.

@willasaywhat
Created June 3, 2010 20:08
Show Gist options
  • Save willasaywhat/424395 to your computer and use it in GitHub Desktop.
Save willasaywhat/424395 to your computer and use it in GitHub Desktop.
import cv
import sys
img = cv.LoadImage(sys.argv[1], 0)
hist = cv.CreateHist([32], cv.CV_HIST_ARRAY, [[0, 255]], 1)
cv.CalcHist([img], hist, 0, None)
hist_img = cv.CreateImage((32*10,255), 8, 3)
(_,max_value,_,_) = cv.GetMinMaxHistValue(hist)
(sizex, sizey) = cv.GetSize(img)
darkcount = 0
for i in range(8):
darkcount = darkcount + cv.QueryHistValue_1D(hist, i)
if darkcount > (.7 * (sizex * sizey)):
print "Image is dark."
else:
print "Image is light."
for h in range(32):
bin_val = cv.QueryHistValue_1D(hist, h)
intensity = cv.Round(bin_val * 255 / max_value)
cv.Rectangle(hist_img, (h*5,255), (h*5+5,255-(intensity*5)), cv.RGB(255,255,255), cv.CV_FILLED)
cv.ShowImage("Histogram", hist_img)
cv.WaitKey(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment