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

  • SEARCH
  • Home
  • 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 6064631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:18:05+00:00 2026-05-23T09:18:05+00:00

I have the image of an open curve in a numpy array and I

  • 0

I have the image of an open curve in a numpy array and I need to build a list of points coordinates ordered according to their position on the curve.
I wrote a draft script using numpy and mahotas. It may not be optimal.

I know that OpenCV can do this for a closed curve. Can OpenCV do the same (faster) with an open curve?

For example, if the original curve is:

[[0 0 0 0 0 0 0]
 [0 1 0 0 1 0 0]
 [0 0 1 0 0 1 0]
 [0 0 0 1 1 0 0]
 [0 0 0 0 0 0 0]]

Using np.where(myarray==1), I can get the indices of the pixels:

(array([1, 1, 2, 2, 3, 3]), array([1, 4, 2, 5, 3, 4]))

But this not what I need. My script yields the indices taking into account the order of the pixels on the curve:

i= 0 ( 1 , 1 )
i= 1 ( 2 , 2 )
i= 2 ( 3 , 3 )
i= 3 ( 3 , 4 )
i= 4 ( 2 , 5 )
i= 5 ( 1 , 4 )

I would like to optimize my script. Any ideas?

  • 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-05-23T09:18:06+00:00Added an answer on May 23, 2026 at 9:18 am

    Assuming that only one curve is present in the matrix/image and that each point on the curve has between 1 and 2 neighbours, the following function will provide the results you need.

    It works by taking the point closest to the top left hand corner, and forming a chain of points by iteratively finding the closest point that has not already been visited until no further points remain. For a closed curve, the squared Euclidean distance between the first/final points on the chain will be less than than 2.

    import numpy as np
    
    def find_chain(mat):
      locs=np.column_stack(np.nonzero(mat))
      chain=[np.array([0,0])]
      while locs.shape[0]>0:
        dists=((locs-np.vstack([chain[-1]]*locs.shape[0]))**2).sum(axis=1)
        next=dists.argmin()
        if dists.min()<=2 or len(chain)==1:
          chain.append(locs[next,:])
          locs=locs[np.arange(locs.shape[0])!=next,:]
        else:
          chain=[chain[0]]+chain[1::][::-1]
      return np.vstack(chain[1::]),((chain[1]-chain[-1])**2).sum()<=2
    

    For an open curve:

    >>> mat1=np.array([[0, 0, 0, 0, 0, 0, 0],
    ...                [0, 1, 0, 0, 1, 0, 0],
    ...                [0, 0, 1, 0, 0, 1, 0],
    ...                [0, 0, 0, 1, 1, 0, 0],
    ...                [0, 0, 0, 0, 0, 0, 0]])
    >>> points,isclosed=find_chain(mat1)
    >>> points
    array([[1, 1],
           [2, 2],
           [3, 3],
           [3, 4],
           [2, 5],
           [1, 4]])
    >>> isclosed
    False
    

    And for a closed curve:

    >>> mat2=np.array([[0, 0, 0, 0, 0],
    ...                [0, 0, 1, 0, 0],
    ...                [0, 1, 0, 1, 0],
    ...                [0, 1, 0, 1, 0],
    ...                [0, 0, 1, 0, 0],
    ...                [0, 0, 0, 0, 0]])
    >>> points,isclosed=find_chain(mat2)
    >>> points
    array([[1, 2],
           [2, 1],
           [3, 1],
           [4, 2],
           [3, 3],
           [2, 3]])
    >>> isclosed
    True
    

    And a curve where the starting point (closest point to the origin) splits the curve in two.

    >>> mat3=np.array([[0, 0, 0, 0, 0],
    ...                [0, 1, 1, 1, 0],
    ...                [0, 1, 0, 0, 0],
    ...                [0, 1, 0, 0, 0],
    ...                [0, 0, 0, 0, 0],
    ...                [0, 0, 0, 0, 0]])
    >>> points,isclosed=find_chain(mat3)
    >>> points
    array([[1, 3],
           [1, 2],
           [1, 1],
           [2, 1],
           [3, 1]])
    >>> isclosed
    False
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a FAT12 image file and I have to open it and read
I am trying to convert a PIL image into an array using NumPy. I
I want to have an image open from a URL to an intent, most
I have an image in a repeater, I only want to open the image
I need to draw a curve smoothly through N points with N>2 with Javascript.
I have an image so when I tap on the image it should open
I have a iterator which goes through a list and create their name and
i have the menu structure like below, <ul> <li style=background-image:ur('open.gif');> <ul class=sub-menu> ........... </ul>
I have image map where i am trying to open another page by using
I have image on my email and I have to open it through my

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.