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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:41:04+00:00 2026-06-12T18:41:04+00:00

this is a followup question arising from this solution . The solution to count

  • 0

this is a followup question arising from this solution.
The solution to count adjacent cells works pretty well unless you have multiple patches in the array.

So this time the array for instance looks like this.

import numpy
from scipy import ndimage

s = ndimage.generate_binary_structure(2,2)
a = numpy.zeros((6,6), dtype=numpy.int) # example array
a[1:3, 1:3] = 1;a[2:4,4:5] = 1
print a
[0 0 0 0 0 0]
[0 1 1 0 0 0]
[0 1 1 0 1 0]
[0 0 0 0 1 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]

# Number of nonoverlapping cells
c = ndimage.binary_dilation(a,s).astype(a.dtype)
b = c - a
numpy.sum(b) # returns 19
# However the correct number of non overlapping cells should be 22 (12+10)

Is there any smart solution to solve this dilemma without using any loops or iterating through the array? The reason is that the array could be quite big.

idea 1:

Just thought over it and a way to do it might be to check for more than one patch in the iterating structure. For the total count number to be correct those cells below have to be equal 2 (or more) in the dilation. Anyone got any idea how to turn this thought into code?

[1 1 1 1 0 0]
[1 0 0 2 1 1]
[1 0 0 2 0 1]
[1 1 1 2 0 1]
[0 0 0 1 1 1]
[0 0 0 0 0 0]
  • 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-12T18:41:06+00:00Added an answer on June 12, 2026 at 6:41 pm

    You can use label from ndimage to segment each patch of ones.

    Then you just ask where the returned array equals 1, 2, 3 etc and perform your algoritm on it (or you just use the ndimage.distance_transform_cdt but with inverting your forground/background for each labeled segment.

    Edit 1:

    This code will take your array a and do what you ask:

    b, c = ndimage.label(a)
    e = numpy.zeros(a.shape)
    for i in xrange(c):
    
        e += ndimage.distance_transform_cdt((b == i + 1) == 0) == 1
    
    print e
    

    I realize it is a bit ugly with all the equals there but it outputs:

    In [41]: print e
    [[ 1.  1.  1.  1.  0.  0.]
     [ 1.  0.  0.  2.  1.  1.]
     [ 1.  0.  0.  2.  0.  1.]
     [ 1.  1.  1.  2.  0.  1.]
     [ 0.  0.  0.  1.  1.  1.]
     [ 0.  0.  0.  0.  0.  0.]]
    

    Edit 2 (Alternative solution):

    This code should do the same stuff and hopefully faster (however it will not find the where
    two patches only touch corners).

    b = ndimage.binary_closing(a) - a
    b = ndimage.binary_dilation(b.astype(bool))
    
    c = ndimage.distance_transform_cdt(a == 0) == 1
    
    e = c.astype(numpy.int) * b + c
    
    print e
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a followup/post question to another question; as I have discovered a solution
NOTE: This is a followup to my question here. I have a program that
This is a followup to my last question . I now have a byte[]
Followup question from this one: Swing font names do not match? (Making a font
This is a followup question from this one. Connecting Pyside with matplotlib My PythonFu
This is a followup to this question: How to wait for input from the
I have a followup question to this question . I'm writing a web service
This is followup question from my previous question Toggle button inside accordion using jquery
this is a followup question to Remove default apps from Django-admin I am trying
I have a followup question to the one asked here: Java Abstract Class (this

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.