Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8911633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:59:47+00:00 2026-06-15T03:59:47+00:00

I have a data that represents a closed contour (with noise): contour = [(x1,

  • 0

I have a data that represents a closed contour (with noise):

contour = [(x1, y1), (x2, y2), ...]

Is there any simple way to fit the contour? There is the numpy.polyfit function. But it fails if x values are repeated and requires some effort to determine an adequate degree of the polynomial.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T03:59:48+00:00Added an answer on June 15, 2026 at 3:59 am

    The distance from a point to the contour you are trying to fit is a periodic function of the angle in polar coordinates centered at that point. This function can be represented as a combination of sine (or cosine) functions which can be calculated exactly by the Fourier transform. Actually, the linear combination calculated by the Fourier transform truncated to the first N functions is the best fit with those N functions, according to Parseval’s theorem.

    To use this in practice, pick a center point (perhaps the center of gravity of the contour), convert the contour to polar coordinates, and calculate the Fourier transform of the distance from the center point. The fitted contour is given by the first few Fourier coefficients.

    The one remaining problem is that the contour converted to polar coordinates does not have distance values at evenly spaced angles. This is the Irregular Sampling problem. Since you have presumably a rather high density of samples, you can go around this very simply by using linear interpolation between the 2 closest points to the evenly spaced angle, or (depending on your data) averaging with a small window. Most other solutions to irregular sampling are vastly more complicated and unnecessary here.

    EDIT: Sample code, works:

    import numpy, scipy, scipy.ndimage, scipy.interpolate, numpy.fft, math
    
    # create simple square
    img = numpy.zeros( (10, 10) )
    img[1:9, 1:9] = 1
    img[2:8, 2:8] = 0
    
    # find contour
    x, y = numpy.nonzero(img)
    
    # find center point and conver to polar coords
    x0, y0 = numpy.mean(x), numpy.mean(y)
    C = (x - x0) + 1j * (y - y0)
    angles = numpy.angle(C)
    distances = numpy.absolute(C)
    sortidx = numpy.argsort( angles )
    angles = angles[ sortidx ]
    distances = distances[ sortidx ]
    
    # copy first and last elements with angles wrapped around
    # this is needed so can interpolate over full range -pi to pi
    angles = numpy.hstack(([ angles[-1] - 2*math.pi ], angles, [ angles[0] + 2*math.pi ]))
    distances = numpy.hstack(([distances[-1]], distances, [distances[0]]))
    
    # interpolate to evenly spaced angles
    f = scipy.interpolate.interp1d(angles, distances)
    angles_uniform = scipy.linspace(-math.pi, math.pi, num=100, endpoint=False) 
    distances_uniform = f(angles_uniform)
    
    # fft and inverse fft
    fft_coeffs = numpy.fft.rfft(distances_uniform)
    # zero out all but lowest 10 coefficients
    fft_coeffs[11:] = 0
    distances_fit = numpy.fft.irfft(fft_coeffs)
    
    # plot results
    import matplotlib.pyplot as plt
    plt.polar(angles, distances)
    plt.polar(angles_uniform, distances_uniform)
    plt.polar(angles_uniform, distances_fit)
    plt.show()
    

    P.S. There is one special case that may need attention, when the contour is non-convex (reentrant) to a sufficient degree that some rays along an angle through the chosen center point intersect it twice. Picking a different center point might help in this case. In extreme cases, it is possible that there is no center point that doesn’t have this property (if your contour looks like this). In that case you can still use method above to inscribe or circumscribe the shape you have, but this would not be a suitable method for fitting it per se. This method is intended for fitting “lumpy” ovals like a potato, not “twisted” ones like a pretzel 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data model that represents a weekly order. Eg. my order might
I have a table of data which represents a series of events that persons
I have some data that won't printf.... echo works, but not printf There is
I have some time series data that represents cumulative sums of a number of
I have a byte[] that contains all the data that represents an image. How
I have set up a circular linked list data structure that represents a word,
So I have the following data that represets the hour and quarter hour with
I have some data in a database that I need represented in an XML
I have data that is organized in kind of a key-key format, rather than
I have data that looks like this: #info #info2 1:SRX004541 Submitter: UT-MGS, UT-MGS Study:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.