Grabcut algorithm —An OpenCV feature that might improve image handling model’s performance greatly.

Saiyam Bhatnagar
Analytics Vidhya
Published in
3 min readJun 30, 2020

The objective behind this story is not to Mathematically illustrate the algorithm behind the Grabcut but is to demonstrate how greatly the feature can improve a model’s performance. Please keep in mind that the story does not, in any sense, infer that the feature works well on all Image handling models. Recently, while participating in a Computer Vision Hackathon that consisted of 5 different problem statements, I decided to read the famous OpenCV’s documentation. In my attempt to see how I could use different tools for data pre-processing, I initially landed upon the Grabcut. I’ll discuss my model performance with and without the Grabcut.

The problem statement was to classify images of leaves into healthy and non healthy category. Although I intended to use a larger and a better model initially but the five hour time constraint backed up my inability to do so. The data-set consisted of pictures of leaves with a few background details. However, the ROI (region of interest) was primarily the surface of the leaf.

Sample image from data-set

The reason that the background was of no need to infer results, I decided to get rid of it without explicitly training a model to do so. Therefore, I decided to use Grabcut. As mentioned earlier the story won’t get into the intricacies of the algorithm, I won’t be discussing the functionality except the key points present. The rect variable in the code stores the dimensions of the RIO in the image. So, this should be chosen wisely.

the function for implementing Grabcut

After using the algorithm, I could easily obtain images with no background. Look below.

An image post Grabcut

For images in which the algorithm removed the complete leaf, I selected those using image thresholding and created a simple black border around them. See the figure below.

image on which Grabcut didn’t work correctly

As a result I got highly improved model accuracy. The former model saturated at 95% accuracy while the latter achieved more than 99% in lesser epochs.

the former model
the latter model

This was how I built an image classification model within time constraints. However, please do not forget to augment the test data in similar way :-) . Special thanks to Google’s free TPU to help me rank in the top ten in the Hackathon.

--

--