How it works...

The OpenCV dnn module contains a convenient function to convert an image to a tensor with preprocessing, blobFromImage. The arguments of this function are the input image (with one or three channels), scale values factor, output spatial size in (width, height) format, mean value to subtract, Boolean flag for whether to swap red and blue channels, and Boolean flag for whether to crop an image from the center before resizing to save the aspect ratio of the object in the image or just resize without preserving the object's proportions. The blobFromImage function goes through the following steps while converting an image to a tensor:

  1. The function resizes the image. If the crop flag is True, the input image is resized while preserving ...

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.