2

I have a Jpeg image with a "seeming" uniform background and an actual object in it. I want to crop the image to extract only the object. I started with following code where I am trying to find the (x,y) on left side to start cropping.

I found out that bottom left corner of the image has (0,0) coordinates. I am looping through and trying to find out the exact point where the color changes.

The problem is during first iteration itself : when x=0 and y is incrementing, though there is no change in color it is giving different RGB values for few pixels.(like pixel 240 , 241, 242)

BufferedImage bf =ImageIO.read.file(imagePath);
for(int x= 0;x<bf.getWidth();x++)
{
    for(int y=0; j<bf.getHeight();y++)
    {
        int color = bf.RGB(x,y);
        int adjacentColor = bf.(x,y+1);

        if(color !=adjacentColor)
        {
            LeftBoundaryPixels[count]=y;
            count++;
            break;
        }
    }
}
ben75
  • 29,217
  • 10
  • 88
  • 134
Chandana D
  • 21
  • 1

1 Answers1

0

You can use a color distance formula (like the distance formula sqrt(x^2 + y^2)) with a threshold distance to dismiss pixels that are close but not completely the same. For example: sqrt(r^2 + g^2 + b^2);.

Michal Kordas
  • 10,475
  • 7
  • 58
  • 103
ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • Thank you ! I used the formula . I also extracted RGB values using Color class and then applied the formula .I also used colorWithinTolerance method in following linkhttp://stackoverflow.com/questions/10678015/how-to-auto-crop-an-image-white-border-in-java – Chandana D May 08 '15 at 23:14