Creating connected areas

If you take a closer look at the image, you'll notice that the letters are always together in blocks, formed by text paragraphs. That leaves us with the question, how do we detect and remove these blocks?

The first step is to make these blocks even more evident. We can do this by using the dilation morphological operator. Recall fromĀ Chapter 8, Video Surveillance, Background Modeling, and Morphological Operations, that dilation makes the image elements thicker. Let's look at a small code snippet that does the trick:

auto kernel = getStructuringElement(MORPH_CROSS, Size(3,3)); 
Mat dilated; 
dilate(input, dilated, kernel, cv::Point(-1, -1), 5); 
imshow("Dilated", dilated); 

In the preceding code, we start by creating a 3 x 3 cross kernel that will be used in the morphological operation. Then, we apply dilation five times, centered on this kernel. The exact kernel size and number of times vary according to the situation. Just make sure that the values glue all of the letters in the same line together.

The result of this operation is presented in the following screenshot:

Notice that we now have huge white blocks. They match exactly with each paragraph of text, and also match with other non-textual elements, like images or the border noise.

The ticket image that comes with the code is a low resolution image. OCR engines usually work with high resolution images (200 or 300 DPI), so it may be necessary to apply dilation more than five times.