Basic image processing tutorial

Basic image processing for deep learning. Refer github for the source code.
The sample image is obtained from PEXELS.

If you are not familiar with image processing, you can read this article before going to convolutional neural network.

OpenCV is image processing library which supports

  • loading image in numpy.ndarray format, save image
  • converting image color format (RGB, YUV, Gray scale etc)
  • resize

and other useful image processing functionality.

To install opencv, execute

$conda install -c -y opencv3


Loading and save image

  • cv2.imread for loading image.
  • cv2.imwrite for save image.
  • plt.imshow for plotting, and plt.savefig for save plot image.

OpenCV image format is usually 3 dimension (or 2 dimension if the image is gray scale).

1st dimension is for height, 2nd dimension is for width, 3rd dimension is for channel (RGB, YUV etc).

To convert color format cv2.cvtColor can be used. Details are written in next section.


image.shape (Height, Width, Channel) = (380, 512, 3)




Change color format

  • cv2.cvtColor for converting color format.

Note that openCV version 3 reads the image color in the order B, G, R. However, matplotlib deals with the image color in the corder R, G, B. So you need to convert color order, refer readRGBImage function.

If the image is gray scale, the image is 2 dimensional array

1st dimension is for height, 2nd dimension is for width.

gray_image.shape (Height, Width) = (380, 512)





  • cv2.imread for resize.

Note that size should be specified in the order width, height.


image.shape (Height, Width, Channel) = (380, 512, 3)
half_image.shape (Height, Width, Channel) = (190, 256, 3)
resized128_image.shape (Height, Width, Channel) = (95, 128, 3)





  • numpy slicing can be used for cropping image

cropped_image.shape (Height, Width, Channel) = (190, 190, 3)


Image processing with channels

RGB channel manipulation.

Understanding the meaning of “channel” is important in deep learning. Below code provides some insight that what each channel represents.




RGB_color.jpg Each R,G,B channel is shown in R, G, B color respectively.


Next: Understanding convolutional layer

Sponsored Links

Leave a Reply

Your email address will not be published.