【问题标题】:Resizing image and its bounding box调整图像大小及其边界框
【发布时间】:2018-09-03 02:19:36
【问题描述】:

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

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

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

【问题讨论】:

  • 欢迎来到本站!查看tourhow-to-ask page,了解更多关于提出可以吸引高质量答案的问题的信息。您可以edit your question 包含更多信息。 Box.drawBox 在哪里定义?我在 numpy 或 opencv 文档中没有看到它。
  • 我删除了那行,这里不相关
  • 我不确定我是否理解。您能否将代码修改为MCVE 并发布?边界框是你原图的图像数据的一部分,还是你单独画的?
  • 我有图像和 xml 文档,其中存储了 x、y、宽度和高度。我认为如何在这里加载图像/xml 文档并不重要,当我调整图像大小时,我也需要调整边界框的大小
  • @jejjejd,我仍然看不到在原始图像上绘制框的一段代码。仅当您展示如何绘制两个框的代码时,我们才能讨论一致性。特别是在您的“原始”图像上,框架不是矩形(128,25) - (447,375)。实际的左上角是关于(160,35)

标签: python-3.x numpy opencv math image-processing


【解决方案1】:

我认为有两个问题:

  1. 您应该交换x_y_,因为shape[0] 实际上是y 维度而shape[1] 是x 维度
  2. 您应该在原始图像和缩放图像上使用相同的坐标。在您的原始图像上,矩形是(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,它都保持在那里(因此缩放实际上可以正常工作)。

【讨论】:

    【解决方案2】:

    你可以使用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>"

    它会调整所有数据集的大小并重写新的注释文件以调整大小的图像

    【讨论】:

    • 你可能想详细解释一下如何做到这一点。
    【解决方案3】:

    另一种方法是使用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/

    pip 安装 chitra

    【讨论】:

      猜你喜欢
      • 2023-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-24
      • 2014-06-10
      • 1970-01-01
      • 2013-09-05
      相关资源
      最近更新 更多