洪水填充算法导致黑色图像 [英] Flood Fill Algorithm Resulting in Black Image

查看:213
本文介绍了洪水填充算法导致黑色图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个洪水填充算法,希望最终,基于正确的中心的颜色和周围的类似颜色在照片的中心找到一个面孔。然而,目前,我的算法应该接受在int数组的边界内的任何颜色,并将其传递到持有者数组,基本上是原始图像的副本。但这不工作,并导致一个黑色的图像,当我运行它。谁能看到我错过的问题?

I'm constructing a flood fill algorithm that will hopefully, eventually, find a face in the center of a photograph based on the color at the exact center, and similar colors surrounding that. At the moment, however, my algorithm should take in any color within the bounds of the int array and transfer it over to a holder array, essentially making a copy of the original image. But this isn't working, and is resulting in a black image when I run it. Can anyone see the problem I'm missing?

public class TemplateMaker {

public static void main(String[] args) throws IOException {
    importPhoto();
}

public static void importPhoto() throws IOException {
    File imgPath = new File("/Pictures/BaseImage.JPG");
    BufferedImage bufferedImage = ImageIO.read(imgPath);
    establishArray(bufferedImage);
}

public static void establishArray(BufferedImage bufferedImage) throws IOException {
    //byte[] pixels = hugeImage.getData();
    int width = bufferedImage.getWidth();
    System.out.println(width);
    int height = bufferedImage.getHeight();
    System.out.println(height);
    int[][] result = new int[height][width];
    for (int i = 0; i < height; i++)
        for (int j = 0; j < width; j++) {
            result[i][j] = bufferedImage.getRGB(j, i);
        }
    findFace(result);
}

public static void findFace(int[][] image) throws IOException {
    int height = image.length;
    int width = image[0].length;
    Color centerStart = new Color(image[height / 2][width / 2], true);
    System.out.println(centerStart.getRGB());
    System.out.println(Color.blue.getRGB());

    int[][] filled = new int[height][width];

    floodFill(height / 2, width / 2, centerStart, image, filled, height, width);

    //construct the filled array as image.
    BufferedImage bufferImage2 = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    for (int x = 0; x < height; x++) {
        for (int y = 0; y < width; y++) {
            bufferImage2.setRGB(y, x, filled[x][y]);
        }
    }
    //save filled array as image file
    File outputfile = new File("/Pictures/saved.jpg");
    ImageIO.write(bufferImage2, "jpg", outputfile);
}

public static int[][] floodFill(int x, int y, Color targetColor, int[][] image, int[][] filled, int height, int width) {

    //execute something similar once algorithm works. 
    // if (image[x][y] < targetColor.getRGB()/2 || image[x][y] > targetColor.getRGB()*2) return filled;

    if (image[x][y] == Color.blue.getRGB()) {
        return filled;
    }
    if (image.length < 0 || image[0].length < 0 || image.length >= height || image[0].length >= width) {
        return filled;
    }
    filled[x][y] = image[x][y];
    image[x][y] = Color.blue.getRGB();

    floodFill(x - 1, y, targetColor, image, filled, height, width);
    floodFill(x + 1, y, targetColor, image, filled, height, width);
    floodFill(x, y - 1, targetColor, image, filled, height, width);
    floodFill(x, y + 1, targetColor, image, filled, height, width);
    return filled;
}

}

推荐答案

您创建的int [] []调用填充,然后调用floodFill(...),不返回任何东西到数组。 image.length总是等于height和image [0] .length总是等于width,所以它总是从第二个if语句返回。

You create int[][] called filled and then call floodFill(...) which returns without doing anything to the array. image.length is always equal to height and image[0].length is always equal to width, so it always returns from the second if statement.

BufferedImage从该空白数组并将其写入文件。

You then build a BufferedImage from that blank array and write it to a file. All of the values in the array are initialized to 0, which gives you black.

将findFace(..)中的for循环更改为下面将会保存原始的

Changing the for loop in findFace(..) to the below will save out the original image from your holder array.

        for (int x = 0; x < height; x++) {
        for (int y = 0; y < width; y++) {
            bufferImage2.setRGB(y, x, image[x][y]);
        }
    }

但我不知道这是什么

编辑:尝试此操作,看看它是否以正确的方向发送了您:

Try this out and see if it sends you in the right direction:

    public static int[][] floodFill(int x, int y, Color targetColor, int[][] image, int[][] filled, int height, int width) {

    //execute something similar once algorithm works. 
    // if (image[x][y] < targetColor.getRGB()/2 || image[x][y] > targetColor.getRGB()*2) return filled;

    if (image[x][y] == Color.blue.getRGB()) {
        System.out.println("returned if 1");
        return filled;
    }
    /*if (image.length < 0 || image[0].length < 0 || image.length >= height || image[0].length >= width) {
        return filled;
    }*/
    filled[x][y] = image[x][y];
    image[x][y] = Color.blue.getRGB();

    if (x - 1 <= 0 && y < width) {
        floodFill(x - 1, y, targetColor, image, filled, height, width);
    }

    if(x + 1 < height && y >= 0 && y < width) {
        floodFill(x + 1, y, targetColor, image, filled, height, width);
    }

    if(x >= 0 && x < height && y - 1 <= 0) {
        floodFill(x, y - 1, targetColor, image, filled, height, width);
    }

    if(x >= 0 && x < height && y + 1 < width) {
        floodFill(x, y + 1, targetColor, image, filled, height, width);
    }

    return filled;
}

这篇关于洪水填充算法导致黑色图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