使用 cv2 调整图像大小后,如何获取新的边界框坐标

PSN*_*SNR 7 python opencv computer-vision

我有一个大小为 720 x 1280 的图像,我可以像这样将其调整为 256 x 256

import cv2
img = cv2.imread('sample_img.jpg')
img_small = cv2.resize(img, (256, 256), interpolation=cv2.INTER_CUBIC)
Run Code Online (Sandbox Code Playgroud)

假设我在原始图像中有一个边界框(左上角 (50, 100),右下角 (350, 300)),我如何获得新边界框的坐标?

Ros*_*ane 6

您可以通过简单地使用调整大小操作的比例来做到这一点。像这样 -

import numpy as np

# Get the scaling factor
# img_shape = (y, x)
# reshaped_img_shape = (y1, x1)
# the scaling factor = (y1/y, x1/x)
scale = np.flipud(np.divide(reshaped_img_shape, img_shape))  # you have to flip because the image.shape is (y,x) but your corner points are (x,y)

#use this on to get new top left corner and bottom right corner coordinates
new_top_left_corner = np.multiply(top_left_corner, scale )
new_bottom_right_corner = np.multiply(bottom_right_corner, scale )
Run Code Online (Sandbox Code Playgroud)