views:

20

answers:

1

I have a BufferedImage which is quite tall (something like 1100 pixels height) and not especially wide (maybe 200-300 pixels). The image consists of a data plot, but most of the pixels represents a background color/value that's not really interesting. My problem is that when the image is resized from a height of 1100 px to something like 200-300 px, many of the pixels that actually contained interesting data is trashed by background color pixels. Is there any way to tell the Java 2D rescaling algorithms that I prefer pixels of certain values (or better, that I want to down-prioritize the background color) while rescaling? I use the following code snippet to rescale the image:

BufferedImage resized = new BufferedImage(width, height, imageType);
Graphics2D graphics2D = resized.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); //Has worked best in my case
graphics2D.drawImage(source, 0, 0, width, height, null);
graphics2D.dispose();

I'm sure that there's more effective ways of resizing images in Java, but that's not interesting (at least not for now).

A kind of boring thing is that I used a library before (which will not be used anymore) that actually managed to keep much of the valuable data representing pixels.

Note that I have the raw pixel data at hand (a 2D int array) if that is of any help. The application will handle many images of different sizes, so I don't know for sure that the height necessarily is 1100 pixels.

Thanks.

+2  A: 

Have you tried turning on anti-aliasing in the rendering hints? That might help.

If not, you could pretty easily write your own own scaling function. This is actually pretty simple - you just need to:

  • Loop over all the points in your target BufferedImage
  • For each of these loop over the relevant points in your raw pixel data (this will be a rectangular zone determined by your scaling factor)
  • Calculate the target colour using a formula of your choice based on the raw pixel values
  • Use BufferedImage.setRGB(...) to set the colour of the target pixel

Normal anti-aliasing would use something like an average of the source pixel values as a formula, it sounds like you want something that gives priority to particular values (e.g. take the maximum pixel value if your background pixels have low colour values)

mikera
I think I'll try that. Thanks!
Yngve Sneen Lindal