numpy 调整图像及其边框的大小

pbwdgjma  于 2023-01-26  发布在  其他
关注(0)|答案(4)|浏览(158)

我有一个图像与边界框中,我想调整图像的大小。

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()

图像和边界框的数据是分别加载的。我在图像内部绘制边界框。图像不包含框本身。

ix0qys7i

ix0qys7i1#

我认为有两个问题:
1.您应该交换x_y_,因为shape[0]实际上是y维度,而shape[1]是x维度
1.在原始图像和缩放后的图像上应该使用相同的坐标,原始图像上的矩形是(160, 35)-(555, 470),而不是代码中使用的(128,25)-(447,375)
如果我使用下面的代码:

import cv2
import numpy as np

def drawBox(boxes, image):
    for i in range(0, len(boxes)):
        # changed color and width to make it visible
        cv2.rectangle(image, (boxes[i][2], boxes[i][3]), (boxes[i][4], boxes[i][5]), (255, 0, 0), 1)
    cv2.imshow("img", image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

def cvTest():
    # imageToPredict = cv2.imread("img.jpg", 3)
    imageToPredict = cv2.imread("49466033\\img.png ", 3)
    print(imageToPredict.shape)

    # Note: flipped comparing to your original code!
    # x_ = imageToPredict.shape[0]
    # y_ = imageToPredict.shape[1]
    y_ = imageToPredict.shape[0]
    x_ = imageToPredict.shape[1]

    targetSize = 416
    x_scale = targetSize / x_
    y_scale = targetSize / y_
    print(x_scale, y_scale)
    img = cv2.resize(imageToPredict, (targetSize, targetSize));
    print(img.shape)
    img = np.array(img);

    # original frame as named values
    (origLeft, origTop, origRight, origBottom) = (160, 35, 555, 470)

    x = int(np.round(origLeft * x_scale))
    y = int(np.round(origTop * y_scale))
    xmax = int(np.round(origRight * x_scale))
    ymax = int(np.round(origBottom * y_scale))
    # Box.drawBox([[1, 0, x, y, xmax, ymax]], img)
    drawBox([[1, 0, x, y, xmax, ymax]], img)

cvTest()

并使用您的“原始”图像作为“49466033\img.png”,

我得到了以下图像

正如你所看到的,我的细蓝线正好位于你原来的红线之内,无论你选择了什么targetSize,它都保持在那里(所以缩放实际上是正确的)。

vnjpjtjt

vnjpjtjt2#

另一种方法是使用CHITRA

image = Chitra(img_path, box, label)
# Chitra can rescale your bounding box automatically based on the new image size.
image.resize_image_with_bbox((224, 224))

print('rescaled bbox:', image.bounding_boxes)
plt.imshow(image.draw_boxes())

https://chitra.readthedocs.io/en/latest/
管道安装chitra

kqlmhetl

kqlmhetl3#

我在使用TensorFlow.js和MobileNet-v2进行预测时遇到了Angular中边界框坐标的问题。坐标基于视频帧的分辨率。
但是我是在一个固定高度和宽度的画布上显示视频的,我通过将坐标除以原始视频分辨率与画布分辨率的比率来解决这个问题。

const x = prediction.bbox[0] / (this.Owidth / 300);
      const y = prediction.bbox[1] / (this.Oheight / 300);
      const width = prediction.bbox[2] / (this.Owidth / 300);
      const height = prediction.bbox[3] / (this.Oheight / 300);
      // Draw the bounding box.
      ctx.strokeStyle = '#99ff00';
      ctx.lineWidth = 2;
      ctx.strokeRect(x, y, width, height);
  • this.Owidth & this.Oheight为视频的原始分辨率,由.
this.video.addEventListener(
      'loadedmetadata',
      (e: any) => {
        this.Owidth = this.video.videoWidth;
        this.Oheight = this.video.videoHeight;
        console.log(this.Owidth, this.Oheight, ' pixels ');
      },
      false
    );
  • 300 X 300是静态画布的宽度和高度。
u4vypkhs

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>"很容易
它可以调整所有数据集的大小,并将新的注解文件重写为调整大小后的图像

相关问题