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 442043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:01:57+00:00 2026-05-12T21:01:57+00:00

I want to calculate a convex hull around a shape in a binary NxM

  • 0

I want to calculate a convex hull around a shape in a binary NxM matrix. The convex hull algorithm expects a list of coordinates, so I take numpy.argwhere(im) to have all shape point coordinates. However, most of those points are not contributing to the convex hull (they lie on the inside of the shape). Because convex hull computation time is at least proportional to the number of points that it gets as input, I devised an idea to filter the plethora of useless points on beforehand and only pass those that span the outline. The idea is quite simple, that for each row in the binary NxM matrix I take only the minimal and maximal indices. So for example:

im = np.array([[1,1,1,0],
              [1,0,1,1],
              [1,1,0,1],
              [0,0,0,0],
              [0,1,1,1]], dtype=np.bool)
outline = somefunc(im)

Then outline should read (in tuples or as a 5×2 numpy array, I don’t mind):

[(0,0),(0,2),(1,0),(1,3),(2,0),(2,3),(4,1),(4,3)]

Any convex hull tight around this shape (im), must a subset of these points (outline). In other words, if “somefunc()” is efficient in filtering the inside points then it saves time for the convex hull computation.

I have code that does the above trick, but I am hoping someone has a more clever (read faster) approach since I need to run it many many times. The code I have is:

# I have a 2D binary field. random for the purpose of demonstration.
import numpy as np
im = np.random.random((320,360)) > 0.9

# This is my algorithm so far. Notice that coords is sorted.
coords = np.argwhere(im)
left = np.roll(coords[:,0], 1, axis=0) != coords[:,0]
outline = np.vstack([coords[left], coords[left[1:]], coords[-1]])

Another idea I had was to use Python’s reduce() so I’d need to run over the list of coords only once. But I have difficulty finding a good reducing function.

Any help would greatly be appreciated!

edit

In the meanwhile I have found a faster way to go from im directly to outline. At least with large images this is significantly faster. In the apparent absence of an external solution I am positing it as the solution to this question.

Still, if somebody knows an even faster method, please speak up 🙂

  • 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-12T21:01:57+00:00Added an answer on May 12, 2026 at 9:01 pm

    In the absence of an acceptable answer I post my best working code as the solution.

    def outline(im):
        ''' Input binary 2D (NxM) image. Ouput array (2xK) of K (y,x) coordinates
            where 0 <= K <= 2*M.
        '''
        topbottom = np.empty((1,2*im.shape[1]), dtype=np.uint16)
        topbottom[0,0:im.shape[1]] = np.argmax(im, axis=0)
        topbottom[0,im.shape[1]:] = (im.shape[0]-1)-np.argmax(np.flipud(im), axis=0)
        mask      = np.tile(np.any(im, axis=0), (2,))
        xvalues   = np.tile(np.arange(im.shape[1]), (1,2))
        return np.vstack([topbottom,xvalues])[:,mask].T
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to calculate covariance matrix using Java. Is there any free library to
I want to calculate the RSA algorithm by myself . I need to calculate
I want to calculate Big O of x++ in below algorithm. for (int i
I'm using OpenCV 2.4 to calculate the convex hull of a image. I'm also
I want to calculate the avarage timedelta between dates in a list. Although the
I want to calculate the age in view class without changing the model class
i want to calculate latest 31-Mar .... suppose date is 1-Jan-2012 i want result
I want to calculate the following integral with Mathematica Integrate[(E^(2 x (-1 - Sqrt[y]))
I want to calculate a bonus based on the two consecutive months where sales
I want to calculate users with accumulating by dates. I have the following map

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.