Wednesday, 15 February 2012

java - Using a Flood Fill Algorith to create an Array -


I am using flooded algorithm to sort through an image. If it encounters the same color, then I want to copy the pixel into an array of equal size that is said to be filled. The filled array is again turned into an image and is saved as a JPG. However, when I open the JPG, it is completely black. Find Face (int [] [] image) throws IOException {int height = image.length; Int width = image [0]. Length; Color center: start = new color (image [width / 2] [height / 2]); Int [] [] Filled = new int [width] [height]; Fill the flood (width / 2, height / 2, middle start, image, fill); // Show the creation of an ARM filled in the image if the face was found Buffer image buffer image 2 = new buffard image (width, height, buffer image, TEPDINRB); (Int x = 0; x and lt; width; x ++) {int pixels = across [x] [y] & lt; & Lt; For (int y = 0; y and lt; height; y ++) 16 | Fill [x] [y] & lt; & Lt; 8 | Fill [x] [y]; Buffer image 2 .set rgb (x, y, pixel); }} // save the ARA saved file output file as a image file = new file ("/ home / lily / picture / api / dpa / jpg"); ImageIO.write (buffer image 2, "jpg", outputfile); } Public static int [] [] floodFill (int x, int y, color target color, int [] [] image, int [] [] full) {if (x [y]]! = Targetcolor.getRGB )) {Return Fill; } Filled [x] [y] = image [x] [y]; Fill the flood (x - 1, y, target color, image, fill); Fill the flood (X + 1, Y, target color, image, fill); Fill the flood (X, Y-1, target color, image, fill); Fill the flood (X, Y + 1, target color, image, fill); Fill the refund; }

Bonus question: I would like to accept similar colors to fill with floods but not in the same way, because I am working with a picture.

Two important elements are missing in the flood flood functions you post. :

  1. If the area of ​​the same color as the first pixel will extend to the extent of the image, then the function will try to reach image on an invalid index, You can check by checking the X and Y coordinates, which you are investigating, and if they are out of range, then immediately return .
  2. If there is more than one adjacent pixel from the same color, then the functional limitless will be the reason for the rebacers, because the initial call will call flood fill on the second pixel, which is then the first pixel On the flood fill call will proceed, and so on. You have a way to make sure that you call only flood fill on special pixels once.

    Since you are not seeing any of these two symptoms, and you do not see anything from the resulting image, I think the color of the initial pixel is not correct when you < Code> color pass the integer to the constructor, are you sure that it uses the RBG interpretation of that integer?

No comments:

Post a Comment