× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Having more control over the thinning algorithm?

Ray Phan
Jul 16, 2015
<p>You could always thin the characters first, then artificially expand their skeletons by performing morphology. For expanding, <a href="https://en.wikipedia.org/wiki/Dilation_(morphology)" rel="nofollow">morphological dilation</a> is most suitable. As such, thin the characters using the standard thinning algorithm, then dilate the result after using a suitable structuring element with a good size. The size of the structuring element should dictate how thick the thinned result is.</p> <p>To further exemplify my point, here's an example with an image I found on Google:</p> <p><img src="https://lh3.ggpht.com/aWaaZ-BsAXSYyyHRlube_NkiB-Q-FDx-Wpgg8qi5jqrNvAvNp87amEwSUNr7PdbCizY=w300" alt=""></p> <p>Reading this in with MATLAB and converting to binary:</p> <pre><code>im = im2bw(imread('https://lh3.ggpht.com/aWaaZ-BsAXSYyyHRlube_NkiB-Q-FDx-Wpgg8qi5jqrNvAvNp87amEwSUNr7PdbCizY=w300')); </code></pre> <p>This is what we get:</p> <p><img src="http://i.stack.imgur.com/jrHKZ.png" alt="enter image description here"></p> <p>Performing a thinning gives us:</p> <pre><code>thinned = bwmorph(im, 'thin', 'n=Inf'); </code></pre> <p><img src="http://i.stack.imgur.com/U6IcP.png" alt="enter image description here"></p> <p>If you want to increase the thickness of the thinning result so that the thickness is <code>n</code> pixels, use a basic square structuring element with size <code>n x n</code> and use this with the <a href="http://www.mathworks.com/help/images/ref/imdilate.html" rel="nofollow"><code>imdilate</code></a> function, which performs morphological dilation on binary images. In general, to increase the thickness of the text to have an <strong>overall</strong> thickness of <code>n</code> pixels, you would choose the size of the square structuring element to be <code>n</code>.</p> <hr> <p>Here are some examples of what I have discussed above.</p> <h1><code>n = 2</code></h1> <p>This would increase the thinning to be 2 pixels wide:</p> <pre><code>se = strel('square', 2); expand = imdilate(thinned, se); imshow(expand); </code></pre> <p>The function <a href="http://www.mathworks.com/help/images/ref/strel.html" rel="nofollow"><code>strel</code></a> defines different structuring elements, but we will choose the square one via <code>'square'</code> flag. Dilating the thinned image that you see above, we get:</p> <p><img src="http://i.stack.imgur.com/eqduu.png" alt="enter image description here"></p> <h1><code>n = 5</code></h1> <p>Simply change the structuring element to size to 5 x 5, and we get:</p> <pre><code>se = strel('square', 5); expand = imdilate(thinned, se); imshow(expand); </code></pre> <p><img src="http://i.stack.imgur.com/KD7KE.png" alt="enter image description here"></p> <hr> <p>If you take any of the results and zoom into the text, you will see that the width of each stroke is indeed either 2 or 5 pixels. However, the assumption with the above code is that each character is sufficiently separated to allow the variable thickness of each stroke to be maintained. Should the characters be very close together, then dilation will merge these text characters together... but the thinning algorithm will most likely give you bad results even before dilation.</p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/31386139/Having%20more%20control%20over%20the%20thinning%20algorithm?/31386405">Stack Overflow</a>.</p>
comments powered by Disqus