📷ROI Segmentation, Contour Detection and Image Thresholding Using OpenCV

OpenCV is a huge open-source library widely used in computer vision, artificial intelligence and image processing domains. The quintessential applications of it in real world are face recognition, object detection, human activity recognition, object tracking and such others.

Now, imagine you have to only detect an object from an entire input frame. So, instead of handling an entire frame, what if you can define a sub-region in the frame and treat it as the new frame to apply the processing. How do we do that you ask? Well, we are going to have a hands-on experience of implementing that in three different stages:

  • Defining Region of Interest(ROI)

  • Detecting Contours in ROI

  • Thresholding the frame outlined by detected contours

Let’s get started, shall we?

What is ROI?

In simple terms, a sub-region within a frame wherein our object of interest lies is known as Region of Interest(ROI).

How do we define ROI?

The process of defining the ROI in an input frame is known as ROI Segmentation.

In ROI Segmentation, (here) we are selecting a specific region in the frame and providing it’s dimensions in the rectangle method so that it will draw the rectangle-shaped ROI on the frame.

(Output) Region covered by the blue rectangle is our ROI

Now, if you want to bound the object of interest too then we can do that by finding contours in the ROI.

What is a Contour?

A Contour means an outline representing or say bounding a shape of an object.

How to find Contours in the frame?

For me, finding contours works best after thresholding the ROI frame. So, for finding the contours, the question on the hand is -

What is Thresholding?

Thresholding is nothing but a simple form of image segmentation. It is a process of transforming a grayscale or rgb image into a binary image. For an instance,

(This is a RGB frame)
(This is a Binary Thresholded frame)

So after thresholding the rgb frame, it becomes easy for the program to find contours because since the color of the object of interest in the ROI will be either Black(in simple Binary Threshing) or White(in Inverse Binary Threshing as above), the segmentation(separating background from foreground i.e our object) will be easily done.

After thresholding the frame and detecting the contours, we apply convex hull technique to the set a tight fitting convex boundary around the points of the object. Implementing this, the frame should look like this-

Another thing we can do is we can mask the ROI to display the object covered by the detected contours itself only. Again-

What is Image Masking?

Image masking is a process to hide some portions of an image and to reveal some portions. It is a non-destructive process of image editing. Most of the time it enables you to adjust and tweak the mask later if necessary. Very often, it is efficient and more creative way of the image manipulation.

So, Basically here we will mask the background of the ROI. To do so, first we will fix the background of the ROI. Then, after fixing the background, we’ll subtract it from the frame and replace it with the background wewant(here, a simple black frame).

Implementing above mentioned technique, we should get an output like this-

(The background is masked as to only capture the object)

Here’s the complete code for the desired implementation of the techniques explained.

Let's make some OpenCV magic happen!

Last updated