0

I have a 2D boolean array that is a segmented image and looks like this:

 000000000000000000
 000000011110000000
 000001110111100000
 000001111110100000
 000000111101100000
 000000111011000000

And I want to crop the picture and resize it to a specific size like this:

 00111100
 11101111
 11111101
 01111011
 01110110

I have tried to use a BufferedImage to save it and use getSubimage() but I can't get the boundaries right...

I also have tried this method where I have the core of the buffered image:

            int Width = buffer.getWidth();
            int Height = buffer.getHeight();
            int x1= (int) (core.getX()) - (Width/2);
            int y1= (int)(core.getY()) - (Height/2);
            buffer = buffer.getSubimage(x1, y1, Width, Height); 

How can I crop it and resize it?

  • Show the code that you tried, it could help someone to give you an answer – faljbour Apr 18 '15 at 22:40
  • your width, height in your getSubimage function is exceeding the image width and height if you are staring in the middle of the image. if your width and height start at width/2 and height/2 then, the remaining width and height is width/2 and height/2, the method will work if you have the correct dimension – faljbour Apr 18 '15 at 23:39

1 Answers1

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



    int lowHorizontal =arr[0].length; // give it length of columns "how many columns
    int highHorizontal =0;

    for(int i=0; i<arr.length; i++){
        for(int j=0; j<arr[0].length; j++){

            if(arr[i][j] == 1 ){
                if(lowHorizontal > j) 
                    lowHorizontal = j;
            }
        }
    }

    for(int i=arr.length-1; i>=0; i--){
        for(int j=arr[0].length-1; j>=0; j--){
            if(arr[i][j]==1){
                if(highHorizontal < j)
                    highHorizontal = j;
            }
        }
    }


    for(int i=0; i<arr.length; i++){
        for(int j=lowHorizontal; j<=highHorizontal; j++){
            System.out.print(arr[i][j] +" ");
        }
        System.out.println();
    }

This code will figure the horizontal indexes, I'm sure there is a better way. Try to apply the same thing to figure the vertical indexes.

Index of j when I print should be in boundaries so thats why I put j<=highHo... this is the output of the above code

0 0 0 0 0 0 0
0 0 1 1 1 1 0
1 1 1 0 1 1 1 
1 1 1 1 1 1 0 
0 1 1 1 1 0 1 
0 1 1 1 0 1 1 
Abdullah
  • 26
  • 3