4

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

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

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

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

我已经读过这个问题,但获得的角度

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实现?

4

3 回答 3

1

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

谢谢大家

于 2011-04-12T23:11:20.487 回答
0

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

于 2011-04-07T14:45:13.273 回答
0

对我来说,以下内容用于对齐 OMR 表标记,其中 pointA 是左上角标记,点 B 是左下角标记

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

于 2013-05-28T06:40:17.450 回答