我有一个带有边界框的图像,我想调整图像的大小。
img = cv2.imread("img.jpg",3)
x_ = img.shape[0]
y_ = img.shape[1]
img = cv2.resize(img,(416,416));
现在我想计算比例因子:
x_scale = ( 416 / x_)
y_scale = ( 416 / y_ )
并绘制图像,这是原始边界框的代码:
( 128, 25, 447, 375 ) = ( xmin,ymin,xmax,ymax)
x = int(np.round(128*x_scale))
y = int(np.round(25*y_scale))
xmax= int(np.round (447*(x_scale)))
ymax= int(np.round(375*y_scale))
但是使用这个我得到:
而原来的是:
我在这个逻辑中没有看到任何标志,怎么了?
整个代码:
imageToPredict = cv2.imread("img.jpg",3)
print(imageToPredict.shape)
x_ = imageToPredict.shape[0]
y_ = imageToPredict.shape[1]
x_scale = 416/x_
y_scale = 416/y_
print(x_scale,y_scale)
img = cv2.resize(imageToPredict,(416,416));
img = np.array(img);
x = int(np.round(128*x_scale))
y = int(np.round(25*y_scale))
xmax= int(np.round (447*(x_scale)))
ymax= int(np.round(375*y_scale))
Box.drawBox([[1,0, x,y,xmax,ymax]],img)
和抽屉
def drawBox(boxes, image):
for i in range (0, len(boxes)):
cv2.rectangle(image,(boxes[i][2],boxes[i][3]),(boxes[i][4],boxes[i][5]),(0,0,120),3)
cv2.imshow("img",image)
cv2.waitKey(0)
cv2.destroyAllWindows()
边界框的图像和数据是分开加载的。我正在图像内部绘制边界框。图像不包含框本身。
原文由 jejjejd 发布,翻译遵循 CC BY-SA 4.0 许可协议
我认为有两个问题:
x_
andy_
becauseshape[0]
is actually y-dimension andshape[1]
is the x-dimension(160, 35)
-(555, 470)
rather than(128,25)
-(447,375)
that you use in the code.如果我使用以下代码:
并将您的“原始”图像用作“49466033\img.png”,
我得到以下图像
正如您所看到的,我的细蓝线正好位于您原来的红线内,并且无论您选择什么
targetSize
它都留在那里(因此缩放实际上可以正常工作)。