【问题标题】:Get size and position of rotated rectangle after using cv2.resize使用 cv2.resize 后获取旋转矩形的大小和位置
【发布时间】:2018-11-02 11:03:30
【问题描述】:

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

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

这是我的代码:

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) 

这是img 例如可能看起来像:

在调整大小之前,带有白色 ROI 的 img - ROI 的中心位置、宽度、高度和角度由 rect 知道。

如何获得调整后 ROI 的新宽度、高度和角度?

【问题讨论】:

  • h(new) / h(old) 和 w(new) / w(old) 会给你新的值,对吧?这里有基础数学!​​

标签: python opencv image-processing


【解决方案1】:

这很简单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

假设old图片中矩形的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,您也需要将此因子乘以新尺寸。

【讨论】:

    猜你喜欢
    • 2018-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-14
    • 1970-01-01
    • 2015-11-30
    相关资源
    最近更新 更多