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