Wednesday, August 22, 2012

Act 8. Morphological operations application 1: Writing extraction

 Given the photo of a receipt shown below, we were tasked to crop an area and isolate the text.

Raw image
 After learning erode and dilate in the previous lesson, of course I was excited to put it to use!

After binarizing the cropped area, we erode it with horizontal structuring element. This must be longer than the length of one letter in the text, but it must be short enough so that the slight tilt of the lines would not pose a problem.

From the dilation, we get an image containing only the horizontal lines. Inverting this gives us a filter that is zero only on these lines. Multiplying this filter with the binarized image isolates the text. As a final step, we thin the image to make the letters one pixel thick simply by applying thin(Im).
(left) cropped are (middle) binarized image (right) isolated text
This code is limited in the sense that one would have to do additional masking(erosion, inversion, multiplication) to remove the vertical lines. This would not be a problem if we just edited the image in the frequency domain. (Remember this?)

Plus, since this code deletes all horizontal lines, it would erase part of the text if the text happened to intersect a horizontal line. This can be remedied a little by dilation of a short vertical structuring element.

Here's the code that takes the cropped picture and isolates the text. :)
Since I was successfully able to do the required task, I give myself a grade of 10/10.

Thursday, August 2, 2012

Act 7: Erosion and dilation: Morphological Operations

(To follow: video tutorial of how to get the erosion/dilation of an image given a structuring element)

In this activity, we were asked to predict the dilation and erosion of the following images:
  • 5x5 square
  • 4x3 triangle
  • 10x10 hollow square; 2 pixels thick
  • 5x5 cross; 1 pixel thick
using the following structuring elements(SE):
  • 2x2 square
  • 2x1 rectangle
  • 1x2 rectangle
  • 3x3 cross
  • 2x2 (lower left to upper right) diagonal
Here are my predictions:



 

 
The predicted dilation(top) and erosion(middle) of four image(in four pages) 
with the structuring elements at the bottom row of each page.


As we can see, erode looks for the SE in the image. Dilate makes the images thicker, with the "pressure" being related to the SE. It must be noted that these processes are not inverses of each other; taking the dilation of the erosion of an image (or vice-versa) does not return the original image. In fact, that process is called opening, and the reverse is called closing.

By virtue of changing the structuring element, one can choose which characteristic one wants to highlight.

Next, we check the predictions using SciLab. Since SIP has morphological operations, getthing the dilation and erosion is easy and intuitive.
dilate(Image, SE);
erode(Image, SE);





Images(top) with various structuring elements(left) 
and the corresponding erosion(middle) and dilation(right)

Almost all of the predictions were confirmed, except for the erosion of the hollow 10x10 and 5x5 cross using the diagonal structuring element. Rechecking my work assures me that it was not simply a careless mistake. The result from the code does not seem to fit the pattern - perhaps an anomaly? It would be of interest to inspect the code for the dilate and erode commands.

 I was able to do the necessary work, so 10/10. :)