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

2024-09-29 23:29:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个图像,例如Width=999Height=767。我知道我的ROI的轮廓,并使用rect=cv2.minAreaRect()来获得CenterPositionWidthHeight和{}周围的rotated rectangle。现在我需要将图像调整为另一个图像的大小,例如Width=4096和{}。到目前为止,我使用cv2.resize()来完成此操作。在

我现在的问题是,当然,矩形的boxPoints发生在其他地方,rect中关于CenterPositionWidthHeight和{}中的数据没有更新,因此是错误的。我尝试过不同的解决办法,但还没有找到任何解决办法。在

这是我的代码:

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知道。在

image

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


Tags: rect图像boximgnpwidthcv2height
1条回答
网友
1楼 · 发布于 2024-09-29 23:29:42

这很简单unitary method。在

在您的示例中,h_old=767,w_旧=999;h_new=4096,w_新=2160。在

h_比率=h_新/h_旧=5.34,w_比率=w_新/w_旧=2.16

假设在旧的图像中找到的矩形的中心位置、宽度和高度分别是:(旧中心x、旧中心)、旧矩形宽度和旧矩形高度。在

然后新值将变成:

(旧中心宽*宽比,旧中心宽*宽比,旧矩形宽*宽比,旧矩形高*高。在

由于两幅图像的纵横比也不一样, 旧宽高比=999/767=1.30,新宽高比=2160/4096=0.52,您也需要将此系数乘以新的维度。在

相关问题 更多 >

    热门问题