Module Expr Str Import alias Import alias identifier Assign NameStore Call AttributeLoad Call AttributeLoad NameLoad attr Num attr Num Num FunctionDef arguments args NameParam defaults body Assign NameStore Call AttributeLoad NameLoad attr TupleLoad Num Num Num If CompareEq Call NameLoad AttributeLoad NameLoad attr Num body Assign NameStore ListLoad TupleLoad Num Num Num orelse If CompareEq SubscriptLoad AttributeLoad NameLoad attr Index Num Num body Assign NameStore ListLoad TupleLoad Num Num Num TupleLoad Num Num Num TupleLoad Num Num Num For TupleStore NameStore NameStore Call NameLoad NameLoad body Assign NameStore Call AttributeLoad NameLoad attr ListLoad NameLoad ListLoad NameLoad NameLoad ListLoad Num ListLoad Num Num Expr Call AttributeLoad NameLoad attr NameLoad NameLoad Num Num AttributeLoad NameLoad attr Assign NameStore Call AttributeLoad NameLoad attr Call AttributeLoad NameLoad attr NameLoad Assign NameStore Call AttributeLoad NameLoad attr Call AttributeLoad NameLoad attr TupleLoad NameLoad NameLoad Expr Call AttributeLoad NameLoad attr NameLoad ListLoad NameLoad NameLoad NameLoad Assign NameStore Call AttributeLoad NameLoad attr NameLoad Return NameLoad decorator_list FunctionDef arguments args NameParam defaults body Assign NameStore Call AttributeLoad NameLoad attr TupleLoad Num Num Num If CompareNotEq Call NameLoad AttributeLoad NameLoad attr Num body Print Str Assign NameStore Call AttributeLoad NameLoad attr NameLoad AttributeLoad NameLoad attr Assign NameStore Call AttributeLoad NameLoad attr ListLoad NameLoad ListLoad Num NameLoad ListLoad Num ListLoad Num Num Expr Call AttributeLoad NameLoad attr NameLoad NameLoad Num Num AttributeLoad NameLoad attr Assign NameStore Call AttributeLoad NameLoad attr Call AttributeLoad NameLoad attr NameLoad For TupleStore NameStore NameStore Call NameLoad NameLoad body Expr Call AttributeLoad NameLoad attr NameLoad TupleLoad NameLoad Num TupleLoad NameLoad NameLoad TupleLoad Num Num Num Assign NameStore Call AttributeLoad NameLoad attr NameLoad Return NameLoad decorator_list If CompareEq NameLoad Str body Import alias If CompareGt Call NameLoad AttributeLoad NameLoad attr Num body Assign NameStore Call AttributeLoad NameLoad attr SubscriptLoad AttributeLoad NameLoad attr Index Num orelse Assign NameStore Call AttributeLoad NameLoad attr Str Print Str Assign NameStore Call AttributeLoad NameLoad attr NameLoad AttributeLoad NameLoad attr Print Str Expr Call AttributeLoad NameLoad attr Str NameLoad While NameLoad body Assign NameStore BinOpBitAnd Call AttributeLoad NameLoad attr Num Num If CompareEq NameLoad Call NameLoad Str body Assign NameStore Call NameLoad NameLoad Expr Call AttributeLoad NameLoad attr Str NameLoad Expr Call AttributeLoad NameLoad attr Str NameLoad Print Str orelse If CompareEq NameLoad Call NameLoad Str body Print Str Assign NameStore Call NameLoad NameLoad Expr Call AttributeLoad NameLoad attr Str NameLoad Expr Call AttributeLoad NameLoad attr Str NameLoad orelse If CompareEq NameLoad Call NameLoad Str body Print Str Assign NameStore Call AttributeLoad NameLoad attr NameLoad Assign NameStore Call NameLoad NameLoad Expr Call AttributeLoad NameLoad attr Str NameLoad Expr Call AttributeLoad NameLoad attr Str NameLoad orelse If CompareEq NameLoad Call NameLoad Str body Print Str Assign NameStore Call NameLoad NameLoad Expr Call AttributeLoad NameLoad attr Str NameLoad Expr Call AttributeLoad NameLoad attr Str NameLoad orelse If CompareEq NameLoad Call NameLoad Str body Print Str Assign NameStore Call AttributeLoad NameLoad attr NameLoad keyword Num keyword Num keyword AttributeLoad NameLoad attr Assign NameStore Call NameLoad NameLoad Expr Call AttributeLoad NameLoad attr Str NameLoad Expr Call AttributeLoad NameLoad attr Str NameLoad orelse If CompareEq NameLoad Num body Print Str Expr Call AttributeLoad NameLoad attr Break Expr Call AttributeLoad NameLoad attr
N/A N/A  This is a sample for histogram plotting for RGB images and grayscale images for better understanding of colour distribution

