Normalize coordinates of an image

白昼怎懂夜的黑 提交于 2020-01-14 06:54:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!