Note that there are some explanatory texts on larger screens.

plurals
  1. POFlood Fill Algorithm Resulting in Black Image
    primarykey
    data
    text
    <p>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?</p> <pre><code>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 &lt; height; i++) for (int j = 0; j &lt; 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 &lt; height; x++) { for (int y = 0; y &lt; 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] &lt; targetColor.getRGB()/2 || image[x][y] &gt; targetColor.getRGB()*2) return filled; if (image[x][y] == Color.blue.getRGB()) { return filled; } if (image.length &lt; 0 || image[0].length &lt; 0 || image.length &gt;= height || image[0].length &gt;= 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; } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload