0

Is anybody of you aware of a higher order interpolation method (Catmull-Rom splines, cubic interpolation, etc.) for 2D contouring in Python?

Skimage, Matplotlib, and OpenCV provide the functions measure.find_contours(), contours() and findContours() respectively, but all are based on linear interpolation (also known as marching squares), I'm looking into something with higher accuracy in Python, preferably. Any pointers would be highly appreciated.

https://www.dropbox.com/s/orgr2yqhbbk2xnr/test.PNG

In the image above I'm trying to extract iso-value 25 from the scalar field of f(x,y)=x^3+y^3. I'm looking for 6 points with better accuracy than the 6 red points given by linear interpolation.

Gustavo Chavez
  • 119
  • 1
  • 2
  • 7
  • 1
    I think that you want to smooth your data before you pass it to contour. Have you seen this post? http://stackoverflow.com/questions/12274529/how-to-smooth-matplotlib-contour-plot Scipy's griddata function might also be of interest if you are not starting with regularly spaced data: http://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.griddata.html – DanHickstein Dec 01 '13 at 22:51
  • Hmmm, I think that this question is probably a duplicate of this one: http://stackoverflow.com/questions/18402355/matplotlib-data-cubic-interpolation-or-fit-for-contour-plot (If not, add more detail to the question to clarify.) – DanHickstein Dec 01 '13 at 22:56
  • Smoothing is a good option, however it creates more points than needed, i.e. the vertices are not positioned in the edges of the scalar field. I'm looking for a more accurate interpolation at the edges (which linear interpolation does not give). Just edited the original post and add an image – Gustavo Chavez Dec 02 '13 at 13:20
  • also, higher order does not always mean more accurate https://en.wikipedia.org/wiki/Runge%27s_phenomenon Higher order also makes you very sensitive to noise. – tacaswell Dec 03 '13 at 02:17
  • Agree, but wouldn't be a reasonable choice to pick a 3rd order interpolation for a family of cubic implicit functions to get the best possible approximation? – Gustavo Chavez Dec 03 '13 at 05:24

1 Answers1

0

For unstructured 2d-data (or triangulated data), you might be interested by the following class:

http://matplotlib.org/api/tri_api.html?highlight=cubictriinterpolator#matplotlib.tri.CubicTriInterpolator

which provides a Clough-Tocher (cubic) interpolator from a user-defined Triangulation and field defined at triangulation nodes. It can also be used through the helper class UniformTriRefiner:

http://matplotlib.org/api/tri_api.html?highlight=refine_field#matplotlib.tri.UniformTriRefiner.refine_field http://matplotlib.org/mpl_examples/pylab_examples/tricontour_smooth_user.png

Nevertheless the choice of the adapted interpolation depends of course of your data set.

GBy
  • 1,719
  • 13
  • 19
  • Hello there, thanks for the reply. I was aware of that routine but it's not quite what I'm looking for. I made a formal problem description on Latex in the current link in order to explain myself better: https://www.dropbox.com/s/f1hgzfgf0pyyqx1/Cubic%20Marching%20Squares.pdf – Gustavo Chavez Jan 19 '14 at 19:03
  • The restriction of the CubicTriInterpolator to one edge is a 3rd degree polynom ; you can easily get an analytical expression from the value of the function + derivatives at both ends. – GBy Jan 19 '14 at 20:43