Rotating BufferedImage changes its colors

前端 未结 3 1257
谎友^
谎友^ 2020-12-06 23:39

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

相关标签:
3条回答
  • 2020-12-07 00:12

    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);
    
    0 讨论(0)
  • 2020-12-07 00:21

    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 );
    
    0 讨论(0)
  • 2020-12-07 00:23

    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));
    
    0 讨论(0)
提交回复
热议问题