旋转图像时如何旋转图像上的点?

时间:2019-06-13 17:52:38

标签: python

我有一个x点列表和一个与图像相关的y点列表。我的意思是旋转此图像并使点也随之旋转。我该怎么做?

这是针对需要更多数据的ML项目,因此,我随机旋转每个图像及其对应的关键点以增加数据集并帮助模型更好地推广。

我正在使用PIL图像库来打开和旋转图像。

def rotate_via_numpy(point, radians):
    """Use numpy to build a rotation matrix and take the dot product."""
    x, y = point
    c, s = np.cos(radians), np.sin(radians)
    j = np.matrix([[c, s], [-s, c]])
    m = np.dot(j, [x, y])

    return float(m.T[0]), float(m.T[1])

for j in range(len(new_points_x)):
    rotated_x, rotated_y = rotate_via_numpy((new_points_x[j], 
                           new_points_y[j]), math.radians(rotation))
    rotated_x, rotated_y = abs(rotated_x), abs(rotated_y)
    rotated_x = rotated_image_width - rotated_x - 1
    rotated_y = rotated_image_height - rotated_y - 1
    rotated_image_points_x.append(rotated_x)
    rotated_image_points_y.append(rotated_y)

我最近仅在90度,180度和270度进行了测试,只是为了了解它如何处理最简单的角度并且通常会变得非常接近,但是通常需要水平或垂直反射点才能将其反射应该在图片上。

0 个答案:

没有答案