简体   繁体   English

归一化:numpy 数组和一个点之间的欧几里德距离

[英]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.我有一个 numpy 数组(68x2),它对应于检测到的人脸的 68 个不同点。

[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):如果您只想转换这些点,您需要做的就是在左列(X 值)和右列(Y 值)中减去一个值:

>>> 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

>>> 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.请注意,使用 numpy,您几乎不必手动迭代每个元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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