Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用泛光填充在图像上查找颜色相似的区域_Java_Image Processing_Arraylist_Pixel_Flood Fill - Fatal编程技术网

Java 使用泛光填充在图像上查找颜色相似的区域

Java 使用泛光填充在图像上查找颜色相似的区域,java,image-processing,arraylist,pixel,flood-fill,Java,Image Processing,Arraylist,Pixel,Flood Fill,我需要创建一个函数来处理图像中的所有像素,并为每个像素使用泛光填充来查找“区域”(具有相同颜色的点列表)。如果区域大于某个变量,则该区域将添加到区域列表中。但是,我在findregions()函数中不断遇到问题。经过第一个像素后,我不断得到一个无限循环 我认为这个错误在ToVisite arraylist的第三个for循环中,但我不确定错误是什么 /**

我需要创建一个函数来处理图像中的所有像素,并为每个像素使用泛光填充来查找“区域”(具有相同颜色的点列表)。如果区域大于某个变量,则该区域将添加到区域列表中。但是,我在findregions()函数中不断遇到问题。经过第一个像素后,我不断得到一个无限循环

我认为这个错误在ToVisite arraylist的第三个for循环中,但我不确定错误是什么

    /**                                                                                                                                                                           
     * 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).                                            
     */                                                                                                                                                                           
/**
*将区域设置为图像中的整体填充区域,与trackColor非常相似。
*/                                                                                                                                                                           
公共无效findRegions(颜色目标颜色){
//TODO:这里是您的代码
对于(int y=0;y