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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:48:10+00:00 2026-06-12T00:48:10+00:00

I am standing in front of a huge problem. Using the python libraries NumPy

  • 0

I am standing in front of a huge problem. Using the python libraries NumPy and SciPy, I identified several features in large array. For this purpose, I created a 3×3 neighbor structure and used it for a connected component analysis –> see docs.

struct = scipy.ndimage.generate_binary_structure(2,2)
labeled_array, num_features = ndimage.label(array,struct)

My problem now is that I want to iterate through all identified features in a loop. Someone has an idea how to address individual features in the resulting NumPy array?

  • 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-12T00:48:11+00:00Added an answer on June 12, 2026 at 12:48 am

    Here’s an example of handling features identified by ndimage.label. Whether this helps you or not depends on what you want to do with the features.

    import numpy as np
    import scipy.ndimage as ndi
    import matplotlib.pyplot as plt
    
    
    # Make a small array for the demonstration.
    # The ndimage.label() function treats 0 as the "background".
    a = np.zeros((16, 16), dtype=int)
    a[:6, :8] = 1
    a[9:, :5] = 1
    a[8:, 13:] = 2
    a[5:13, 6:12] = 3
    
    struct = ndi.generate_binary_structure(2, 2)
    lbl, n = ndi.label(a, struct)
    
    # Plot the original array.
    plt.figure(figsize=(11, 4))
    plt.subplot(1, n + 1, 1)
    plt.imshow(a, interpolation='nearest')
    plt.title("Original")
    plt.axis('off')
    
    # Plot the isolated features found by label().
    for i in range(1, n + 1):
        # Make an array of zeros the same shape as `a`.
        feature = np.zeros_like(a, dtype=int)
    
        # Set the elements that are part of feature i to 1.
        # Feature i consists of elements in `lbl` where the value is i.
        # This statement uses numpy's "fancy indexing" to set the corresponding
        # elements of `feature` to 1.
        feature[lbl == i] = 1
    
        # Make an image plot of the feature.
        plt.subplot(1, n + 1, i + 1)
        plt.imshow(feature, interpolation='nearest', cmap=plt.cm.copper)
        plt.title("Feature {:d}".format(i))
        plt.axis('off')
    
    plt.show()
    

    Here’s the image generated by the script:

    enter image description here

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

Sidebar

Related Questions

I have a long standing problem: at work we're using mercurial as a DSCM,
This has been a fairly long-standing problem for us with our Hudson installation, and
Using Xcode. In this code (func is declared in interface), tells subj error, standing
Is there any solution for this problem? When using rounded-corner in IE9 with background
I love auto-implemented properties in C# but lately there's been this elephant standing in
Here is my problem. I have a chipmunk's rigid body (a soda can standing
Imagine I'm standing in a large room that has a router in the corner.
I've several long-standing apps written in Delphi that persist their settings in the registry.
This is a long standing source of frustration, but maybe there is something I'm
We have a long standing bug in our production code. This is essentially a

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.