python-3.x - 调整图像及其边界框的大小

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

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

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

但是使用这个我得到:

enter image description here

虽然原来是:

enter image description here

我在这个逻辑中没有看到任何标志,怎么了?

完整代码:

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

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

最佳答案

我认为有两个问题:

  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”,

Original image

我得到了下图

Processed image

正如您所看到的,我的细蓝线正好位于您原来的红线内,并且无论您选择什么 targetSize,它都会保持在那里(因此缩放实际上可以正常工作)。

关于python-3.x - 调整图像及其边界框的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49466033/

相关文章:

python - 将 JSON 字符串解析为 numpy 数组的最快方法

opencv - Opencv,赢得7、64位

opencv - 将L形分成两行

python - Keras pad_sequences 为基数为 10 的 int () 抛出无效文字

python - 无法在 VS Code 中导入

python - 递归错误 : maximum recursion depth exceeded while calling a Python object when using pickle. 加载()

python - 获取 N 维数组的所有索引作为列表

python - 在 python3 中加入字符串列表的第一个元素

python - 如何在 numpy/ipython.parallel 中进行分布式矩阵乘法?

android - 错误 : cannot find symbol variable GL_TEXTURE_EXTERNAL_OES + OpenCV on Android Studio