我有一个图像,例如Width=999
和Height=767
。我知道我的ROI的轮廓,并使用rect=cv2.minAreaRect()
来获得CenterPosition
、Width
、Height
和{rotated rectangle
。现在我需要将图像调整为另一个图像的大小,例如Width=4096
和{cv2.resize()
来完成此操作。在
我现在的问题是,当然,矩形的boxPoints
发生在其他地方,rect
中关于CenterPosition
、Width
、Height
和{
这是我的代码:
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知道。在
我如何获得新的宽度,高度和角度的调整后的投资回报率?在
这很简单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,您也需要将此系数乘以新的维度。在
相关问题 更多 >
编程相关推荐