【问题标题】:How to rotate an image based on two points如何基于两点旋转图像
【发布时间】:2011-04-07 14:31:55
【问题描述】:

我处理了一些图像,在这些图像中我总是有两个点 (x1, y1) 和 (x2, y2)。像这样:

|----------------|
|                |
|    .           |
|                |
|          .     |
|                |
|----------------|

我需要编写一个算法来像这样对齐图像

|----------------|
|                |
|                |
|    .     .     |
|                |
|                |
|----------------|

我已经阅读了this的问题,但是获得的角度

double angle = Math.Atan2(pointB.Y - pointA.Y, pointB.X - pointA.X);

当我在 java 中使用这个旋转代码时不起作用:

public static BufferedImage tilt(BufferedImage image, double angle) {
    double sin = Math.abs(Math.sin(angle)), cos = Math.abs(Math.cos(angle));
    int w = image.getWidth(), h = image.getHeight();
    int neww = (int) Math.floor(w * cos + h * sin), newh = (int) Math
            .floor(h * cos + w * sin);
    GraphicsConfiguration gc = getDefaultConfiguration();
    BufferedImage result = gc.createCompatibleImage(neww, newh,
            Transparency.OPAQUE);
    Graphics2D g = result.createGraphics();

    g.setColor(Color.white);
    g.setBackground(Color.white);
    g.fillRect(0, 0, neww, newh);

    g.translate((neww - w) / 2, (newh - h) / 2);

    g.rotate(angle, w / 2, h / 2);
    g.drawRenderedImage(image, null);
    g.dispose();
    return result;
}

在之前的帖子中提到过他们使用c#代码之类的

myImage.TranslateTransform(-pointA.X, -pointA.Y);
myImage.RotateTransform((float) angle, MatrixOrder.Append);
myImage.TranslateTransform(pointA.X, pointA.Y, MatrixOrder.Append);

有没有人可以帮助这个案例的 java 实现?

【问题讨论】:

  • 当你说不工作时,到底发生了什么。我使用完全相同的代码(忘记了它来自哪里),非常成功。
  • 我的意思是,旋转没有正确对齐图像。我的一张图片需要旋转 19 度,但 Math.Atan2(pointB.Y - pointA.Y, pointB.X - pointA.X);返回类似 2 的东西

标签: java image image-rotation


【解决方案1】:

好的...为了解决问题,我刚刚将 Math.atan2 返回的弧度值转换为度数,旋转效果很好。

谢谢大家

【讨论】:

    【解决方案2】:

    如果示例中的左点是 A,示例中的右点是 B,请想象绘制一个具有 90 度角的点 C (A.X, B.Y) 来完成三角形。如果你用几何计算来计算角A的角度,你就知道旋转了多少。

    【讨论】:

    • 对不起,我如何计算角?
    【解决方案3】:

    对我来说,下面的工作是对齐 OMR 表标记,其中 pointA 是左上角标记,点 B 是左下角标记

    双角 = Math.Atan2(pointB.x - pointA.x, pointB.y - pointA.y);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多