Click here to Skip to main content
15,559,959 members
Articles / Programming Languages / C#
Technical Blog
Posted 29 Apr 2013

Tagged as


6 bookmarked

Image Thinning Using R

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
29 Apr 2013GPL33 min read
Image thinning using R.


I was recently looking on the internet for a good binary image thinning algorithm using R. According to Wikipedia, “Thinning is the transformation of a digital image into a simplified, but topologically equivalent image. It is a type of topological skeleton, but computed using mathematical morphology operators.” [1]  However, my favorite description of thinning comes from Mafahir’s blog: “Morphological Erosion is like setting the edges of the digital pattern on fire. Where the fire erases the boundaries little by little until the digital pattern is one or two pixels in width.” [2]  Image thinning is a form of this so called “Morphological Erosion”.  While I am sure that there are many great purposes for image thinning, I required image thinning for the purposes of image clustering in R.  After scouring the internet, I was unfortunately unable to find a good image thinning application written in R. Upon locating a good C++ / OpenCv implementation of the Zhang-Suen algorithm for image thinning explained in “A fast parallel algorithm for thinning digital patterns”. [3]  I set out to convert this code to R. [4]

Image Thinning Examples

The following “thinned” before and after images were produced using my own version of the Zhang-Suen algorithm written in R.   Upon review of the code, you will see it is a very close replication of the C++ code described above to which I owe substantial credit for this thinning innovation.  In addition, I cannot take complete credit for the code’s conversion to R as Dr. Michael Hahsler was kind enough to contribute some of his debugging expertise as well.  It is important to note that the thinning library code is only intended for the thinning of binary images.  This means that gray-scale or color images must be converted to binary before executing the thinning operation.  This can clearly be seen in the examples below:

Image 1

Using the Image Thinning Library in R

I used the following R code to produce the images above.  In my own situation, I was working with a collection of 28 x 28 pixel scans of handwritten images.

Image 2

The Image Thinning Library in R

You will notice that in the example code above, the function thinImage() is called to convert the binary image to a “thinned” image as displayed in the examples above.  This function can be found in the following image thinning library which I implemented using R:

Image 3

Image 4

Image 5



  1. Thinning (morphology). (2013, April 14). In Wikipedia, The Free Encyclopedia. Retrieved 19:19, April 28, 2013, from
  2. Image Thinning/Skeletonization (C/C++ Implementation).  (2011, February 2). In Mafahir: A Developers Diary. Retrieved 19:28, April 28, 2013, from
  3. A Fast Parallel Algorithm for Thinning Digital Patterns. (1984, March).  T. Y. Zhang, C. Y. Suen. Retrieved 19:37, April 28, 2013, from
  4. Implementation of thinning algorithm in OpenCV. (2012, December 25). OpenCV Code. Retrieved 19:40, April 28, 2013, from


This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

Written By
United States United States
If you would like to know more about me, please feel free to visit my website at


Jake Drew

Comments and Discussions

-- There are no messages in this forum --