ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java-AffineTransform截断图像

2019-10-09 17:02:09  阅读:236  来源: 互联网

标签:affinetransform java image-processing rotation image-rotation


我有一个图像,必须将其旋转45、90、135、180度.我在做什么:

try {
    BufferedImage src = ImageIO.read(new File("src.png"));
    double ang = Math.toRadians(90);

    AffineTransform t = new AffineTransform();
    t.setToRotation(ang, src.getWidth() / 2, src.getHeight() / 2);

    AffineTransformOp op = new AffineTransformOp(t, null);
    BufferedImage dst = new BufferedImage(src.getWidth(), src.getHeight(), src.getType());
    op.filter(src, dst);

    ImageIO.write(dst, "png", new File("output.png"));
} catch(Exception ex) { ex.printStackTrace();
}

问题是图像改变了位置并超出了目标图像的范围:

The problem http://img32.imageshack.us/img32/3328/resultcs.png

我已经对此进行了搜索,并找到了以下问题的解决方案:AffineTransform truncates image, what do I wrong?但是我不太理解,它仅适用于象限.我尝试将目的地的宽度和高度乘以两倍,但失败了:

Another fail http://img401.imageshack.us/img401/2417/result2a.png

如何解决这个问题?目标图像不应有任何多余的(对角线旋转除外)空白或截断的区域.角度问题(0 == 180或顺时针方向)并不重要.

谢谢你的帮助.

解决方法:

编辑:现在,它适用于一般情况.

围绕中心进行旋转,并将中心放置在目标图像中与源图像中相同的位置(正确行为).

我已经修改了您的代码以转换源图像矩形,以便我们可以轻松获得新的尺寸/图像偏移.这用于构造具有正确尺寸的目标BufferedImage,并将平移附加到AffineTransform,以便将图像中心放置在目标图像的中心.

        BufferedImage src = ImageIO.read(new File(INPUT));
        int w = src.getWidth();
        int h = src.getHeight();

        AffineTransform t = new AffineTransform();
        double ang = Math.toRadians(35);
        t.setToRotation(ang, w / 2d, h / 2d);

        // source image rectangle
        Point[] points = {
            new Point(0, 0),
            new Point(w, 0),
            new Point(w, h),
            new Point(0, h)
        };

        // transform to destination rectangle
        t.transform(points, 0, points, 0, 4);

        // get destination rectangle bounding box
        Point min = new Point(points[0]);
        Point max = new Point(points[0]);
        for (int i = 1, n = points.length; i < n; i ++) {
            Point p = points[i];
            double pX = p.getX(), pY = p.getY();

            // update min/max x
            if (pX < min.getX()) min.setLocation(pX, min.getY());
            if (pX > max.getX()) max.setLocation(pX, max.getY());

            // update min/max y
            if (pY < min.getY()) min.setLocation(min.getX(), pY);
            if (pY > max.getY()) max.setLocation(max.getX(), pY);
        }

        // determine new width, height
        w = (int) (max.getX() - min.getX());
        h = (int) (max.getY() - min.getY());

        // determine required translation
        double tx = min.getX();
        double ty = min.getY();

        // append required translation
        AffineTransform translation = new AffineTransform();
        translation.translate(-tx, -ty);
        t.preConcatenate(translation);

        AffineTransformOp op = new AffineTransformOp(t, null);
        BufferedImage dst = new BufferedImage(w, h, src.getType());
        op.filter(src, dst);

        ImageIO.write(dst, "png", new File(OUTPUT));

标签:affinetransform,java,image-processing,rotation,image-rotation
来源: https://codeday.me/bug/20191009/1880586.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有