Examples of using histograms in python
authorGary Bradski <no@email>
Wed, 14 Mar 2012 23:33:16 +0000 (23:33 +0000)
committerGary Bradski <no@email>
Wed, 14 Mar 2012 23:33:16 +0000 (23:33 +0000)
samples/python2/hist.py [new file with mode: 0755]

diff --git a/samples/python2/hist.py b/samples/python2/hist.py
new file mode 100755 (executable)
index 0000000..4b03467
--- /dev/null
@@ -0,0 +1,109 @@
+''' This is a sample for histogram plotting for RGB images and grayscale images for better understanding of colour distribution\r
+\r
+Benefit : Learn how to draw histogram of images\r
+          Get familier with cv2.calcHist, cv2.equalizeHist,cv2.normalize and some drawing functions\r
+\r
+Level : Beginner or Intermediate\r
+\r
+Functions : 1) hist_curve : returns histogram of an image drawn as curves\r
+            2) hist_lines : return histogram of an image drawn as bins ( only for grayscale images )\r
+\r
+Usage : python hist.py <image_file> \r
+\r
+Abid Rahman 3/14/12 debug Gary Bradski\r
+'''\r
+\r
+import cv2,sys\r
+import numpy as np\r
+\r
+bins = np.arange(256).reshape(256,1)\r
+\r
+def hist_curve(im):\r
+    h = np.zeros((300,256,3))\r
+    if len(im.shape) == 2:\r
+        color = [(255,255,255)]\r
+    elif im.shape[2] == 3:\r
+        color = [ (255,0,0),(0,255,0),(0,0,255) ]\r
+    for ch, col in enumerate(color):\r
+        hist_item = cv2.calcHist([im],[ch],None,[256],[0,255])\r
+        cv2.normalize(hist_item,hist_item,0,255,cv2.NORM_MINMAX)\r
+        hist=np.int32(np.around(hist_item))\r
+        pts = np.int32(np.column_stack((bins,hist)))\r
+        cv2.polylines(h,[pts],False,col)\r
+    y=np.flipud(h)\r
+    return y\r
+\r
+def hist_lines(im):\r
+    h = np.zeros((300,256,3))\r
+    if len(im.shape)!=2:\r
+        print "hist_lines applicable only for grayscale images"\r
+        #print "so converting image to grayscale for representation"\r
+        im = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)\r
+    hist_item = cv2.calcHist([im],[0],None,[256],[0,255])\r
+    cv2.normalize(hist_item,hist_item,0,255,cv2.NORM_MINMAX)\r
+    hist=np.int32(np.around(hist_item))\r
+    for x,y in enumerate(hist):\r
+        cv2.line(h,(x,0),(x,y),(255,255,255))\r
+    y = np.flipud(h)\r
+    return y\r
+\r
+\r
+if __name__ == '__main__':\r
+    \r
+    import urllib2\r
+\r
+    if len(sys.argv)>1:\r
+        im = cv2.imread(sys.argv[1])\r
+    else :\r
+        im = cv2.imread('../cpp/lena.jpg')\r
+        print "usage : python hist.py <image_file>"\r
+        \r
+\r
+    gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)\r
+\r
+   \r
+    print ''' Histogram plotting \n\r
+    Keymap :\n\r
+    a - show histogram for color image in curve mode \n\r
+    b - show histogram in bin mode \n\r
+    c - show equalized histogram (always in bin mode) \n\r
+    d - show histogram for color image in curve mode \n\r
+    e - show histogram for a normalized image in curve mode \n\r
+    Esc - exit \n\r
+    '''\r
+\r
+    cv2.imshow('image',im)\r
+    while True:\r
+        k = cv2.waitKey(0)&0xFF\r
+        if k == ord('a'):\r
+            curve = hist_curve(im)\r
+            cv2.imshow('histogram',curve)\r
+            cv2.imshow('image',im)\r
+            print 'a'\r
+        elif k == ord('b'):\r
+            print 'b'\r
+            lines = hist_lines(im)\r
+            cv2.imshow('histogram',lines)\r
+            cv2.imshow('image',gray)\r
+        elif k == ord('c'):\r
+            print 'c'\r
+            equ = cv2.equalizeHist(gray)\r
+            lines = hist_lines(equ)\r
+            cv2.imshow('histogram',lines)\r
+            cv2.imshow('image',equ)\r
+        elif k == ord('d'):\r
+            print 'd'\r
+            curve = hist_curve(gray)\r
+            cv2.imshow('histogram',curve)\r
+            cv2.imshow('image',gray)\r
+        elif k == ord('e'):\r
+            print 'e'\r
+            norm = cv2.normalize(gray,alpha = 0,beta = 255,norm_type = cv2.NORM_MINMAX)\r
+            lines = hist_lines(norm)\r
+            cv2.imshow('histogram',lines)\r
+            cv2.imshow('image',norm)\r
+        elif k == 27:\r
+            print 'ESC'\r
+            cv2.destroyAllWindows()\r
+            break\r
+\r