Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用泛洪填充算法创建阵列_Java_Recursion_Flood Fill - Fatal编程技术网

Java 使用泛洪填充算法创建阵列

Java 使用泛洪填充算法创建阵列,java,recursion,flood-fill,Java,Recursion,Flood Fill,我正在使用一个泛洪填充算法对图像进行排序。如果它遇到相同的颜色,我希望它将该像素复制到一个大小相同的数组中,称为filled。然后将填充的数组转换回图像并保存为jpg。然而,当我打开jpg时,它看起来完全是黑色的 public static void findFace(int[][] image) throws IOException { int height = image.length; int width = image[0].length; Color cent

我正在使用一个泛洪填充算法对图像进行排序。如果它遇到相同的颜色,我希望它将该像素复制到一个大小相同的数组中,称为filled。然后将填充的数组转换回图像并保存为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;
}
publicstaticvoidfindface(int[]image)引发IOException{
int height=image.length;
int width=图像[0]。长度;
颜色中心开始=新颜色(图像[宽度/2][高度/2]);
int[][]填充=新int[宽度][高度];
泛光填充(宽度/2,高度/2,中心开始,图像,填充);
//将填充数组构造为图像。显示是否找到面。
BuffereImage bufferImage2=新的BuffereImage(宽度、高度、BuffereImage.TYPE_INT_RGB);
对于(int y=0;yint Pixel=filled[x][y]您发布的
floodFill
函数缺少两个重要元素:

  • 如果包含与第一个像素相同颜色的区域一直延伸到图像的边界,该函数将尝试以无效索引访问
    图像
    。您可以通过首先检查正在检查的像素的x和y坐标来解决此问题,如果超出边界,则立即返回
  • 如果有同一颜色的多个相邻像素,则该函数将导致无限递归,因为初始调用将调用第二像素上的“代码> FooDebug < /代码>,然后在第一个像素上进行调用<代码> FooDebug < /代码>,等等。单个像素一次

  • 由于您没有观察到这两种症状中的任何一种,并且您也没有从结果图像中观察到任何东西,因此我猜初始像素的颜色检查不正确。当您将整数传递给
    color
    构造函数时,您确定它使用了该整数的RBG解释吗?

    floodFill
    函数您发布的内容缺少两个重要元素:

  • 如果包含与第一个像素相同颜色的区域一直延伸到图像的边界,该函数将尝试以无效索引访问
    图像
    。您可以通过首先检查正在检查的像素的x和y坐标来解决此问题,如果超出边界,则立即返回
  • 如果有同一颜色的多个相邻像素,则该函数将导致无限递归,因为初始调用将调用第二像素上的“代码> FooDebug < /代码>,然后在第一个像素上进行调用<代码> FooDebug < /代码>,等等。单个像素一次

  • 由于您没有观察到这两种症状中的任何一种,并且您也没有从结果图像中观察到任何东西,因此我猜初始像素的颜色检查是不正确的。当您将整数传递给
    color
    构造函数时,您确定它使用该整数的RBG解释吗?

    位移位来创建
    像素似乎有点奇怪…似乎您还没有决定是否使用3字节RGB或int RGB样本。也很难知道什么是
    int[][]图像
    确实如此。尝试发布一个完全可运行但精简的代码版本。此外,您是否多次覆盖同一像素?递归似乎有点失控。您还需要边界检查。创建
    像素
    的位移动似乎有点奇怪……如果您有e 3字节RGB或int RGB样本。也很难知道什么是
    int[]image
    确实如此。尝试发布一个完全可运行但精简的代码版本。此外,您是否多次覆盖同一像素?递归似乎有点失控。您还需要边界检查。我建议对递归进行调整,结果仍然是黑色jpg。我将进一步研究如何使用他正确地使用了颜色构造函数。你是对的,这可能是问题的根源。谢谢你的帮助。我建议对递归进行调整,这仍然会导致黑色jpg。我将进一步研究如何正确使用颜色构造函数。你是对的,这可能是问题的根源。谢谢你的帮助。