OpenCV Tutorial – Reading, Displaying and Writing Image using imread() , imshow() and imwrite()

OpenCV Tutorial – Reading Displaying and Writing Image using imread() , imshow() and imwrite()

Introduction

A very basic Computer Vision operation is to read the visual data, display on the screen and also write or save visual data. The visual data is mostly like images and videos. In this tutorial we are going to see some OpenCV function which helps to read the visual input using cv2.imread(), display visual data on screen using cv2.imshow() and write the output file using cv2.imwrite().

Importing OpenCV Library

To begin this article, let us first import OpenCV library

In [1]:
#Lets import the OpenCV and Numpy library 
import cv2
import numpy

Reading and Loading of an Image: cv2.imread()

To read an image ,opencv provide a cv2.imread() function . This function support various file format like BMP, PNG, JPEG, TIFF etc.

Syntax

Let us see the syntax

cv2.imread(path, flag)

Parameters:

path: The image should be in the working directory or a full path of image should be given.

flag: The flags option is used to control how the image is read.

cv2.IMREAD_COLOR: Loads a color image. Any transparency of the image will be neglected. It is the default flag.

cv2.IMREAD_GRAYSCALE : Loads image in grayscale mode

cv2.IMREAD_UNCHANGED: This reads all of the image data, including the alpha or transparency channel (if there is one) as a fourth channel.

Note: Instead of these three flags, you can simply pass integers 1, 0 or -1 respectively.

If you are confused by seeing syntax of the function then don’t worry. We will take several examples to understand it completely.

[adrotate banner=”3″]

Display an image: cv2.imshow()

Use the function cv2.imshow() to display an image in a window.

Syntax

cv2.imshow(window_name, image)

window_name: A string representing the name of the window in which image to be displayed.

image: It is the image that is to be displayed.

Code to display an image

window_name=’image’

cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)

cv2.imshow(window_name,img)

cv2.waitKey(0)

cv2.destroyAllWindows()

cv2.waitKey() is a keyboard binding function. Its argument is the time in milliseconds. The function waits for specified milliseconds for any keyboard event. If you press any key in that time, the program continues. If 0 is passed, it waits indefinitely for a keystroke.

cv2.destroyAllWindows() simply destroy all the windows we created. If you want to destroy any specific window, use the function cv2.destroyWindow() where you pass the exact window name as the argument.

Using cv2.namedWindow(window_name, cv2.WINDOW_NORMAL) , we first create a window and load image to it later.

In cv2.namedWindow(),by default, the flag is cv2.WINDOW_AUTOSIZE. But if you specify flag to be cv2.WINDOW_NORMAL , you can resize the window. It will be helpful when an image is too large in dimension and adding track bar to windows.

Example1: Reading an image using cv2.imread() with flag cv2.IMREAD_COLOR

We will be using this sample image for our next two examples.

cv2.imread() Sample Image
cv2.imread() Sample Image
In [2]:
#Here I am providing only image name because this image is in my current 
working directory. 
#If this is not the case please provide the full path

img_first=cv2.imread('image.jpg',cv2.IMREAD_COLOR)

#you can also use below syntax for same
img=cv2.imread('image.jpg')
#or
img=cv2.imread('image.jpg',1)

Before going further let us explore an image

In [4]:
#Finding shape of an image
img_first.shape
Out[4]:
(1227, 1534, 3)
In [5]:
#Finding the type of image
type(img_first)
Out[5]:
numpy.ndarray
In [6]:
print(img_first)
Output
[[[255 255 255]
  [255 255 255]
  [255 255 255]
  ...
  [255 255 255]
  [255 255 255]
  [255 255 255]]

 [[255 255 255]
  [255 255 255]
  [255 255 255]
  ...
  [255 255 255]
  [255 255 255]
  [255 255 255]]

 [[255 255 255]
  [255 255 255]
  [255 255 255]
  ...
  [255 255 255]
  [255 255 255]
  [255 255 255]]

 ...

 [[255 255 255]
  [255 255 255]
  [255 255 255]
  ...
  [255 255 255]
  [255 255 255]
  [255 255 255]]

 [[255 255 255]
  [255 255 255]
  [255 255 255]
  ...
  [255 255 255]
  [255 255 255]
  [255 255 255]]

 [[255 255 255]
  [255 255 255]
  [255 255 255]
  ...
  [255 255 255]
  [255 255 255]
  [255 255 255]]]
In [7]:
print(img_first.max())
print(img_first.min())
Output
255
0

