diff options
author | bsdnoobz <nash@bsd-noobz.com> | 2012-12-14 18:28:41 +0700 |
---|---|---|
committer | bsdnoobz <nash@bsd-noobz.com> | 2012-12-14 18:28:41 +0700 |
commit | 5a83174b7cc307e1d1f1e4ebc1a3b1be9734570a (patch) | |
tree | 7e4d22c586ac257f23856f23efec28752582681f | |
parent | 9fab7231cc5a2ed31f2e78f16263d26b094e6ea4 (diff) |
Added display-histogram.py
-rw-r--r-- | display-histogram.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/display-histogram.py b/display-histogram.py new file mode 100644 index 0000000..0f0fedd --- /dev/null +++ b/display-histogram.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python + +""" +Sample code for displaying image histogram with Matplotlib +""" + +import cv2 +import matplotlib.pyplot as plt + +def show_histogram(im): + """ Function to display image histogram. + Supports single and three channel images. """ + if im.ndim == 2: + # Input image is single channel + plt.hist(im.flatten(), 256, range=(0,250), fc='k') + plt.show() + elif im.ndim == 3: + # Input image is three channels + fig = plt.figure() + fig.add_subplot(311) + plt.hist(im[...,0].flatten(), 256, range=(0,250), fc='b') + fig.add_subplot(312) + plt.hist(im[...,1].flatten(), 256, range=(0,250), fc='g') + fig.add_subplot(313) + plt.hist(im[...,2].flatten(), 256, range=(0,250), fc='r') + plt.show() + +if __name__ == '__main__': + im = cv2.imread("lena.jpg"); + if not (im == None): + show_histogram(im) + + |