好的,我现在已经花了几个小时,因此,如果解决方案可能非常简单,请原谅我。我有一个循环遍历图像的循环,它从某个像素开始,从这一点开始,它向左移动几个像素,并检查它们是否满足条件。如果找到满足的要点,我将其退还。如果找不到图片或图片用完,我将返回{-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
条件的内部为false时,没有异常被捕获。调试器仅跳回到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进行了一些更新,以使源匹配更加清晰并提高可读性。