简体   繁体   中英

Normalization: Euclidean distance between a numpy array and one point

I have a numpy array (68x2) which correspond to 68 different points of a detected face.

[16.0000 93.0000]
[17.0000 116.0000]
[20.0000 139.0000]
[25.0000 162.0000]
[33.0000 184.0000]
[47.0000 205.0000]
[66.0000 219.0000] ... until 68

These points have the origin at the left bottom corner of the picture. I want to normalize according to a new center. Two questions, is there a way to do this without a loop? And is this the correct way to normalize according to a new origin?

new_origin = [112,135]
new_X
for point in X[0][0]:
    new_X.append(point-new_origin)

If you just want to translate those points, all you need to do is subtract a value to the left column (X values) and another to the right column (Y values):

>>> import numpy as np
>>> a = np.arange(10).reshape(5,2)
>>> a
array([[0, 1],
       [2, 3],
       [4, 5],
       [6, 7],
       [8, 9]])
>>> a[:,0] = a[:,0] - 112
>>> a[:,1] = a[:,1] - 135
>>> a
array([[-112, -134],
       [-110, -132],
       [-108, -130],
       [-106, -128],
       [-104, -126]])

You can do directly with np.subtract :

>>> np.subtract(a, [112, 135])
array([[-112, -134],
       [-110, -132],
       [-108, -130],
       [-106, -128],
       [-104, -126]])

or just :

>>> a - [112, 135]
array([[-112, -134],
       [-110, -132],
       [-108, -130],
       [-106, -128],
       [-104, -126]])

Note that with numpy, you almost never have to manually iterate over each element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM