the code to use to crop contour to segments is implemented in mex