Reputation: 5233
Ok I've spent hours on this now, so pls forgive me if the solution for this might be very simple. I have a loop which iterates over an image, it starts at a certain pixel, and from this point, it goes several pixels to the left and checks if they fulfill a condition. If I find a point which fulfills, I return it. If I do not find one or run out of the image, I return {-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};
}
My Problem is that I get really strange results. I have always a point of {-23646,286}
(second value is good). I cannot come up with an explanation why this can even happen. I debugged this and saw that the condition was not fulfilled at a certain point (the point that I want to detect), but the function just goes back to the beginning of the for-loop and starts all over instead of returning my {-1,-1}
.
Here is how I call the function as well:
int[] newMarkerBottom = checkLineLeft(markerBottom, intensity);
while (newMarkerBottom[0] != -1) {
markerBottom = newMarkerBottom.clone();
newMarkerBottom = checkLineLeft(markerBottom, intensity);
}
EDIT
I checked again, and there is no exception that gets caught when the inner part of the if
condition is false. Debugger just jumps back to the line for(...)
and keeps going.
EDIT 2
I am running this on an Android Application. However, I think this cannot be part of the problem here.
EDIT3
This might help: When I set a breakpoint to return return new int[]{point[0] - i, point[1]};
, it will stop there, then in the next step it will jump to the last return new int[]{-1, -1};
and will never reach the breakpoint again.
Upvotes: 3
Views: 133
Reputation: 1100
I'm not sure of reasoning of strange behavior of your source (there are many possibilities).
According to your sources it looks like final sources should look like: additional class:
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;
}
}
checker method:
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 some updates to make source match clearer and improve readability.
Upvotes: 2