Iterating array in Java

120 views Asked by At

I have data BufferedImage containing black and white pixels, which is represented in 2-dimensional array like this

 protected int[][] arr = {{1,1,1,1,1,0,0},
                          {1,0,0,0,1,0,0},
                          {1,0,0,0,1,0,0},
                          {1,1,1,1,1,0,0},
                          {0,0,0,0,0,0,0}};

array value is 1 means the pixel is white and 0 black pixels.

I want to create a program that is worth tracing array 1 and array that has been traced converted to 0 like this:

The first iteration :

0111100
1000100
1000100
1111100
0000000

second iteration :

0011100
1000100
1000100
1111100
0000000

third :

0001100
1000100
1000100
1111100
0000000

fourth :

0000100
1000100
1000100
1111100
0000000

fifth :

0000000
1000100
1000100
1111100
0000000

sixth :

0000000
1000000
1000100
1111100
0000000

until the last iteration all array values will be 0.

I have made a program like this:

public void cekNearby()
{
   /* for(int y=0;y<arr.length;y++)
    {
        for(int x=0;x<arr[0].length;x++)
        {
           */
            int x=0,y=0;
            if(arr2[y][x] ==1)
            {
                startPointX = x;
                startPointY = y;
                int moveNextX=x,tempX = arr.length;
                int moveNextY=y,tempY = arr[0].length;
                arr2[y][x]=0;
                while(startPointX!= tempX || startPointY != tempY)
                {
                    System.out.println("MoveNextX->"+moveNextX+"moveNextY->"+moveNextY+"tempX ->"+tempX+ " tempY ->" +tempY + " StartpointX->"+startPointX +" startPointY ->"+startPointY );

                    int moveY = max(0,moveNextY-1) ;
                    ketemu = false;
                    System.out.println("Atas "+moveNextX + " "+moveNextY);
                    while(moveY <= min(arr2.length-1,moveNextY+1) && ketemu == false)
                    {
                        System.out.println("ASD "+moveY);
                        int moveX = max(0,moveNextX-1);
                       while(moveX <=min(arr2[0].length-1,moveNextX+1) && ketemu == false)
                            {
                                System.out.print(" Y :" + moveY);
                                System.out.print(" X : "+moveX);
                                if(arr2[moveY][moveX]==1)
                                {
                                    arr2[moveY][moveX]=0;
                                    tempX = moveX;
                                    tempY = moveY;
                                    moveNextX = moveX;
                                    moveNextY = moveY;
                                    ketemu = true;
                                    System.out.print("Ketemu");
                                }else
                                {
                                    ketemu = false;
                                }
                                moveX++;
                            }


                        moveY++;
                        System.out.println();
                    }
                    print_array(arr2);
                }
            }
        }

however, there are white pixels in these positions when the iterations are completed.

0000000
0000000
0000000
1000100
0000000

What is wrong with my logic?
any suggestions would be greatly appreciated.

1

There are 1 answers

1
Silly Freak On BEST ANSWER

glancing at your output, it seems you use a neighborhood of 8 cells; you seem to want to only delete one "run" of 1 pixels, thus this is correct:

11100  01100  00100  00000  00000  00000  00000
10101  10101  10101  10101  10001  10001  00001
11101  11101  11101  11101  11101  10101  10101

The left-bottom pixel is checked before the bottom pixel, and the top-left pixel is checked before the left pixel. If you want to only go right angles, you need to use a neighborhood of four. in each case, the disconnected ones to the right will stay.

If you want to have both right and diagonal angles, you will have to prefer one angle over the other; check the surrounding pixels in this (or a similar) order:

516
2 3
748

If you mean to remove all neighboring cells, not only those on a single connected run, you might want the floodfill algorithm.