循环的奇怪行为

时间:2015-12-15 22:37:51

标签: java android

好的我现在花了好几个小时,所以请原谅我,如果解决方案可能非常简单。我有一个迭代图像的循环,它从某个像素开始,从这一点开始,它向左移动几个像素并检查它们是否满足条件。如果我找到一个满足的点,我就把它归还。如果我没有找到图像或用完图像,我会返回{-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,-1}

修改

我再次检查过,当int[] newMarkerBottom = checkLineLeft(markerBottom, intensity); while (newMarkerBottom[0] != -1) { markerBottom = newMarkerBottom.clone(); newMarkerBottom = checkLineLeft(markerBottom, intensity); } 条件的内部部分为false时,没有异常被捕获。调试器只是跳回到if行并继续前进。

编辑2

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

EDIT3

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

1 个答案:

答案 0 :(得分:2)

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

根据您的消息来源,看起来最终来源应该如下所示: 附加课程:

+

检查方法:

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;
   }
}

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