Rotating BufferedImage changes its colors

2020-02-13 04:29发布

I'm trying to code a class to seam carve images in x and y direction. The x direction is working, and to reduce the y direction I thought about simply rotating the image 90° and run the same code over the already rescaled image (in x direction only) and after that, rotate it back to its initial state.

I found something with AffineTransform and tried it. It actually produced a rotated image, but messed up the colors and I don't know why.

This is all the code:

import java.awt.image.BufferedImage;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.io.File;
import java.io.IOException;
import javafx.scene.paint.Color;
import javax.imageio.ImageIO;


public class example {
/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws IOException {
    // TODO code application logic here

    BufferedImage imgIn = ImageIO.read(new File("landscape.jpg"));
    BufferedImage imgIn2 = imgIn;

    AffineTransform tx = new AffineTransform();
    tx.rotate(Math.PI/2, imgIn2.getWidth() / 2, imgIn2.getHeight() / 2);//(radian,arbit_X,arbit_Y)

    AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
    BufferedImage last = op.filter(imgIn2, null);//(sourse,destination)
    ImageIO.write(last, "JPEG", new File("distortedColors.jpg"));
}

}

Just alter the filename in
BufferedImage imgIn = ImageIO.read(new File("landscape.jpg")); and try it.

When executed, you get 4 images: a heatmap, an image with seams in it and a rescaled image. The last image is a test to see if the rotation worked and it should show a rotated image but with distorted colors...

Help would be greatly appreciated!

EDIT:

enter image description here

3条回答
Deceive 欺骗
2楼-- · 2020-02-13 05:18

The problem is with the AffineTransformOp You need :

AffineTransformOp.TYPE_NEAREST_NEIGHBOR

instead of the BILINEAR you have now.

Second paragraph from documentation hints towards this.

This class uses an affine transform to perform a linear mapping from 2D coordinates in the source image or Raster to 2D coordinates in the destination image or Raster. The type of interpolation that is used is specified through a constructor, either by a RenderingHints object or by one of the integer interpolation types defined in this class. If a RenderingHints object is specified in the constructor, the interpolation hint and the rendering quality hint are used to set the interpolation type for this operation.

The color rendering hint and the dithering hint can be used when color conversion is required. Note that the following constraints have to be met: The source and destination must be different. For Raster objects, the number of bands in the source must be equal to the number of bands in the destination.

So this works

AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
查看更多
ゆ 、 Hurt°
3楼-- · 2020-02-13 05:20

Building on what bhavya said...

Keep it simple and you should use the dimensions expected from the operation:

AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
BufferedImage destinationImage = op.filter(bImage, op.createCompatibleDestImage(bImage, null));
查看更多
Emotional °昔
4楼-- · 2020-02-13 05:24

It seems like there's a color conversion happening due to passing null to op.filter(imgIn2, null);.

If you change it like that it should work:

BufferedImage last = new BufferedImage( imgIn2.getWidth(), imgIn2.getHeight(), imgIn2.getType() );
op.filter(imgIn2, last );
查看更多
登录 后发表回答