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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:24:48+00:00 2026-06-03T01:24:48+00:00

I’ve got a 640×480 binary image (0s and 255s). There is a single white

  • 0

I’ve got a 640×480 binary image (0s and 255s). There is a single white blob in the image (nearly circular) and I want to find the centroid of the blob (it’s always convex). Essentially, what we’re dealing with is a 2D boolean matrix. I’d like the runtime to be linear or better if possible – is this possible?

Two lines of thought so far:

  1. Make use of the numpy.where() function
  2. Sum the values in each column and row, then find where the max value is based on those numbers… but is there a quick and efficient way to do this? This might just be a case of me being relatively new to python.
  • 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-03T01:24:49+00:00Added an answer on June 3, 2026 at 1:24 am

    This code will find the centroid of any shaped image. It will find it exactly for rez = 1. Increasing rez, increases the grid spacing, so can massively increase the speed of the search, at the obvious cost of the accuracy. If the size of the blob is known within bounds you could chain a low rez search, with a high rez search, thus finding the answer quickly and pricesely

    import Image
    
    def find_centroid_faster(im, rez):
        width, height = im.size
        XX, YY, count = 0, 0, 0
        for x in xrange(0, width, rez):
            for y in xrange(0, height, rez):
                if im.getpixel((x, y)) == 255:
                    XX += x
                    YY += y
                    count += 1
        return XX/count, YY/count
    

    for example, with the below image:

    im = Image.open('blob.png')
    print find_centroid(im, 1)
    print find_centroid(im, 20)
    #output:
    (432, 191)
    (430, 190)
    

    Timing with timeit the first option (which is linear time, O(n)) has a run time of 1.7s, and the second 0.005s.

    You can’t do better than O(n) to find an exact answer, unless you have some constraints on size and shape. But, you can sacrifice accuracy for speed. The above code is O(n/(rez ** 2)), which can be a massive improvement. The accuracy of reported results is: ± rez / 2, in each dimension.

    Update:

    sega_sai wrote a nice piece of numpy code (see post below) to find the centroid. I’ve modified it to take advantage of grid spacing, by using slicing. It operates in the same fashion as above:

    def find_centroid_faster_numpy(im,rez):
            h, w = im.size
            arr = np.array(im)
            arr_rez = arr[::rez,::rez]
            ygrid, xgrid  = np.mgrid[0:w:rez, 0:h:rez]
            xcen, ycen = xgrid[arr_rez == 255].mean(), ygrid[arr_rez == 255].mean()
            return xcen, ycen
    

    Here are graphed timeit results for these two functions over a span of rez values:

    enter image description here

    Its a log graph, so it really illustrates the advantage of combining the two approaches.

    This is the image I used for the tests:

    enter image description here

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.