How to do it…

  1. Import the necessary packages:
import sys 
import cv2 
import numpy as np 
  1. Load the input image:
in_file = sys.argv[1] 
image = cv2.imread(in_file) 
  1. Convert the RGB image into grayscale:
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 
cv2.imshow('Input grayscale image', image_gray) 
  1. Regulate the histogram of the grayscale image:
image_gray_histoeq = cv2.equalizeHist(image_gray) 
cv2.imshow('Histogram equalized - grayscale image', image_gray_histoeq) 
  1. Regulate the histogram of the RGB image:
image_yuv = cv2.cvtColor(image, cv2.COLOR_BGR2YUV) 
image_yuv[:,:,0] = cv2.equalizeHist(image_yuv[:,:,0]) 
image_histoeq = cv2.cvtColor(image_yuv, cv2.COLOR_YUV2BGR) 
  1. Display the output image:
cv2.imshow('Input image', image) cv2.imshow('Histogram ...

Get Raspberry Pi 3 Cookbook for Python Programmers - Third Edition 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.