Explore OpenCV & Why Do We Need To Know About It?

Reading Time: 4 minutes

OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning software library. It was built to provide a common infrastructure for computer vision applications and to accelerate the use of machine perception in commercial products. Being a BSD-licensed product, OpenCV makes it easy for businesses to utilize and modify the code.

OpenCV

OpenCV is the huge open-source library for computer vision, machine learning, and image processing and now it plays a major role in real-time operation which is very important in today’s systems. By using it, one can process images and videos to identify objects, faces, or even the handwriting of a human. When integrated with various libraries, such as NumPy, python is capable of processing the OpenCV array structure for analysis. To Identify image patterns and their multiple features we use vector space and perform mathematical operations on these features. 

The purpose of computer vision is to understand the content of the images. It extracts the description from the pictures, which may be an object, a text description, and three-dimension model, and so on. For example, cars can be facilitated with computer vision, which will be able to identify different objects around the road, such as traffic lights, pedestrians, traffic signs, and so on, and acts accordingly.

Computer vision allows the computer to perform the same kind of tasks as humans with the same efficiency. Following are the two main tasks:

  • Object Classification – In object classification, we train a model on a dataset of particular objects, and the model classifies new objects as belonging to one or more of your training categories.
  • Object Identification – In object identification, our model will identify a particular instance of an object – for example, parsing two faces in an image and tagging one as Virat Kohli and another one as Rohit Sharma.

OpenCV installation

There are many ways in which you can install OpenCV on your computer. Here are some:

Install using Anaconda

Anaconda is a conditional free and open-source distribution of the Python and R programming languages for scientific computing, that aims to simplify package management and deployment. You can download it from here and install it.

After successfully installing anaconda, just go to the anaconda prompt and use this command to install OpenCV:

conda install -c conda-forge opencv

For Windows

You can use pip to install OpenCV on windows. Pip is a de facto standard package-management system used to install and manage software packages written in Python and it usually comes in installed when you install Python. If you do not have Python installed, I would suggest downloading it from here. Use this command in the command prompt to install OpenCV:

pip install opencv-python

For Mac

You can use homebrew to install OpenCV as it makes it really easy and you just have to use this command for installing:

brew install opencv

Now that you have installed the OpenCV onto your system, let’s see how it works.

How Does A Computer Read An Image?

Consider the below image:

OpenCV Python Tutorial - Edureka

We can figure out that it is an image of the New York Skyline. But, can a computer find this out all on its own? The answer is no! The computer reads any image as a range of values between 0 and 255. For any color image, there are 3 primary channels – Red, green and blue. How it works is pretty simple.

A matrix is formed for every primary color and later these matrices combine to provide a Pixel value for the individual R, G, and B colors.

Each element of the matrices provide data pertaining to the intensity of brightness of the pixel.

Consider the following image:

OpenCV Python Tutorial - Edureka

As shown, the size of the image here can be calculated as B x A x 3.

Benefits of OpenCV

The main benefit of OpenCV is its vast access to algorithms, extensive use, and algorithmic efficiency. Here are more details of its benefits:

Vast Algorithms

OpenCV gives access to more than 2,500 state-of-the-art and classic algorithms. By using this library, users can perform various tasks like removing red eyes, extracting 3D models of objects, following eye movements, etc.

Extensive Use

Big companies like IBM, Google, Toyota, and even startups like Zeitera and Applied Minds are using OpenCV for multifarious tasks.

In the vast community of OpenCV, users can ask for assistance and provide help to other developers. This gives developers access to insights of people about libraries and codes.

Efficient Solution

OpenCV provides algorithmic efficiency mainly to process real-time programs. Moreover, it has been designed in a way that allows it to take advantage of hardware acceleration and multi-core systems to deploy.

Technical Specifications

OpenCV Demo

# Python program to explain cv2.imwrite() method

# importing cv2
import cv2

image_path = 'example.png'

# Using cv2.imread() method
# to read the image
img = cv2.imread(image_path)

# Filename
filename = 'savedImage.jpg'

# Using cv2.imwrite() method
# Saving the image
cv2.imwrite(filename, img)

# Reading and showing the saved image
img = cv2.imread(filename)
cv2.imshow("GeeksforGeeks", img)

cv2.waitKey(0)
cv2.destroyAllWindows()

Conclusion

In conclusion, we have studied what is OpenCV, the Benefits of OpenCV, its Technical Specifications, How a computer read an Image, and how to install OpenCV on Windows, Linux, and Mac. I hope this blog gives you some understanding to start with OpenCV.

Happy Reading !! 🙂

Written by 

Tanishka Garg is a Software Consultant working in AI/ML domain.