From the above three steps, we are able to know some important points regarding this image:

  • An image is a multidimensional array(m n c), where m is the no. of rows, n is no. of columns and c is no. of channels.
  • It has columns and rows of pixels, and each pixel has a value.
  • The pixel values range from 0 to 255. That means 0 represents black and 255 represents white.
  • The range is 0-255 means that each pixel is represented by a single 8-bit integer.
  • Since the image is a colored image there are three channels. Opencv reads the image in Blue Green Red(BGR) format.
  • So in the colored image, each pixel is represented by a three-element array, with each integer representing one of the three color channels: B, G, and R, respectively.

Display image using cv2.imshow()

In [13]:
window_name='imagefirst'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,img_first)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output Screenshot

cv2.imshow() Output
cv2.imshow() Output

Example2: Reading an image using cv2.imread() with flag cv2.IMREAD_GRAYSCALE

In [8]:
#Lets see second flag cv2.IMREAD_GRAYSCALE
img_second=cv2.imread('image.jpg',cv2.IMREAD_GRAYSCALE)

#or you can do like this

img_second=cv2.imread('image.jpg',0)

Let us explore this image

In [9]:
img_second.shape
Out[9]:
(1227, 1534)
In [10]:
img_second.size
Out[10]:
1882218
In [11]:
type(img_second)
Out[11]:
numpy.ndarray

As you see here there are only rows and columns but there is no third channel. This is because in grayscale only one channel is there.

So we come to know some important points –

  • An OpenCV image is a 2D or 3D array of the numpy.array type.
  • An 8-bit grayscale image is a 2D array containing byte values. In an 8-bit grayscale image with a white pixel in the upper-left corner i.e image[0][0] is 255.
  • A 24-bit BGR image is a 3D array, which also contains byte values.For a 24-bit (8-bit-per-channel) BGR image with a blue pixel in the upper-left corner i.e image[0][0][0] is [255, 0, 0].

Display this gray scale image

In [14]:
window_name='image_second'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,img_second)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output Screenshot

cv2.imshow() Gray Scale Image
cv2.imshow() Gray Scale Image

Example 3: Reading an image using cv2.imread() with flag cv2.IMREAD_UNCHANGED

Most digital SLR cameras are capable of recording images at a higher bit depth than 8-bits / channel. The raw images from these cameras can be converted to 16-bit / channel PNG or TIFF images.
These 16-bit / channel images can be read using cv2.IMREAD_UNCHANGED flag.

A transparent image has four channels — 3 for color, and one for transparency. These images can be read in OpenCV using the cv2.IMREAD_UNCHANGED flag.

We are going to use the below image for this example.

cv2.imread() Sample Alpha Image
cv2.imread() Sample Alpha Image
In [12]:
img_third=cv2.imread('alph.png',cv2.IMREAD_UNCHANGED)

Let us explore this image and understand what transparency means

In [13]:
img_third.shape
Out[13]:
(371, 602, 4)

As you see here, there are four channels in the image

A pixel in a typical image has 3 values: blue, green and red. Usually, they range from 0 to 255. So a pixel that has the values 255, 0, 0 is pure blue — maximum value for the blue component, minimum values for both green and red.

With an image that has an alpha channel, each pixel has four components: red, blue, green, red and alpha. Alpha of 255 is opaque, while alpha of 0 is transparent.

Alpha channels are masks through which you can display images. The alpha channel is an 8-bit channel, which means it has 256 levels of gray from 0 (black) to 255 (white). White acts as the visible area; black acts as the transparent area (you see the background behind the image when displayed). The level of gray in between determines the level of visibility.

PNG, BMP and PSD file format actually support alpha channel.

Alpha channel for Transparent Image
source: https://www.axialis.com

 

Display this image

In [14]:
window_name='image_third'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,img_third)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output Screenshot

cv2.imshow() Alpha Channel Image Output
cv2.imshow() Alpha Channel Image Output

 

Saving or Writing image: cv2.imwrite()

cv2.imwrite() method is used to save an image to any storage device. This will save the image according to the specified format in the current working directory.

Syntax

cv2.imwrite(filename, image)

filename: A string representing the file name. The filename must include image format like .jpg, .png, etc.

image: It is the image that is to be saved.

Example1: writing an image using cv2.imwrite() in current working directories

In [15]:
cv2.imwrite('imagefirst.jpg',img_first)
Out[15]:
True

In this example, the image gets saved in current working directories.
If you want to save the image in specific directories then provide full path in filename parameter with image name like in below example

Example2: Writing an image using cv2.imwrite() in specific directories

In [16]:
cv2.imwrite('/home/sachin/Desktop/imagesecond.jpg',img_second)
Out[16]:
True

Conclusion

Reaching the end of this tutorial, we learned how we can read the image using cv2.imread() , display image using cv2.imshow() and to save image using cv2.imwrite() that are in-built functions of OpenCV library. With the help of syntax and examples, we got a deeper understanding of these functions.

 

 

Follow Us

Leave a Reply

Your email address will not be published. Required fields are marked *