OpenCV Image Basics

Introduction

From the previous tutorial, we can get to know, how the images are stored in the NumPy. Now, in this tutorial we shall get to know about the OpenCV image basics, how OpenCV stores the images and some operations on the image using the openCV image processing techniques.

About OpenCV

OpenCV is known as Open Computer Vision, a library developed by the Intel in 1999. It is basically developed using C++ but entirely used by the python programming functions for many real-time computer vision applications. It consists of a wide range of algorithms that includes object detection, tracking images, drawing geometric shapes and much more.

Operations on Images using OpenCV

Let us look on to the usage of this famous library and perform the few operations using OpenCV. Let us import OpenCV along with NumPy and Matplotlib.

opencv

We can read the image data using the cv2 command and store the image data into the N-dimensional array. As discussed in the previous tutorial of storing the image in to the array using the Numpy library with certain steps, using cv2 one can read the image by cv2.imread command.

After reading the image, it is better and safe to check the type of the image because sometimes even if the path of the image is wrong it reads some other data and stores it.

OpenCV Image Processing

You can look at the array elements of the image and check the shape of the image accordingly.

array

As stored in the previous tutorial the image looks with the proper colour texture. But with the OpenCV, the look of the image is quite different because Matplotlib library stores the image colours and layer them as red, green and blue whereas OpenCV layers them as blue, green and red.

OpenCV Image Processing

OpenCV provides the feature to convert the image from BGR format to RGB format accordingly, such that the image will look in the proper colour texture.

OpenCV Image Processing

We can also convert the image into the grayscale using OpenCV by mapping the colour values to gray accordingly.

OpenCV Image Processing

You can perform the resizing of the image into different sizes, based on the pixel arrangement, width and height ratio.

Using OpenCV one can rotate the images and flip them in different directions.

image

Now, let us look on to the basic code, which displays the image when the code is run and exits when escape key is clicked.

Sometimes, there is a problem to run the code in the Jupyter notebook results to the crashing of the kernel. To run the block of code, open the python file in the notebook and write the code accordingly, to expect the better results.

import cv2 
img = cv2.imread('DATA/00-puppy.jpg')
while True:
    cv2.imshow('Puppy',img)
    if cv2.waitKey(1) & 0xFF == 27:
        break
        
cv2.destroyAllWindows()

This block of code when executed will open a window that consists of the image that is read using OpenCV and will be displayed.

This completes the tutorial about the OpenCV basics to read and perform operations on the images. For any sort of doubts or questions, you can get them posted below in the comment box. 

Spread knowledge

Leave a Comment

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