【发布时间】:2020-12-09 20:58:38
【问题描述】:
我看到了一些与此主题相关的问题,但即使在关注它们之后我也无法获得我想要的结果,所以我认为我的数学有些问题。这是场景。
我有一张必须旋转 90 度的图像。同时,我得到了在图像中某个对象上绘制的矩形角的坐标 (x_min, y_min) 和 (x_max, y_max)。我们以下图为例,这里使用的矩形由 (4, 196) 和 (145, 269) 定义。
然后,我的目标是旋转图像并对矩形执行相同的操作,以确保它始终围绕目标对象。
首先,我使用 imutils 将图像旋转 90 度(顺时针)。
90_rotated_image = imutils.rotate_bound(original_image, 90)
然后,我正在计算矩形的新坐标。为此,我需要知道 imutils 旋转图像的点。我尝试了一些组合,包括 (0,0),但在这里我假设它是图像本身的中心。
radians = math.radians(90)
h, w, c = original_image.shape
center_x = w/2
center_y = h/2
# Changes to solve the problem. Center of rotated image is now considered.
h_rotated, w_rotated, c = 90_rotated_image.shape
center_x_rotated = w_rotated/2
center_y_rotated = h_rotated/2
x_min_90 = center_x_rotated + math.cos(radians) * (x_min - center_x) - math.sin(radians) * (y_min - center_y)
y_min_90 = center_y_rotated + math.sin(radians) * (x_min - center_x) + math.cos(radians) * (y_min - center_y)
x_max_90 = center_x_rotated + math.cos(radians) * (x_max - center_x) - math.sin(radians) * (y_max - center_y)
y_max_90 = center_y_rotated + math.sin(radians) * (x_max - center_x) + math.cos(radians) * (y_max - center_y)
最后,我在旋转后的图像上绘制新的旋转矩形。
start_point = (x_min_90, y_min_90)
end_point = (x_max_90, y_max_90)
image = cv2.rectangle(90_rotated_image, start_point, end_point, (255, 0, 0), 2)
这就是我得到的。
问题是:这里有什么问题?为什么矩形不能正确旋转并给我预期的结果,模拟如下。
编辑: 对于遇到相同问题的任何人,使旋转适用于矩形图像的解决方案非常简单。添加到旋转结果的 center_x 和 center_y 变量(涉及 sin 和 cos 的乘法)必须与图像旋转后的中心相关,而不是与我最初写入代码的预旋转相关。该帖子已更新以反映解决方案。
【问题讨论】: