홍수 채우기 알고리즘을 사용하여 이미지를 정렬했습니다. 동일한 색상을 발견하면 해당 픽셀을 채워진 동일한 크기의 배열로 복사해야합니다. 채워진 배열은 이미지로 다시 변환되어 jpg로 저장됩니다. 그러나 jpg를 열면 완전히 검은 색으로 보입니다.홍수 채우기 알고리즘을 사용하여 배열 만들기
public static void findFace(int[][] image) throws IOException {
int height = image.length;
int width = image[0].length;
Color centerStart = new Color(image[width/2][height/2]);
int[][] filled = new int[width][height];
floodFill(width/2, height/2, centerStart, image, filled);
//construct the filled array as image. Show if the face was found.
BufferedImage bufferImage2 = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int Pixel = filled[x][y] << 16 | filled[x][y] << 8 | filled[x][y];
bufferImage2.setRGB(x, y, Pixel);
}
}
//save filled array as image file
File outputfile = new File("/home/lily/Pictures/APicaDay/saved.jpg");
ImageIO.write(bufferImage2, "jpg", outputfile);
}
public static int[][] floodFill(int x, int y, Color targetColor, int[][] image, int[][] filled) {
if (image[x][y] != targetColor.getRGB()) {
return filled;
}
filled[x][y] = image[x][y];
floodFill(x - 1, y, targetColor, image, filled);
floodFill(x + 1, y, targetColor, image, filled);
floodFill(x, y - 1, targetColor, image, filled);
floodFill(x, y + 1, targetColor, image, filled);
return filled;
}
보너스 질문 : 사진 다루고있어 이후가 아니라 동일한도 비슷 색상을 받아들이는 홍수 채우기를하고 싶습니다.
'Pixel'을 만드는 비트 쉬프트는 약간 이상합니다. 3 바이트 RGB 또는 int RGB 샘플이 있다면 마음을 편히 들여 놓은 것처럼 보입니다. 'int [] [image]가 실제로 무엇인지를 아는 것도 어렵습니다. 완전히 실행 가능하지만 코드의 버전을 삭제 해보십시오. – haraldK
또한 동일한 픽셀을 여러 번 다루지 않습니까? 재귀는 약간 통제를 벗어난 것처럼 보입니다. 경계 검사가 필요합니다. – haraldK