A Box detection algorithm for any image containing boxes.

Kanan Vyas
Coinmonks
6 min readJul 22, 2018

--

When you are working with Optical character recognition(OCR) or any data or object recognition problem, the first thing to do is preprocessing. Here preprocessing means to extract the location where our information is located. After extracting the location, any machine algorithm will be performed on that image.

The problem arises when you have to detect objects which are located in any tables/boxes or in row-column format. If the image is like this then you have to detect boxes and extract them one by one. Now it should be done accurately for all images. As an example, see this following image:

Example of an image for extracting information.

Here for this image I want to do Optical Character Recognition for all the equations. I want to extract each cell one by one(not any blank) to detect the numbers. After extracting each cell I will do segmentation for all the numbers and apply my ML model to do recognition.For this algorithm we will use python language by using opencv and numpy.So let’s start extracting each cell one-by-one:

First import some libraries:

Now read the image,Convert it into grayscale, Do thresholding and invert the image.

So our image will look like this:

image_bin.jpg

Now we need to detect boxes.For that we will use morphological operations.For that We will define rectangular kernel with the length based on the width of the image.We will define two kernels. 1) Kernel to detect horizontal lines. 2) Kernel to detect vertical lines.

Now after defining kernels we will do morphological operations to detect the vertical and horizontal lines.Below code shows the image containing vertical lines.

Image containing vertical lines
Image containing horizontal lines

Now we will add these two images.This will have only boxes and the information written in the box will be erased.So we can accurately detect the boxes and no noise will occur for false box extraction.

Final image containing only boxes

Now we will apply findContours() method to this image. This will find all the boxes and we will sort them from top to bottom.For sorting the contours we will use the function provided by https://www.pyimagesearch.com/2015/04/20/sorting-contours-using-python-and-opencv/. We will use top-to-bottom approach.

Now loop over all the contours,find the location of all the boxes and crop the part which has a rectangle and save it into one folder.

Now it’s Done!. Check your folder and you will see images containing every extracted boxes.Like this:

Extracted images

So now you can use this images for further implementation.You can change the kernel_length parameter by increasing to get a good output in very large image.

Note: This method is applicable everywhere, For detecting data from OMR sheets to any excel sheets. This method uses normal morphological operations and it erased all the inner information so there will be no noise coming in for false detection of boxes. You can use the following method as a preprocessing and get a good output.:)

The whole code for box detection is here:

You can see the full source code here:

Thank You!

Join Coinmonks Telegram Channel and Youtube Channel get daily Crypto News

Also, Read

--

--

Kanan Vyas
Coinmonks

Software Engineer in ML | Founder at @clique_org | Interested in Cricket and Kathak.