Share if you like it!
And especially if you don't!

Object Detection from Image and Video using HSV Color Space

In this essay, we will see how to detect an object from video & image using HSV (hue, saturation, value) color space, OpenCV library in C++ and Python programming languages.

Let's first quickly explore how these two applications (detection from image and video) look in practice and then we will go over OpenCV, HSV color space and the C++ and Python code.

Applications: Object detection from image and video

Object detection from image:

In this first application, we have an adjustable HSV mask ("Set Mask" window) which masks the original image and produces a resulting masked image. More details on this can be seen in the code which is heavily commented.

The program starts with a default Hue range (90, 140) which can detect blue objects. Then, we adjust the mask to find green and yellow objects as seen in the demo above.

Object detection from video:

In this second application, we have the same adjustable HSV mask ("Set Mask" window) but this time it masks the video (from the webcam) and produces a resulting masked video.

The program starts with a default Hue range (90, 140) which can detect blue objects. And then, we adjust the mask to find purple and red objects.

What is OpenCV?

Short for Open Computer Vision — OpenCV is exactly what the name implies. An open-source C++ library that tackles real-time computer vision tasks across a wide range of applications. Although it's primary interface is in C++, there are also bindings in Python, Java and MATLAB/OCTAVE.

With over 2500 classic and modern CV and ML algorithms, we prefer to use OpenCV because:

  • It's open-source and available free of cost.
  • It's fast and written in C/C++.
  • It's portable as OpenCV can run on any device that can run C. It runs on both desktop (Windows, Linux, Android, MacOS, FreeBSD, OpenBSD) and mobile (Android, Maemo, iOS).
  • It has low RAM usage.

Some features of OpenCV:

  • 2D and 3D feature toolkits
  • Egomotion estimation
  • Facial recognition system
  • Gesture recognition
  • Human–computer interaction (HCI)
  • Mobile robotics
  • Motion understanding
  • Object identification
  • Segmentation and recognition
  • Stereopsis stereo vision: depth perception from 2 cameras
  • Structure from motion (SFM)
  • Motion tracking
  • Augmented reality

Learn more at opencv.org or on Github.

What is HSV color space?

HSV (hue, saturation, value, also known as HSB [hue, saturation, brightness]) is an alternative representation of the RGB (Red-Green-Blue) color model. Created by computer graphics researchers in the 1970s, it's designed to match more closely to the way our human vision interprets color attributes.

Source

Code (C++ and Python)

In this section, we will see 4 programs: 2 programs (same logic in C++ and Python) for detection from image and 2 programs (same logic in C++ and Python) for detection from video.

The code is heavily commented and mostly readable. So, let's look at the code now which explains itself step by step in the comments. The lines starting with // for C++ and # for Python:

C++ : Object detection from image

Python: Object detection from image

C++ : Object detection from video

Python: Object detection from video

You can check out the source code here.

This wraps up our three-part series on Computer Vision — part one explored Image Similarity Search and part two outlined Face Recognition also using OpenCV, Python and C++.

Next up, we'll dive into Transfer Learning 💡

Thanks for reading 🙏

Previous Article
Most common challenges faced by startups
Next Article
A Quick Guide to Product vs Project Management

Get the useful tips on building startups to your email

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.