The Pavlidis algorithm for contour tracing
This algorithm traces the contour of a binary mask in a clockwise direction. It was first described by Theo Pavlidis in "Algorithms for Graphics and Image Processing", 1982, chapter 7.
This implementation is derived from http://www.imageprocessingplace.com/downloads_V3/root_downloads/tutorials/contour_tracing_Abeer_George_Ghuneim/theo.html