# 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

• converting image color format (RGB, YUV, Gray scale etc)
• resize

and other useful image processing functionality.

To install opencv, execute

\$conda install -c https://conda.binstar.org/menpo -y opencv3

• 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)

out_plt.jpg

## 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)

out_gray.jpg

## Resize

• 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)

out_resized128.jpg

## Crop

• 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_gray.jpg

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