欢迎关注微信公众号——计算机视觉:
How to get the skeleton of an object using OPENCV
http://pinkjade8848.spaces.live.com/blog/cns!E4159959CD42C507!195.entry
Skeletonization is a process for reducing foreground regions in a binary image to a skeletal remnant that largely preserves the extent and connectivity of the original region while throwing away most of the original foreground pixels. Generally speaking, there are 2 methods to obtain the skeleton.(I am not sure there are other methods.)
thinning
distance transform
OpenCV doesnot provide functions for skeletonization, but provides some functions on distance transform, so I use distance transform to complete this task.
Distance transform is an operator normally only applied to binary images. The result of the transform is a gray level image that looks similar to the input image, except that the gray level intensities of points inside foreground regions are changed to show the distance to the closest boundary from each point
This figure show the distance transform.
Following steps show how it achieves:
Input: one image
Output: skeleton image
Step1: convert the input image to grey image if the input one is a 3-channel image. convert the grey image to binary image using threshold:
cvThreshold( src, src, 1, 255, CV_THRESH_BINARY );
Step 2: using distance transform functions to get the distance transform images.
cvDistTransform( src_img, dist_img, CV_DIST_L2,3 ,NULL,NULL );
notes : dist_img is a 32 bit float image, so if it is be display by cvShowImage(), we will find that it is same as src_image. It is better to convert 32F image to 8U image (cvConvertScale(dist_img,dist_img_8u,1,0);), and then show it.
Step 3: Take Laplacian (2nd derivative) of the distance transformed image. cvLaplace(dist_img,dist_img_laplac,3);
Step 4: threshold the result of step 3. this is very important. Otherwise , you can not get it. (Till now. I am not sure how to select the threshold. )
It is supposed that the central ridge in the distance transform should be the same as thee medial axis or skeleton.
In order to remove noise, it is suppose to do some morphology before step 1.