Мне нужно создать функцию, которая обрабатывает все пиксели в изображении, и для каждого пикселя использует заливку, чтобы найти «регион» (список точек одного цвета). Если регион больше определенной переменной, регион будет добавлен в список регионов. Однако я продолжаю сталкиваться с проблемой в моей функции findregions (). Я продолжаю получать бесконечное l oop после прохождения первого пикселя.
Я думаю, что ошибка в моем третьем для l oop для массива toVisit, но я не уверен, что это за ошибка.
/**
* 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).
*/