Ask Your Question
1

cubic spline interpolation on contour points

asked 2015-05-11 17:18:33 -0600

NabeelKhan gravatar image

updated 2015-05-11 21:09:47 -0600

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

image description

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:

image description

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):

image description

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 flag offensive close merge delete

Comments

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

wuling gravatar imagewuling ( 2015-05-11 19:24:22 -0600 )edit

code is added

NabeelKhan gravatar imageNabeelKhan ( 2015-05-11 21:10:08 -0600 )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

wuling gravatar imagewuling ( 2015-05-11 23:24:03 -0600 )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)

NabeelKhan gravatar imageNabeelKhan ( 2015-05-11 23:33:32 -0600 )edit

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

wuling gravatar imagewuling ( 2015-05-12 02:13:24 -0600 )edit

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

NabeelKhan gravatar imageNabeelKhan ( 2015-05-12 15:25:56 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-12-13 20:45:39 -0600

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

edit flag offensive delete link more

Question Tools

1 follower

Stats

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

Seen: 7,688 times

Last updated: May 11 '15