简体   繁体   中英

Rotating BufferedImage changes its colors

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:

在此处输入图片说明

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);

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 );

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM