I have an image of size 720 x 1280, and I can resize it to 256 x 256 like this
import cv2
img = cv2.imread('sample_img.jpg')
img_small = cv2.resize(img, (256, 256), interpolation=cv2.INTER_CUBIC)
Say I have a bounding box in the original image (top left corner (50, 100), bottom right corner (350, 300)), how do I get the coordinates of new bounding box?
You can do it by simply using the scale of your resize operation. Like this -
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 )
Answer:
new_x_coordinate = x_coordinate/(original_x_length/new_x_length)
new_y_coordinate = y_coordinate/(original_y_length/new_y_length)
Full code:
import numpy as np
def new_coordinates_after_resize_img(original_size, new_size, original_coordinate):
original_size = np.array(original_size)
new_size = np.array(new_size)
original_coordinate = np.array(original_coordinate)
xy = original_coordinate/(original_size/new_size)
x, y = int(xy[0]), int(xy[1])
return (x, y)
output = new_coordinates_after_resize_img((1080,720), (244,244), (102, 34)) # just modify this line
print(output) # output: (23, 11)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With