使用cv2.resize后获取旋转矩形的大小和位置

时间:2018-05-23 12:39:47

标签: python opencv image-processing

我有一张图片,例如Width=999Height=767。我知道投资回报率的轮廓,并使用rect=cv2.minAreaRect()获取了CenterPosition周围的WidthHeightAnglerotated rectangle 。现在,我需要将图像大小调整为另一个图像的大小,例如Width=4096Height=2160。到目前为止,我正在使用cv2.resize()这样做。

我现在的问题是,我的矩形的boxPoints当然也发生在其他地方,rect中的数据关于CenterPositionWidth,{{1现在已调整大小的ROI周围的Height的{​​{1}}和Angle未更新,因此错误。我尝试了不同的解决方法,但还没有找到任何解决方案。

这是我的代码:

rotated rectangle

以下是import numpy as np import cv2 #Create black image img = np.zeros((767, 999, 3), np.uint8) #Turn ROI to white cv2.fillConvexPoly(img, np.array(ROI_contour), (255, 255, 255)) #Get Width, Height, and Angle of rectangle around ROI rect = cv2.minAreaRect(np.array(ROI_contour)) #Draw rotated rectangle in red box = cv2.boxPoints(rect) box = np.int0(box) cv2.drawContours(img,[box], 0, (0,0,255), 1) #Resize img to new size img_resized = cv2.resize(img, (4096, 2160), interpolation=cv2.INTER_AREA) 例如可能看起来像:

在调整大小之前将ROI设置为白色 - 通过rect知道ROI的CenterPosition,Width,Height和Angle。

image

如何获得调整后的投资回报率的新宽度,高度和角度?

1 个答案:

答案 0 :(得分:2)

这很简单unitary method

在你的例子中,h_old = 767,w_old = 999; h_new = 4096,w_new = 2160。

h_ratio = h_new / h_old = 5.34,w_ratio = w_new / w_old = 2.16

假设图像中找到的矩形的center_position,width和height分别为:(old_center_x,old_center_y),old_rect_width和old_rect_height。

然后新值将变为:

(old_center_x * w_ratio,old_center_y * h_ratio),old_rect_width * w_ratio,old_rect_height * h_ratio。

由于两幅影像的宽高比也不相同, old_aspect_ratio = 999/767 = 1.30,new_aspect_ratio = 2160/4096 = 0.52,您还需要将此因子乘以新维度。

相关问题