调整图像大小及其边界框 [英] Resizing image and its bounding box

查看:139
本文介绍了调整图像大小及其边界框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an image with bounding box in it, and I want to resize the image.

img = cv2.imread("img.jpg",3)
x_ = img.shape[0]
y_ = img.shape[1]
img = cv2.resize(img,(416,416));

现在我想计算比例因子:

Now I want to calculate the scale factor:

x_scale = ( 416 / x_)
y_scale = ( 416 / y_ )

并绘制图像,这是原始边界框的代码:

And draw an image, this is the code for the original bounding box:

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

但是使用这个我得到:

原文是:

我看不到这个逻辑中的任何一个标志,出了什么问题?

I don't see any flag in this logic, what's wrong?

整个代码:

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)

和drawbox

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

图像并且单独加载边界框的数据。我正在图像中绘制边界框。图像不包含框本身。

The image and the data for the bounding box are loaded separately. I am drawing the bounding box inside the image. The image does not contain the box itself.

推荐答案

我认为有两个问题:


  1. 你应该交换 x _ y _ ,因为 shape [0] 实际上是y维度, shape [1] 是x维度

  2. 您应该在原始和缩放图像上使用相同的坐标。在原始图像上,矩形为(160,35) - (555,470)而不是<$ c $您在代码中使用的c>(128,25) - (447,375)

  1. You should swap x_ and y_ because shape[0] is actually y-dimension and shape[1] is the x-dimension
  2. You should use the same coordinates on the original and scaled image. On your original image the rectangle is (160, 35) - (555, 470) rather than (128,25) - (447,375) that you use in the code.

如果我使用以下代码:

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,

and use your "original" image as "49466033\img.png",

我得到以下图片

正如你所看到的那样,我更薄的蓝线恰好位于你的内心你原来的红线,无论你选择什么 targetSize ,它都会留在那里(所以缩放实际上可以正常工作)。

And as you can see my thinner blue line lies exactly inside your original red line and it stays there whatever targetSize you chose (so the scaling actually works correctly).

这篇关于调整图像大小及其边界框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