• Category
  • >Artificial Intelligence

OpenCV: Applications & Functions

  • Ripul Agrawal
  • Jun 30, 2020
  • Updated on: Jul 01, 2020
OpenCV: Applications & Functions title banner

"As more and more artificial intelligence is entering into the world, more and more emotional intelligence must enter into leadership."- Amit Ray

 

What is Computer Vision?

 

Computer vision is part of the artificial intelligence field, where computers are trained to intervene in digital media, i.e. images, videos like humans can, now it can be either stored media or live streaming from any camera or web-camera.

 

In simple words, with machine learning and deep learning algorithms, computers are able to recognize and process the objects from the video/images as a human can able to recognize. 


This image is showing the relation between AI, ML, and Computer Vision

Relationship of AI and CV, Source


 

Application of Computer Vision

 

  • Automobiles: 

Companies like Tesla and Waymo invested in self-driving cars, that will be assumed to be the future of the automobile industry.

 

  • Retail Stores:  

Amazon Go, which overcame the work of store owners by eliminating the need for a cashier and checkout counters, as a customer can directly pay their bills utilizing, which proves to save the time of the customer and disappear the endless queues of people waiting at the checkout counters for their turn.

 

  • Face Recognition: 

Using machine learning and computer vision, it’s now possible to recognize the faces of a particular person either from videos or photos. In particular, this application can also be utilized in surveillance.

 

  • Medical Diagnosis:  

Although it’s hard to substitute computers with a human in the healthcare sector. But still, computer vision will be helpful to the doctors in medical diagnosis as it can detect patterns from the images that humans can’t like, cancer tumours.

 

(Learn here how AI in healthcare with threats and applications)


 

What is the Open CV?

 

Open Source Computer Vision, started by Intel in 1999 for the computer vision and machine learning applications. Initially, it was developed in C/C++ which later developed to support other languages too including Python

 

It provides support to some of the deep learning frameworks including TensorFlow, PyTorch, Caffe. This blog will include the use of Open CV ‘s Python API.

 

Application of Open CV with Python

 

With the help of Open CV in python, its possible to process images, videos easily and can extract useful information out of that, as there are lots of functions available. Some of the common applications are,

 

  1. Image Processing: 

Images can be read, write, show, and processed with the OpenCV, like can generate a new image from that either by changing its shape, colour, or extract something useful from the given one and write into a new image.

 

  1. Face Detection:

Either from the live streaming using web camera or from the locally stored videos/images utilizing Haar- Cascade Classifiers.

 

  1. Face Recognition: 

It followed by face detection from the videos using open cv by drawing bounding boxes i.e. rectangle and then model training using ML algorithms to recognize faces.


This Image is showing the application of computer vision i.e. face recognition using OpenCV

Face Recognition using Open CV, Source


  1. Object Detection: 

Open CV along with the YOLO, an object detection algorithm can be used to detect objects from the image, videos either moving or stationary objects.


This image is showing the object detection algorithm YOLO which detects objects present in the image/video.

Object Detection using YOLO and OpenCV


Python represents an image as a Numpy array.

 

 

How to do Installation?

 

Run the below code to install an Open CV in your system. Install Python before following these steps.

 

  • Windows

 

In the command prompt,

pip  install OpenCV-python

In the anaconda prompt,

conda install -c conda-forge OpenCV

 

  • Linux

sudo apt-get install libopencv-dev python-Open

 

What are the Open CV functions?

 

  • Read, write and display the image

 

Images in python represented as NumPy array, basically array of pixels. Whenever the user reads the image using OpenCV it will be the colour image (by default) that contains multiple values for single-pixel i.e. for Red, green, Blue channels.

 

Run the below code to read and display image using OpenCV in google collab,


This image is demonstrating the code to read and display image from local storage using open cv

Read & Display image using OpenCV


When the user reads the image using OpenCV then there are different formats available, you just need to pass flag corresponding as per your requirement,

 

  • cv2.IMREAD_COLOR: Default format to read the image. 

  • cv2.IMREAD_GRAYSCALE: Load the image in grayscale format.

  • cv2.IMREAD_UNCHANGED: Load the image in the same format in which it stored.

Run the below code to read the image as grayscale and also to convert the colour image into a grayscale format,


  This image is demonstrating the code to convert the color image to grayscale and also the loading of images in grayscale format from the local storage.

Display the grayscale Image


Run the below code to convert the image into HSV format.


This image is demonstrating the code to convert the color image into HSV format using OpenCV

Display the HSV format


 Run the below code to write the grayscale image into the local machine.


This image is demonstrating the code to write any image into local storage using opencv

Write the image using OpenCV


  • Rotate the Images

 

Sometimes you require the same images with different orientation i.e. rotated images at different angles or you can say data augmentation.

Run the below code to rotate the image,


This image is demonstrating the code to perform data augmentation steps i.e. rotation of the image.

Rotate the image by 90 degree


  • Image Thresholding

 

An image segmentation technique, where each pixel value will be compared with some threshold value and will be updating the pixel value of the image.

But the image threshold will only work on a grayscale image.

 

Run the below code for image thresholding,


This image is demonstrating the code to perform image thresholding.

Image thresholding


There are different types of Image Thresholding flag available, which you can use as per your requirement, 

cv2.THRESH_BINARY,

cv2.THRESH_BINARY_INV,

cv2.THRESH_TRUNC,

cv2.THRESH_TOZERO,

cv2.THRESH_TOZERO_INV

 

  • Edge Detection

 

Edge detection will help in many applications like object detections from images/videos. 

Run the below code to detect edges from the image.


This image is demonstrating the code for detecting the edges in the image.

Edge detection of an image


  • Contour Detection

 

Now its time to detect contours from the images i.e. lines present in the images. Even you can also get the count of objects present in the image as well as shapes can also polygonal shapes can be detected present in the images.

 

Run the below code for contour detection,


 This image is demonstrating the code for detecting the shapes using contour detection.

Contour detection


  • Face Detection

 

As mentioned above, face detection can be done using OpenCV as there are machine learning-based classifiers available to detect face and different parts of the face by using appropriate classifiers i.e. Haar Cascade classifiers.

 

Run the below code to detect face from the images,


This image is demonstrating the code for detecting faces and other parts i.e. eyes, nose, etc from the images or live streaming using a web camera

Face Detection using OpenCV, Source


Apart from all the above-discussed functions, there are some more functionalities available by Open CV, for which you can refer to its official documentation. 


 

Conclusion

 

Computer Vision is the subfield of Artificial Intelligence, where computers are trained to process the image and extract the important features from the images or videos. Open Computer Vision (OpenCV) a python library, written in C++, provides various functionalities for computer vision applications. 

 

Applications of computer vision are object detection, face recognition, medical diagnosis, etc. Using OpenCV, users can read, display the images from the local storage, also images can be saved into local storage using OpenCV.

 

Apart from that images loaded can be read either in colour format or grayscale format. Edge detection is another method to detect edges of the image, contour detection will be used to detect all the shapes present in the image. Several functions and applications of OpenCV including face detection using Haar Cascade classifiers have been covered in this blog.

Latest Comments

  • Ripul Agrawal

    Dec 09, 2020

    For any queries, please reach out to me at ripulagrawal98@gmail.com