A Gentle Introduction to OpenCV Using Python

8+
This article gives us a brief introduction and implementation of applying OpenCV techniques on images using Python.

OpenCV is a well known open-source computer vision and machine learning library. It was officially launched in 1999 by Intel.  We can develop various computer vision applications by using it. It provides convenience to the machine to identify the faces or objects.OpenCV supports various programming languages like Python, C++, Java, etc.OpenCV has been a necessary part of software development for a long time. 

In the article, I will give you a basic introduction to OpenCV using Python. We will cover the basic functionality of the library in this article. 

Do check about cross-validation and cross-entropy also. 

What is Computer Vision?

To make it simple for you let me consider a scenario. We all use Facebook, right? Let us consider you and your friends went on a trip and clicked a lot of pictures. Now, you want to post them on Facebook. You’ll notice it wouldn’t take much time just to find your friend’s faces and tag them in every picture? Facebook is intelligent enough to tag persons. But how does this auto-tag feature works? In simple terms, it uses computer vision. Computer Vision is a discipline that deals with making computers obtain a high-level understanding of digital images and videos. The intention here is to automate tasks that the human visuals can do. A computer is supposed to recognize objects like the face of a human being.

What is OpenCV?

OpenCV is a Python open-source library, which is used for computer vision in various applications. In OpenCV, the CV stands for computer vision. It was launched in 1999 by Intel. Initially, it was written in C/C++, but now it is generally used in Python for computer vision. It is defined as a field of study that helps computers to understand the material of digital images, for example, photographs and videos.

How does OpenCV work?

Human eyes render lots of information based on their observation. Machines convert whatever they see into numbers, and store it in the memory. Now you must be guessing how the computer converts images into numbers. Okay, the answer is, machines use the pixel values to convert images into numbers. A pixel is defined as the smallest unit of an image or display that can be printed and displayed by the computer

The pixel intensity of a particular location is represented by a number. In the above figure, we have shown the pixel values for a grayscale image where every pixel is containing one value only.

The most common ways to identify the images are – 

1.Grayscale

2. RGB

How to read and save images using OpenCV?

We can perform multiple operations on the image using OpenCV. We read image files as input and then perform operations accordingly. Let us see how to read and write images using OpenCV. 

Reading Image using OpenCV

The cv2.imread() function is used to load an image from the specified file and returns it. 

Syntax: cv2.imread(filename, flag)

The filename specified the file to be loaded. Flag describes the format or color type in which the image has to be loaded. 

Implementation:

We can see from the code, that imread() module is used to read the image.  The 0  in the parameters denotes a black and white image. If the parameter was 1 instead of 0, it would mean that the imported image is colored. 

If the image can’t be read, then imread() module returns an empty matrix. 

Saving Image using OpenCV

The write() module is used to save an image to the storage device. This will save the image according to the file extension. 

Syntax: cv2.imwrite(filename, image)

The filename specifies the file to be loaded. The filename has to include image format like .jpg, .jpeg, .png, etc.

Image is specifying the image to be saved. 

Implementation:

Output: The image is successfully saved as a file.

If the imwrite() method returns True, it indicates the file is successfully written in the secondary storage.

We saw how to read and save images using OpenCV. Let us see the basic operations that we can perform using OpenCV.

Basic Operations on Images using OpenCV

  1. Retrieve pixel values

We can access and modify pixel values using OpenCV. We will get an array of  blue, green, red values of the BGR image

Output :  [194 196 197]

  1. Image Properties

It is always good to know the size of the image you’re working on. Images are usually stored in the Numpy nparray in OpenCV. To find the dimension of the image we have to use nparray.shape. We can use the indexing to access the height, width, and number of channels.

Implementation:

Output: 

Image Dimension is   :  (269, 404, 3)
Image Height is:  269
Image Width is:  404
Number of Channels is :  3
Image Size is: 326028

  1. Region of Interest 

We can extract the region of interest using OpenCV. Region of the interest shows the specific area of an image on which we have to work on. 

Implementation:

  1. Rotating the Image

We can use the cv2.resize() function to resize the image. However, the aspect ratio of an image is not maintained when using this approach.  Hence, we have to put some extra effort to maintain a proper aspect ratio. 

Implementation:

Conclusion

This is it for this article. I hope you all clear with the basic things in OpenCV. I would recommend checking this interesting article on the chrome dragon game using OpenCV. 

Please do upvote, to keep me motivated to write more informative articles for you. 

close
8+

You may also like...

Leave a Reply

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

DMCA.com Protection Status