Benefit : Learn how to draw histogram of images
          Get familier with cv2.calcHist, cv2.equalizeHist,cv2.normalize and some drawing functions

Level : Beginner or Intermediate

Functions : 1) hist_curve : returns histogram of an image drawn as curves
            2) hist_lines : return histogram of an image drawn as bins ( only for grayscale images )

Usage : python hist.py <image_file>

Abid Rahman 3/14/12 debug Gary Bradski
 N/A cv2 N/A numpy np N/A bins N/A N/A N/A N/A np arange 256 reshape 256 1 hist_curve N/A N/A im N/A N/A N/A h N/A N/A np zeros N/A 300 256 3 N/A N/A N/A len N/A im shape 2 N/A N/A color N/A N/A 255 255 255 N/A N/A N/A N/A N/A im shape N/A 2 3 N/A N/A color N/A N/A 255 0 0 N/A 0 255 0 N/A 0 0 255 N/A N/A ch col N/A enumerate color N/A N/A hist_item N/A N/A cv2 calcHist N/A im N/A ch None N/A 256 N/A 0 256 N/A N/A N/A cv2 normalize hist_item hist_item 0 255 N/A cv2 NORM_MINMAX N/A hist N/A N/A np int32 N/A N/A np around hist_item N/A pts N/A N/A np int32 N/A N/A np column_stack N/A bins hist N/A N/A N/A cv2 polylines h N/A pts False col N/A y N/A N/A np flipud h N/A y N/A hist_lines N/A N/A im N/A N/A N/A h N/A N/A np zeros N/A 300 256 3 N/A N/A N/A len N/A im shape 2 N/A N/A hist_lines applicable only for grayscale images N/A im N/A N/A cv2 cvtColor im N/A cv2 COLOR_BGR2GRAY N/A hist_item N/A N/A cv2 calcHist N/A im N/A 0 None N/A 256 N/A 0 256 N/A N/A N/A cv2 normalize hist_item hist_item 0 255 N/A cv2 NORM_MINMAX N/A hist N/A N/A np int32 N/A N/A np around hist_item N/A N/A x y N/A enumerate hist N/A N/A N/A N/A cv2 line h N/A x 0 N/A x y N/A 255 255 255 N/A y N/A N/A np flipud h N/A y N/A N/A N/A __name__ __main__ N/A N/A sys N/A N/A N/A len N/A sys argv 1 N/A N/A im N/A N/A cv2 imread N/A N/A sys argv N/A 1 N/A N/A im N/A N/A cv2 imread ../cpp/lena.jpg N/A usage : python hist.py <image_file> N/A gray N/A N/A cv2 cvtColor im N/A cv2 COLOR_BGR2GRAY N/A  Histogram plotting 

    Keymap :

    a - show histogram for color image in curve mode 

    b - show histogram in bin mode 

    c - show equalized histogram (always in bin mode) 

    d - show histogram for color image in curve mode 

    e - show histogram for a normalized image in curve mode 

    Esc - exit 

     N/A N/A N/A cv2 imshow image im N/A True N/A N/A k N/A N/A N/A cv2 waitKey 0 255 N/A N/A k N/A ord a N/A N/A curve N/A hist_curve im N/A N/A N/A cv2 imshow histogram curve N/A N/A N/A cv2 imshow image im N/A a N/A N/A N/A k N/A ord b N/A N/A b N/A lines N/A hist_lines im N/A N/A N/A cv2 imshow histogram lines N/A N/A N/A cv2 imshow image gray N/A N/A N/A k N/A ord c N/A N/A c N/A equ N/A N/A cv2 equalizeHist gray N/A lines N/A hist_lines equ N/A N/A N/A cv2 imshow histogram lines N/A N/A N/A cv2 imshow image equ N/A N/A N/A k N/A ord d N/A N/A d N/A curve N/A hist_curve gray N/A N/A N/A cv2 imshow histogram curve N/A N/A N/A cv2 imshow image gray N/A N/A N/A k N/A ord e N/A N/A e N/A norm N/A N/A cv2 normalize gray alpha 0 beta 255 norm_type N/A cv2 NORM_MINMAX N/A lines N/A hist_lines norm N/A N/A N/A cv2 imshow histogram lines N/A N/A N/A cv2 imshow image norm N/A N/A N/A k 27 N/A N/A ESC N/A N/A N/A cv2 destroyAllWindows N/A N/A N/A N/A cv2 destroyAllWindows