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])
ySample = np.flipud(contours[:,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

Sorry,could you post your code?It's helpful for everyone.

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

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)

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

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

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

Stats

Asked: 2015-05-11 17:18:33 -0500

Seen: 4,492 times

Last updated: May 11 '15