In [5]:
# load the input image and construct an input blob for the image
# by resizing to a fixed 300x300 pixels and then normalize it

'''
    Then we load the image , extract the dimensions, and create a blob.
    The dnn.blobFromImage  takes care of pre-processing which includes setting the
    blob  dimensions and normalization.
'''

image = cv2.imread("test_image3.jpg")
image = cv2.resize(image,(int(500),int(500)))
(h, w) = image.shape[:2]

cv2.dnn.blobFromImage

This function perform:

  • Mean subtraction
  • Scaling
  • And optionally channel swapping

Mean subtraction is used to help combat illumination changes in the input images in our dataset.

Before we even begin training our deep neural network, we first compute the average pixel intensity across all images in the training set for each of the Red, Green, and Blue channels.

This implies that we end up with three variables:

$\mu_R$, $\mu_G$, and $\mu_B$

Typically the resulting values are a 3-tuple consisting of the mean of the Red, Green, and Blue channels, respectively.

When we are ready to pass an image through our network (whether for training or testing), we subtract the mean, \mu, from each input channel of the input image:

R = R - $\mu_R$

G = G - $\mu_G$

B = B - $\mu_B$

We may also have a scaling factor, $\sigma$. The value of $\sigma$ may be the standard deviation across the training set which adds in a normalization:

R = (R - $\mu_R$) / $\sigma$

G = (G - $\mu_G$) / $\sigma$

B = (B - $\mu_B$) / $\sigma$

Function signature:

blob = cv2.dnn.blobFromImage(image, scalefactor=1.0, size, mean, swapRB=True)

Where:

  • scalefactor - we can optionally scale our images by some factor. This value defaults to 1.0 (no scaling)
  • size - spatial size that the Convolutional Neural Network expects
  • mean - our mean subtraction values
  • swapRB - OpenCV assumes images are in BGR channel order; however, the mean value assumes we are using RGB order. To resolve this discrepancy we can swap the R and B channels in image by setting this value to True.
In [6]:
blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0))