javabufferedimage

How to programmatically remove background of an image in java?


Using bufferedImage or something is there a quick way to do this?

At present my approach:

All the images are centered on a white background. So the top left pixel is always white.

This seems ok to me. But I was wondering if there is a better way to do this?


Solution

  • But I was wondering if there is a better way to do this?

    AFAIK, no. (In fact, I suspect that it is provable that there is no alternative to your general approach.)

    However, your "repeat for all connected pixels" doesn't specify how you do that. The obvious alternatives are:

    There may be other strategies.

    This problem is similar to flood filling, and flood-fill algorithms can be adapted to do this. Look at Best way to fill shapes in Java (no recursion) for some examples.