我有一张尺寸为 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)
假设我在原始图像中有一个边界框(左上角 (50, 100),右下角 (350, 300)),如何获取新边界框的坐标?
您只需使用调整大小操作的比例即可完成此操作。像这样 -
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 )
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)