Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by EricDaily · Sep 05, 2014 at 05:17 AM · detectionpixelsbytearrayregionflood

Need Help Detecting Islands/Regions of Image Using Floodfill

(I moved this from the Forums, I think it's supposed to go here).

Hi All,

Using the alpha channel of the pixels of an image, I have created a 2-dimensional byte array where each byte is designated as a 0 or a 1 according to whether it's alpha is 0 (invisible) or not (!=0). The 1s are clustered in groups were the non-transparent parts of the image are, but these parts are not necessarily all connected to one another. What I'm trying to do is detect how many unique clusters (or islands) there are. This is sometimes known as blob extraction or connected component labeling. I'm currently going through all the bytes in the 2d array and when I find a 1, I start floodfilling.

My trouble is, when do I increment the regionCounter so that the next pass will skip the cluster I just labelled in the first pass. I figure the regionCounter should increment when a region is done being labelled, but how do I know when a region is done being labelled? In other words, where should I put the "regionCounter++"?

Note: I'm starting the regionCounter at 2 because I'm trying to turn the first batch of 1s into 2s, then the next into 3s through the increment step.

Any hints?

 public byte[,] map;
 public int regionCounter = 2;
  
 public void FindRegions () {
             for (int y = 1; y < this.map.GetLength(1)-1; y++) {
                 for (int x = 1; x < this.map.GetLength(0)-1; x++) {
                     FloodFill(x,y,1,(byte)regionCounter);
                 }
             }  
     }
  
  
     public void FloodFill(int x, int y, byte targetNumber, byte replacementNumber) {
         //if(x>0 && x < map.GetLength(0)-1 && y > 0 && y < map.GetLength(1)-1){ return; } //if you're within the array
         if(map[x,y] == replacementNumber) { return; } //if targetNum = replacementNum, return
         if(map[x,y] != targetNumber) { return; } //if number of Node != targetNum, return because you're not what we're looking for
         //if you've got this far...
         map[x,y] = replacementNumber; //set your current node to replacementNum
         //run function on it's neighbors...this is the floodfilling in action
         //WEST
         FloodFill(x-1,y, targetNumber, replacementNumber);
         //EAST
         FloodFill(x+1,y, targetNumber, replacementNumber);
         //NORTH
         FloodFill(x,y+1, targetNumber, replacementNumber);
         //SOUTH
         FloodFill(x,y-1, targetNumber, replacementNumber);
  
         return;
     }

Any help you could off would be fantastic.

Thanks so much! - Eric

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Area sensing - Needs fixing 1 Answer

RigidBody Collision Detection 1 Answer

How to perform some code if you are close to an object 2 Answers

Screen orientation change notification 1 Answer

Regarding the efficiency of enemy detection 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges