gpt4 book ai didi

java - 循环的奇怪行为

转载 作者:太空狗 更新时间:2023-10-29 13:16:22 26 4
gpt4 key购买 nike

好的,我现在已经在这上面花了几个小时,所以如果这个解决方案可能非常简单,请原谅我。我有一个遍历图像的循环,它从某个像素开始,从这一点开始,它向左移动几个像素并检查它们是否满足条件。如果我找到一个满足的点,我会返回它。如果我没有找到或用完图像,我将返回 {-1,-1}

private static int[] checkLineLeft(int[] point, Mat intensity) {
for (int i = 1; i < intensity.width()*0.2f; i += 1) {
try {
if (intensity.get(point[1], point[0] - i)[0] > 100
&& intensity.get(point[1], point[0] - i)[2] < 50) {
return new int[]{point[0] - i, point[1]};
} else {
continue;
}
} catch (Exception e) {
return new int[]{-1, -1};
}
}
return new int[]{-1, -1};
}

我的问题是我得到了非常奇怪的结果。我一直点{-23646,286}(第二个值很好)。我无法解释为什么会发生这种情况。我对此进行了调试,发现条件在某个点(我想要检测的点)未满足,但该函数只是返回到 for 循环的开头并重新开始,而不是返回我的 {-1,-1}.

我也是这样调用函数的:

int[] newMarkerBottom = checkLineLeft(markerBottom, intensity);
while (newMarkerBottom[0] != -1) {
markerBottom = newMarkerBottom.clone();
newMarkerBottom = checkLineLeft(markerBottom, intensity);
}

编辑

我再次检查,当if 条件的内部部分为假时,没有捕获异常。调试器只是跳回 for(...) 行并继续。

编辑 2

我在 Android 应用程序上运行它。但是,我认为这不是问题的一部分。

EDIT3

这可能会有所帮助:当我将断点设置为 return return new int[]{point[0] - i, point[1]}; 时,它将停在那里,然后在下一个它将跳转到最后一步 return new int[]{-1, -1}; 并且永远不会再次到达断点。

最佳答案

我不确定你来源的奇怪行为的推理(有很多可能性)。

根据您的来源,最终来源看起来应该是这样的:附加类:

public class Point {
private final int x;
private final int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}

检查方法:

private static Point checkLineLeft(Point point, Mat intensive) {
int minX = point.getX() - intensive.width()*0.2;
int y = point.getY();
for (int x = point.getX() - 1 ; x > minX && x >= 0 ; x--) {
if (isCorrectPoint(intensive, x, y)) {
return new Point(x, y);
}
}
return new Point(-1, -1);
}

private static boolean isCorrectPoint(Mat intensive, int x, int y) {
return intensity.get(y, x)[0] > 100
&& intensity.get(y, x)[2] < 50;
}

PS一些更新,使源匹配更清晰,提高可读性。

关于java - 循环的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34300791/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com