How to do it...

Follow these steps:

  1. Import all necessary modules:
import cv2import numpy as npimport matplotlib.pyplot as plt
  1. Load an image and display it:
grey = cv2.imread('../data/Lena.png', 0)cv2.imshow('original grey', grey)cv2.waitKey()cv2.destroyAllWindows()
  1. Compute a histogram function:
hist, bins = np.histogram(grey, 256, [0, 255])
  1. Plot histogram and display it:
plt.fill(hist)plt.xlabel('pixel value')plt.show()

Get OpenCV 3 Computer Vision with Python Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.