In the previous page, you have seen that you cannot just apply morphological transformation for more number of times as it will also degrade the quality of the text. Here, you will learn about contours, which can identify and extract the shapes present in the image.
What are contours?
Contours are the curves joining all the continuous points (along with the boundary), having the same colour or intensity. Since they are continuous points, each of the curves can be extracted.
Contours vs Edge Detection
One may argue that in edge detection, one identifies the pixels that act as a boundary where the change is maximum. Here also we identify the boundary, as in the case of contours, what is the difference? As contours are continuous points, they will always form a closed boundary. In edge detection, the boundary may be open or closed, it does not matter.
In the above image, there are 7 balls and the contour is represented by green colour. There are 7 contour which has half hemisphere like shape.
Let’s see this in action in the following video.
As explained, the objective of the contour is to identify and remove the dots. Contours will identify all the shapes present in the image, including the dots and well as text. Now, you will see to remove only the dots keeping the text as it is.
Here, we named each of the contours as blobs and store boundary of the contour by making a Blob() class. The Blob() class can also contain additional properties of the blob such as the perimeter of the blobs which have an area less than a threshold (in our case, the threshold is 30). The value of the threshold is, again, subjective and will vary based on the problem. Here, ’30’ denotes the approximate area of a dot. After we remove the dots, we get a clean image and produces a much superior result with the OCR library.
To summarise the whole process, we started with an image having text in it. We then blurred the image to remove the noise. Then, thresholding is done to convert the pixels, either to ‘o’ or ‘255’. This has made the text sharp but has also infused some noise in the form of dots. Then we tried to remove those dots by performing erosion and dilation operations. The problem here is that these operations work better if the size of the noise/dots is much smaller than the text. Doing more morphological transformation for more number of loops or increasing the size of the structuring element will also adversely affect the shape of the text. So, we identified the dots by finding the contours and removed the ones whose area is less than a threshold. Finally, we had a clean image which can be used as input to OCR.
Report an error