Receipt Scanner (Text Detection)05 Oct 2015
Text extraction from image becomes more popular these days. For example text translation from images, receipt scanner, paper scanner, etc.
But the question is, how is it done? First, to recognize what texts inside the image we need to know where the texts are located and that’s our topic today.
My approaches to detect where the texts are:
- Change color space from BGR (or RGB) to Gray
- Apply Morphological Gradient
- Apply threshold to convert to binary image
- Apply Closing Morphological Transformation
- Find contours and filter it
Let’s take a look at each step in detail.
###Change color space from BGR to Gray.
In image processing it’s pretty common to change the color space from BGR (or RGB) to Gray. One of the reason is luminance is by far more important in distinguishing visual features compared to chrominance (Good explanation by John Zhang).
To convert image from BGR (or RGB) to Gray color space in OpenCV is quite simple.
###Apply Morphological Gradient.
Morphological Gradient is a combination from dilation and erosion. Where dilation will act as local maximum operator and erosion as local minimum operator.
Which morphStructure is kernel of structuring element that will define how the gradient will be calculated.
To understand more about dilaton and erosion you can check really nice demonstration in OpenCV’s example here
###Apply threshold to convert to binary image I’m using Otsu algorithm to choose the optimal threshold value to convert the processed image to binary image.
###Apply Closing Morphological Transformation
And the last one before we can find the contours we need to do Closing Morphological Transformation which is dilation followed by erosion that will close small holes between words so we can group it into a sentence horizontally.
###Find contours and filter it
After all the processing it’s time to find contours and filter it so we finally can locate where the texts are located.
That’s all! I hope my post can help you to understand how to detect texts inside an image.
For recognizing the texts I will write a post about it in the future.
If you have any question, send me a tweet! If you like this post you can share it with your friends using share buttons below.