0

I need to create a function that processes all the pixels in an image, and for each pixel uses flood fill to find a "region" (a list of points with the same color). If the region is larger than a certain variable then the region will be added to a list of regions. However I keep encountering a problem within my findregions() function. I keep getting an infinite loop after I pass the first pixel.

I think the error is within my third for loop for the toVisit arraylist but im not sure what the error is.

    /**                                                                                                                                                                           
     * Sets regions to the flood-fill regions in the image, similar enough to the trackColor.                                                                                     
     */                                                                                                                                                                           
    public void findRegions(Color targetColor) {                                                                                                                                  
        // TODO: YOUR CODE HERE                                                                                                                                                   

        for (int y= 0; y < image.getHeight(); y++){         // Loop over all the pixels                                                                                           
            for (int x = 0; x < image.getWidth(); x++){                                                                                                                           
                Color c = new Color(image.getRGB(x, y));                                                                                                                          
                if (visited.getRGB(x, y) == 0 && colorMatch(c, targetColor )){  //Checks if pixel is unvisited and of the same color                                              

                        //start a new region                                                                                                                                      
                        Point point = new Point(x, y);                                                                                                                            
                        ArrayList<Point> region = new ArrayList<>();        // starts a new region                                                                                

                        ArrayList<Point> toVisit = new ArrayList<>();   // Keeps track of what pixels need to be visited                                                          

                        toVisit.add(point);                             // Initially you just need to visit current point                                                                                                                                                           
                        for (int i = 0; i < toVisit.size(); i++){  //As  long as there's a pixel that needs to be visited                                                         
                            region.add(toVisit.get(i));                         //add it to the region                                                                                                                                                                    
                            Point current = toVisit.get(i);                                                                                                                       
                            int cx = (int)current.getX();                                                                                                                         
                            int cy = (int)current.getY();                                                                                                                         

                            //Loop over all current pixels eight neighbors                                                                                                        
                            for (int ny = Math.max(0, cy-1); ny < Math.min(image.getHeight(), cy+1); ny++) {                                                                      
                                for (int nx = Math.max(0, cx-1); nx < Math.min(image.getWidth(), cx+1); nx++) {                                                                   
                                    Color cn = new Color(image.getRGB(nx, ny));                                                                                                   
                                    Point new_point = new Point(nx, ny);                                                                                                          
                                    //if neighbor is the correct color                                                                                                            
                                    if (colorMatch(cn, targetColor)) {                                                                                                            

                                        toVisit.add(new_point);           //add it to the list of pixels to be visited                                                                           
                                    }                                                                                                                                             
                                }                                                                                                                                                 
                            }                                                                                                                                                                                                                                                                   
                            visited.setRGB(x, y, 1);                                                                                                                              
                        }                                                                                                                                                         
                        toVisit.remove(point);                                                                                                                                    

                        if (region.size() >= minRegion){    //if region is big enough, we add it to the regions ArrayList                                                         
                            regions.add(region);                                                                                                                                                                                                                                                          
                        }                                                                                                                                                         
                }                                                                                                                                                                 
            }                                                                                                                                                                     
        }                                                                                                                                                                         
    }                                                                                                                                                                             

    /**                                                                                                                                                                           
     * Tests whether the two colors are "similar enough" (your definition, subject to the maxColorDiff threshold, which you can vary).                                            
     */                                                                                                                                                                           
Jose
  • 11

1 Answers1

0

For loops are LIMITED they don't loop indefinitely unless you made it so, And in your code everything is fine, Except that you have 4 for loops which is quite tasking computationally.

imagine looping through a 1000 x 1000 img:
You can get some idea through the equation from here.

I would recommend trying on a small image to make sure the code works or to find an alternative solution other than the nested loops.

Makdous
  • 1,447
  • 1
  • 12
  • 24