Morphological Operations for Text Detection in high contrast Images

In my stint with image processing.I have come to believe that the most important part of image processing is not the actual “detection” but the preprocessing that goes before the operation.In my previous post I had talked about how to use cvDilate .CvDilate is specifically used when the connected components are not properly connected.

As opposed to Dilation ,we have something called erosion which involves choosing of Maxima.In this post I will be showing you the effects of Morphological operations on an image with text.Our sample image will be an image with high contrast.Sample text with Image

Now our first step will be to convert this image to gray scale using cvConvertImage;

Now we apply a morphological operation called Top Hat.Before explaining Tophat operate let me tell you a little about “Closing” an image.Closing(Morphological Close) is basically dilation done after erosion.The reason for erosion is to eliminate noise and speckle in an image.The reason for use of erosion over blurring is that large significant regions do not get affected.It is the protrusions that get eroded.

After that Dilation is done to connect the components which are very close to each other giving rise to connected components.

Thus Morphological Close = Erosion and then Dilation.

Morphological Top Hat Operation:

TopHat(src) = src–open(src)

Thus TOP HAT reveals areas that are lighter than the surroundings,which is exactly that we would require in this case.Notice that color of the text is lighter than the surroundings.

The GrayScale Image


In my previous post while explaining Dilation I asked you to imagine a disc(Kernel) moving over the image and replacing all the cells that it covers with the local maxima.Now instead of a disc,I will be using my own kernel.

Making your kernel is pretty easy using cvCreateStructuringElementEx().I have used a 21*3 kernel,specifically because I want my algorithm to work on horizontal text like License Plates.

After application of TOPHAT morphological operations.


You can notice slight stretch marks sort of.That is because we have used a fairly large rectangular Kernel.The result is exactly as we wanted.Now the next step is Thresholding.

We will be apply Binary thresholding in which any pixel above 128 will be replaced with 255 and all the ones below it will become 0.Thus actually we are brightening the brighter parts of the image.

Result after Thresholding.


Now the stretch marks appear more visible cause they have been reduced to “O”

The image is also speckled so we need to smooth it using cvSmooth


Now the last step is obviously cvDilate as we can see gaps between the connected components.Example :The letter R which should have been together is filled with gaps.Now we need to apply dilation.But since we are also thinking about license plates,so we already know that these letters will be closely placed and it may be difficult to get each letter as a connected component.I will talk about that in my next post.

Anyway I again use my own kernel,optimized for horizontal closely placed texts.

Final Result.


You can see the texts are not visible at all instead they have been blurred together to form a Blob or a CONTOUR.This is exactly what you would want when going for license plates.Getting each letter is not only tedious but a waste of time .Localization can be best done when you can Coalesce the text into a blob.

The Source:

  1:         img3=cvCreateImage(cvSize(img->width,img->height),img->depth, 1 );
  2: 	IplImage *img_temp=cvCreateImage(cvSize(img->width,img->height),img->depth, 1 );
  3: 	cvSetZero(img_temp);
  4: 	cvConvertImage(img, img3,0);
  5: 	display(img3);
  6: 	IplImage *cc_color=cvCreateImage(cvGetSize(img3), IPL_DEPTH_8U, 3);
  7: 	cvMorphologyEx(img3,img3,img_temp,cvCreateStructuringElementEx(21,3,10,2,CV_SHAPE_RECT,NULL),CV_MOP_TOPHAT,1);
  8: 	display(img3);
  9: 	cvThreshold(img3,img3,128,255,CV_THRESH_BINARY);
 10: 	display(img3);
 11: 	cvSmooth(img3, img3, CV_GAUSSIAN, 5, 5 );
 12: 	display(img3);
 13: 	cvDilate(img3,img3,cvCreateStructuringElementEx(21,3,10,2,CV_SHAPE_RECT,NULL),2);
 14: 	display(img3);

I have used a high contrast image so that it resembles a typical License Plate.

The next steps will be done in my next post.Stay Tuned. Smile


One response to “Morphological Operations for Text Detection in high contrast Images


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s