How to do it...

Use the following steps:

  1. Import all necessary modules:
import cv2import numpy as npimport matplotlib.pyplot as plt
  1. Load the image as grayscale and display it:
grey = cv2.imread('../data/Lena.png', 0)cv2.imshow('original grey', grey)cv2.waitKey()cv2.destroyAllWindows()
  1. Equalize the histogram of the grayscale image:
grey_eq = cv2.equalizeHist(grey)
  1. Compute the histogram for the equalized image and show it:
hist, bins = np.histogram(grey_eq, 256, [0, 255])plt.fill_between(range(256), hist, 0)plt.xlabel('pixel value')plt.show()
  1. Show the equalized image:
cv2.imshow('equalized grey', grey_eq)cv2.waitKey()cv2.destroyAllWindows()
  1. Load the image as BGR and convert it to the HSV color space:
color = cv2.imread('../data/Lena.png') ...

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.