问题
I am trying to create an object detection program in Python. The output should be the filename of the picture being analyzed, plus the "normalized coordinates" of the the object center.
Each image should be normalized as follows:
Top left = (0,0), Bottom left = (0,1), Top right = (1,0), Bottom right = (1,1)
Any advice or suggestions on how I can normalize the coordinates of each image?
回答1:
This simple function would get the job done:
def normalize_coordinates(row_i, col_j, img):
num_rows, num_cols = img.shape[:2]
x = col_j/(num_cols - 1.)
y = row_i/(num_rows - 1.)
return x, y
Demo
from skimage import io
from numpy import random
random.seed(42)
img = io.imread('https://i.stack.imgur.com/94l13.jpg')
row_i = random.randint(0, img.shape[0])
col_j = random.randint(0, img.shape[1])
x, y = normalize_coordinates(row_i, col_j, img)
print 'row = %d, column = %d >>> x = %.2f, y = %.2f' % (row_i, col_j, x, y)
Output:
row = 102, column = 435 >>> x = 0.71, y = 0.25
来源:https://stackoverflow.com/questions/48524516/normalize-coordinates-of-an-image