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:

Thinning Examples

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.

Thinning Example R

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:

Thinning Lib 1 Thinning Lib 2 Thinning Lib 3



  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

2 thoughts on “Image Thinning Using R

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s