I'm new for image processing, I'm tring to find a contour of an image without using external libraries.
public static List<Contour> findContours(Bitmap edgeImage) {
List<Contour> contours = new ArrayList<>();
int width = edgeImage.getWidth();
int height = edgeImage.getHeight();
boolean[][] visited = new boolean[width][height];
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
if (!visited[x][y] && isEdgePixel(edgeImage, x, y)) {
Contour contour = traceContour(edgeImage, visited, x, y);
contours.add(contour);
}
}
}
return contours;
}
private static Contour traceContour(Bitmap edgeImage, boolean[][] visited, int startX, int startY) {
List<Point> contourPoints = new ArrayList<>();
// Depth-first search to trace contour
dfs(edgeImage, visited, startX, startY, contourPoints);
return new Contour(contourPoints);
}
private static void dfs(Bitmap edgeImage, boolean[][] visited, int x, int y, List<Point> contourPoints) {
int width = edgeImage.getWidth();
int height = edgeImage.getHeight();
if (x < 0 || x >= width || y < 0 || y >= height || visited[x][y] || !isEdgePixel(edgeImage, x, y)) {
return;
}
visited[x][y] = true;
contourPoints.add(new Point(x, y));
// Visit neighbors
dfs(edgeImage, visited, x + 1, y, contourPoints);
dfs(edgeImage, visited, x - 1, y, contourPoints);
dfs(edgeImage, visited, x, y + 1, contourPoints);
dfs(edgeImage, visited, x, y - 1, contourPoints);
}
private static boolean isEdgePixel(Bitmap edgeImage, int x, int y) {
// Customize this method based on how edges are represented in your edge image
// For simplicity, assuming black pixels represent edges
int pixelValue = edgeImage.getPixel(x, y);
return Color.red(pixelValue) == 0; // Assuming black edges
}
I use the above code for finding a contour but the code is crashed after calling a function dfs
I don't know the reason.
please any one help me for resolove this issue