Image Processing






Basic Operations



1. Read and display Image

In Computer Vision applications, images are an integral part of the development process. Often there would be a need to read images and display them if required. To read and display image using OpenCV Python, you could use cv2.imread() for reading image to a variable and cv2.imshow() to display the image in a separate window.

The Syntax  to read image is

cv2.imread(/complete/path/to/image,flag)

First argument is complete path to the image along with the extension.

Second argument is an optional flag which could be any one of the following :

  1. cv2.IMREAD_COLOR : Loads a color image. Any transparency of image will be neglected. It is the default flag.
  2. cv2.IMREAD_GRAYSCALE : Loads image in grayscale mode
  3. cv2.IMREAD_UNCHANGED : Loads image as such including alpha channel

Returns numpy array, containing the pixel values. For colored images, each pixel is represented as an array containing Red, Green and Blue channels.

Note that the default flag is cv2.IMREAD_COLOR. Hence even if read a png image with transparency, the transparency channel is neglected.

 

The syntax to display image is

cv2.imshow(window_name, image)

First argument is name of the window that is displayed when image is shown in.

Second argument is the image to be shown in the window.

Example:

Following is example of reading ad displaying image using cv2

import cv2
 
img = cv2.imread('abc.jpeg')
 
cv2.imshow('sample image',img)
 
cv2.waitKey(0) # waits until a key is pressed
cv2.destroyAllWindows() # destroys the window showing image

 

When above program is run, an image is shown in another window named ‘sample image’, as specified in the arguments of imshow().

 

2. Save an Image 

While working with images in Image Processing applications, it is quite often that you need to store intermediate results of image transformations or save the final resulting image. When working with OpenCV Python, images are stored in numpy ndarray. To save an image to the local file system, use cv2.imwrite() function of opencv python library.

The syntax to write an image is

cv2.imwrite('/path/to/destination/image.png',image)

where

  • First Argument is Path to the destination on file system, where image is ought to be saved.
  • Second Argument is ndarray containing image
  • Returns True is returned if the image is written to file system, else False.

Example:

import cv2
 
# read image as grey scale
img = cv2.imread('abc.jpg')
 
# save image
cv2.imwrite('xyz.jpg', img)

 cv2.waitKey(0) # waits until a key is pressed
cv2.destroyAllWindows() # destroys the window showing image

 

To check its output, go in current folder and check image file "xyz.jpg". if it is present there, it means porgram runs perfectly.

 

3. Image Size

When working with OpenCV Python, images are stored in numpy ndarray. To get the image shape or size, use ndarray.shape to get the dimensions of the image. Then, you can use index on the dimensions variable to get width, height and number of channels for each pixel.

import cv2
 
# read image
img = cv2.imread('abc.jpg')
 
# get dimensions of image
dimensions = img.shape
 
# height, width, number of channels in image
height = img.shape[0]
width = img.shape[1]
channels = img.shape[2]
 
print('Image Dimension    : ',dimensions)
print('Image Height       : ',height)
print('Image Width        : ',width)
print('Number of Channels : ',channels)

 

Output is 

Image Dimension : (1492004)
Image Height : 149
Image Width : 200
Number of Channels : 4
 
img.shape returns (Height, Width, Number of Channels)
 
Where
  1. Height represents the number of pixel rows in the image or the number of pixels in each column of the image array.
  2. Width represents the number of pixel columns in the image or the number of pixels in each row of the image array.
  3. Number of Channels represents the number of components used to represent each pixel.

In the above example, Number of Channels = 4 represent Alpha, Red, Green and Blue