我试图得到一个位置的新点P1_r(x1_new, y1_new)
和P2_r(x2_new, y2_new)
后,图像旋转使用imutils.rotate_bound()
。原始图像包含线定义的点P1(x1, y2)
和P2(x2, y2)
。
import imutils
import numpy as np
P1 = np.array([x1, y1])
P2 = np.array([x2, y2])
字符串
起始位置如下(* 绿色区域表示图像 *):
的数据
在旋转之前,我必须计算由点P1(x1, y2)
、P2(x2, y2)
和y-axis
定义的线之间的angle
(在这种情况下,* 是由绿色线 * 表示的y轴):
的
然后有必要计算angle
,其中使用函数np.arccos()
:
if (P2[1] - P1[1]) < 0:
y_axis_vector = np.array([0, -1])
else:
y_axis_vector = np.array([0, 1])
if (P2[0] - P1[0]) < 0 and (P2[1] - P1[1]) :
y_axis_vector = np.array([0, 1])
p_unit_vector = (P2 - P1) / np.linalg.norm(P2-P1)
angle = np.arccos(np.dot(p_unit_vector, y_axis_vector)) * 180 / math.pi
型
然后可以旋转图像:
rotated_image = imutils.rotate_bound(original_image, -angle)
型
因此,结果应该如下所示:
的
现在我尝试计算新点P1_r(x1_new, y1_new)
和P2_r(x2_new, y2_new)
的位置。我尝试使用“标准”sin()
和cos()
函数来计算点P1_r
的新位置x
和y
:
x1_new = y1 * sin(angle) + x1 * cos(angle)
y1_new = y1 * cos(angle) - x1 * sin(angle)
型
点P2_r
:
x2_new = y2 * sin(angle) + x2 * cos(angle)
y2_new = y2 * cos(angle) - x2 * sin(angle)
型
但是它是doesn't work
,因为整个画面都是旋转的,不仅仅是线。
我该如何解决这个问题?
2条答案
按热度按时间hgqdbh6s1#
图像围绕其中心旋转。因此,对于任何点,首先围绕图像中心旋转该点,然后在旋转图像中调整偏移。
绕给定原点旋转:
个字符
然后计算旋转图像中的偏移量:
型
调整点数:
型
u1ehiz5o2#
你可以让opencv为你做计算。
字符串
的数据
型
的
型
[105 452]