How to do it...

The steps for this recipe are as follows:

  1. Load the image as grayscale and convert every pixel value to the np.float32 data type in the [0, 1] range:
import cv2import numpy as npimage = cv2.imread('../data/Lena.png', 0).astype(np.float32) / 255
  1. Apply per-element exponentiation using the specified exponent value, gamma:
gamma = 0.5corrected_image = np.power(image, gamma)
  1. Display the source and result images:
cv2.imshow('image', image)cv2.imshow('corrected_image', corrected_image)cv2.waitKey()cv2.destroyAllWindows()

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.