# cubic spline interpolation on contour points

I have traced the boundary of an object using openCV as shown in the figure :

I have vectorX and vectorY holding the x-axis and y-axis coordinates of above curve. Th curve looks fine but when we zoom it in we find that the curve is not smooth i.e. jagged:

I require smooth curve before further processing. In MATLAB, we can use CSAPS (Cubic smoothing spline) to smooth such curves and it works pretty well. I am looking to do the same thing either using openCV or some free C++ library. I am looking to get an output like the following (where curve is smoothed by CSAPS function):

Any help will be really appreciated in this regard.

My code:

ret,thresh = cv2.threshold(imgray,50,255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

xSample = np.flipud(contours[0][:,0,0])
ySample = np.flipud(contours[0][:,0,1])

arraySample = np.vstack([xSample, ySample])
arraySample = arraySample.T
plt.figure(2)
plt.plot(arraySample[:,0],  arraySample[:,1], 'ro-')
plt.show()

edit retag close merge delete

( 2015-05-11 19:24:22 -0500 )edit

( 2015-05-11 21:10:08 -0500 )edit

hi, maybe you can try draws contours outlines:drawContours or filled contours drawContours or Approximates a polygonal curve(s) with the specified precision :approxPolyDP

( 2015-05-11 23:24:03 -0500 )edit

I also need to preserve the sequence of points for complex objects. After drawing contour, I cannot keep track of it .. Moreover, it's still not smooth (I tired that)

( 2015-05-11 23:33:32 -0500 )edit

I think you use python,in numpy, there is a fununction:numpy.polyfit.I think it's helpful.

( 2015-05-12 02:13:24 -0500 )edit

yes, I can apply interpolation in python. Though looking for alternative in C++

( 2015-05-12 15:25:56 -0500 )edit

Sort by ยป oldest newest most voted

Just had a similar problem, needed a C++ natural, smoothed cubic spline similar to Matlab's csaps() and R's smooth.spline(). There is now one available here (see skel__Csppline.h and skel__TestCsppline.h): https://bitbucket.org/aperezrathke/skel

more

Official site

GitHub

Wiki

Documentation