Image Classification with Open CV-Python Overview

Nayana Mahajan
3 min readMay 25, 2022

--

Open CV (Computer Vision) is an open-source library that allows us to process images and videos to identify objects, faces, or even the handwriting of a human. Isn’t it cool??

In this post, I am going to give you an overview of the open cv library along with that we are going to use a feature of open CV for Image classification.

So, what is an open cv??

OpenCV is a huge open-source library for computer vision, machine learning, and image processing. OpenCV supports a wide variety of programming languages like Python, C++, Java, etc.

When it is integrated with various libraries, such as NumPy which is highly optimized for numerical operations OpenCV-Python makes use of NumPy, which is a highly optimized library for numerical operations with a MATLAB-style syntax.

First and foremost, To work with an OpenCV library, you need to install it in the virtual environment using pip install OpenCV-contrib-python

or if you are using google colab, OpenCV comes preinstalled on Google colab. so simply use import cv2

CV2 package has the following functions which can be used to deal with images:

1.imread() function is used to load the image and It also reads the given image (PIL image) in the NumPy array format.

2.imwrite() is used to save the image in the file.

3.resize() is used to resize the image from its original size to our desired size.

4.imshow() to display the image.

5.The only thing we need to convert is the image color from BGR to RGB. imwrite() saves the image in the file.

6.cvtColor() Once you load the image, you can also convert it to different color schemes using different flags in cvtColor cv2.cvtColor(image,cv2.COLOR_BGR2RGB)

7. cv2.split() Each picture has 3 channels and if we want to split each of them into separate images, we can do that by using split functions (channel_b, channel_g, channel_r) = cv2.split(img)

8. cv2.vconcat/hconcat() Use vconcat(), hconcat() to concatenate (combine) images vertically and horizontally. v means vertical and h means horizontal

9. ones/zeros If you want to fill an image (Mat) with ones or zeros for all three dimensions because Mat requires 3 layers/dimensions for a color image.

To read about cv2 Library visit the official page of open CV: https://pypi.org/project/opencv-python/

Check out my video on YouTube for a small tutorial on open cv functions in google colab: https://www.youtube.com/watch?v=GR3fiR41Vvg

Once you are comfortable using the above functions of Open CV that deal with images, we can move on to image classification with open CV.

Feature Detection and Matching with Open cv is one of the ways to classify images.

Feature detection and Matching functionality of Open cv in python allow us to detect the features of images by capturing their key points and descriptors.

ORB (Oriented FAST and Rotated BRIEF) function is one of the most widely used and open source functions that is fast and easily computes features in the images.

Well, there are other functions too like SWIFT and SURF but those are not free to use. Hence for learning purposes, you can use ORB. Trust me it’s very efficient!

open cv documentation website on ORB : https://docs.opencv.org/3.4/d1/d89/tutorial_py_orb.html
The code for using ORB to detect the key points and descriptors on the images
The code for using ORB to detect the key points and descriptors on the images
The green signs on the image are the key points that ORB has captured

For this post, I am letting you grasp this part. In my next post, I will continue on image classification.

Hope you like this.

Happy Learning!!

Please put down your thoughts and any requests in the comments

Or you can contact me at nayana.mahajan@gmail.com

My LinkedIn page:https://www.linkedin.com/in/nayana-mahajan-2914182a/

--

--