How to do it...

The following steps are required for this recipe:

  1. Import all necessary modules, open an image, print its shape and data type, and display it on the screen:
import cv2, numpy as npimage = cv2.imread('../data/Lena.png')print('Shape:', image.shape)print('Data type:', image.dtype)cv2.imshow('image', image)cv2.waitKey()cv2.destroyAllWindows()
  1. Convert our image to one with floating data type elements:
image = image.astype(np.float32) / 255print('Shape:', image.shape)print('Data type:', image.dtype)
  1. Scale the elements of our image by 2 and clip the values to keep them in the [0, 1] range:
cv2.imshow('image', np.clip(image*2, 0, 1))cv2.waitKey()cv2.destroyAllWindows()
  1. Scale the elements of our image back to the [0, 255] range, ...

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.