java - 递归不打印错误但卡住了
问题描述
我的代码似乎卡在第一次递归中。我的 IDE 也不会打印出任何 StackOverflow 错误或其他错误。
该算法的目的是找到图像的所有彩色区域。
任何有关解决此问题的帮助将不胜感激。
PS:pixels[] 是任何“PImage”的 rgb 值数组。
public class Test {
PImage finalImage;
public Test(PImage pImage) {
finalImage = pImage.get(0, 0, pImage.width, pImage.height);
}
public void floodFill() {
int targetColor;
for (int x = 0; x < finalImage.width; x+=3) {
for (int y = 0; y < finalImage.height; y+=3) {
targetColor = finalImage.pixels[index(x, y, finalImage)];
flood4(x, y, targetColor, color(69, 69, 69));
}
}
}
public void flood4(int x, int y, int target, int replacement) {
finalImage.loadPixels();
int currentColor = finalImage.pixels[index(x, y, finalImage)];
if (currentColor != target) {
return;
}
if (currentColor == replacement) {
return;
}
if (!inBounds(x, y, finalImage)) {
return;
}
finalImage.pixels[index(x, y, finalImage)] = replacement;
finalImage.updatePixels();
flood4(x+1, y, target, replacement);
flood4(x-1, y, target, replacement);
flood4(x, y+1, target, replacement);
flood4(x, y-1, target, replacement);
}
PImage getImage() {
return finalImage.get(0, 0, finalImage.width, finalImage.height);
}
}
boolean inBounds(int x, int y, PImage img) {
return x>-1&&x<img.width&&y>-1&&y<img.height;
}
解决方案
推荐阅读
- c++ - 覆盖和净化都抱怨,但我不知道:如何使用字符串流?
- azure - 在 Azure Functions for Javascript 中 serviceBusTrigger 未注册错误
- java - 使用 Jdf 和 Intellij:专门在 intellij 中运行 Jfd 表单
- python - 当变量超出范围时,Python 不会释放内存
- hystrix - Hystrix:不能跳闸断路器
- python - Python webscraping 时,beautifulSoup 与 chrome 检查不匹配
- wix - 如何制作 64 位 MSI?
- scala - 如果你数两次会发生什么?
- c# - '在预期条件的上下文中指定的非布尔类型的表达式,靠近','。' 当日期之间
- php - 将多维数组转换为字符串