Representing images as tensors/blobs
Deep Learning models for computer vision usually get images as an input. However, they do not operate with images, but with tensors. A tensor is more general than an image; it's not limited by two-spatial and one-channel dimensions. In this recipe, we will learn how to convert an image to a multidimensional tensor.
Getting ready
Before you proceed with this recipe, you need to install the OpenCV 3.3 (or greater) Python API package.
How to do it...
You need to complete the following steps:
- Import the modules:
import cv2 import numpy as np
- Open an input image and print its shape:
image_bgr = cv2.imread('../data/Lena.png', cv2.IMREAD_COLOR) print(image_bgr.shape)
- Transform the image to a four-dimensional floating point tensor:
image_bgr_float = image_bgr.astype(np.float32) image_rgb = image_bgr_float[..., ::-1] tensor_chw = np.transpose(image_rgb, (2, 0, 1)) tensor_nchw = tensor_chw[np.newaxis, ...] print(tensor_nchw.shape)
How it works...
As you know, matrices and...