ViktorG ViktorG - 1 year ago 100
Java Question

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

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 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 = 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 = 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!


enter image description here

Answer Source

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