我有一个图像与边界框中,我想调整图像的大小。
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()
图像和边界框的数据是分别加载的。我在图像内部绘制边界框。图像不包含框本身。
4条答案
按热度按时间ix0qys7i1#
我认为有两个问题:
1.您应该交换
x_
和y_
,因为shape[0]
实际上是y维度,而shape[1]
是x维度1.在原始图像和缩放后的图像上应该使用相同的坐标,原始图像上的矩形是
(160, 35)
-(555, 470)
,而不是代码中使用的(128,25)
-(447,375)
。如果我使用下面的代码:
并使用您的“原始”图像作为“49466033\img.png”,
我得到了以下图像
正如你所看到的,我的细蓝线正好位于你原来的红线之内,无论你选择了什么
targetSize
,它都保持在那里(所以缩放实际上是正确的)。vnjpjtjt2#
另一种方法是使用CHITRA
https://chitra.readthedocs.io/en/latest/
管道安装chitra
kqlmhetl3#
我在使用TensorFlow.js和MobileNet-v2进行预测时遇到了Angular中边界框坐标的问题。坐标基于视频帧的分辨率。
但是我是在一个固定高度和宽度的画布上显示视频的,我通过将坐标除以原始视频分辨率与画布分辨率的比率来解决这个问题。
this.Owidth & this.Oheight
为视频的原始分辨率,由.300 X 300
是静态画布的宽度和高度。u4vypkhs4#
您可以使用resize_dataset_pascalvoc
使用
python3 main.py -p <IMAGES_&_XML_PATH> --output <IMAGES_&_XML> --new_x <NEW_X_SIZE> --new_y <NEW_X_SIZE> --save_box_images <FLAG>"
很容易它可以调整所有数据集的大小,并将新的注解文件重写为调整大小后的图像