如何基于两点旋转图像
我有一些我操作的图像,在这些图像中我总是有两个点 (x1, y1) 和 (x2, y2)。像这样:
|----------------|
| |
| . |
| |
| . |
| |
|----------------|
我需要编写一个算法来对齐图像,就像这样
|----------------|
| |
| |
| . . |
| |
| |
|----------------|
我已经读过这个问题但是当我在 java 中使用此旋转代码时,获得的角度
double angle = Math.Atan2(pointB.Y - pointA.Y, pointB.X - pointA.X);
不起作用:
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 实现?
I have some images I manipulate, and in these images I always have two points (x1, y1) and (x2, y2). like this:
|----------------|
| |
| . |
| |
| . |
| |
|----------------|
I need to code an algorythm to align the image like this
|----------------|
| |
| |
| . . |
| |
| |
|----------------|
I already read this question but the angle obtained by
double angle = Math.Atan2(pointB.Y - pointA.Y, pointB.X - pointA.X);
Is not working when I use this rotation code in 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;
}
In the post mentioned before they use the c# code like
myImage.TranslateTransform(-pointA.X, -pointA.Y);
myImage.RotateTransform((float) angle, MatrixOrder.Append);
myImage.TranslateTransform(pointA.X, pointA.Y, MatrixOrder.Append);
Is there anybody that could help with a java implementation for this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好的...为了解决这个问题,我只是将 Math.atan2 返回的弧度值转换为度数,并且旋转效果很好。
谢谢大家
Ok... to solve the problem I just converted the radians value returned by Math.atan2 to degrees and the rotation worked well.
Thanx everybody
如果示例中的左侧点是 A,示例中的右侧点是 B,想象一下绘制一个具有 90 度角的点 C (AX, BY) 来完成三角形。如果你用几何计算来计算角A的角度,你就知道要旋转多少度。
If the left point in your example is A, and the right point in your example is B, imagine drawing a point C (A.X, B.Y) that has a 90 degree angle to complete the triangle. IF you use geometry calculation to calculate the angle of corner A, you know by how much to rotate.
对我来说,下面的工作是对齐OMR表标记,其中pointA是左上角标记,pointB是左下角标记
双角= Math.Atan2(pointB.x - pointA.x, pointB.y - pointA.y);
for me the below worked to align OMR sheet markings where pointA is TopLeft Corner mark, pointB is BottomLeft corner mark
double angle = Math.Atan2(pointB.x - pointA.x, pointB.y - pointA.y);