如何计算 Python OpenCV 中两个 ndarray 之间的欧氏距离

How to calculate euclidean distance between two ndarrays in Python OpenCV

我正在尝试计算两个图像之间的 euclidean distance。为此,我首先获取图像的 128d 数组,然后使用 cv2.norm() 获取距离。下面是代码:

embedder = cv2.dnn.readNetFromTorch(<model_path>)
embedder.setInput(faceBlob)
vec = embedder.forward()
print(vec)

vec_file = pickle.loads(open(args["recognizer"], "rb").read())
known_vec = vec_file.support_vectors_

for embedding in known_vec:
    print(embedding)
    distance = cv2.norm(vec, embedding)

在上面的代码中,我有来自图像文件的 vec 和来自 known_vecembedding。下面是 vecembedding 的样子:

vec:
[[ 1.50953727e-02  2.81099556e-03 -3.50600183e-02 -5.78538561e-03
   2.31029615e-02  1.73964068e-01  3.79475281e-02  1.27083873e-02
  -9.68848541e-02 -1.13846334e-02  1.92795545e-02  7.36472011e-02
   7.79130757e-02 -2.11485863e-01 -6.82436973e-02 -1.64214987e-04
  -2.01231852e-01  2.29396261e-02 -4.34093624e-02  9.49875787e-02
   1.96524531e-01 -1.40022561e-01  1.00606538e-01  3.70812230e-02
  -1.45635298e-02  3.85013111e-02 -8.84107649e-02 -3.15038770e-01
   3.25521380e-02  4.29384746e-02  1.74971391e-02  3.27903479e-02
  -4.76430990e-02  6.02841079e-02  3.60031053e-02 -4.40581292e-02
  -8.15121531e-02  1.46739334e-01  3.19194235e-02 -5.45275658e-02
   3.90344337e-02 -1.47340044e-01 -8.87186751e-02  9.13328975e-02
  -1.33012265e-01 -6.64092153e-02  1.45769000e-01 -4.49066125e-02
  -1.70968711e-01  1.84094254e-02 -1.43186841e-02 -3.82681675e-02
  -9.34342016e-03  3.55955921e-02  6.70149326e-02  1.09950025e-02
   1.09302737e-01  6.81546181e-02 -7.36390129e-02 -1.16702713e-01
  -1.40488185e-02 -2.61708386e-02  2.10996747e-01 -6.54504001e-02
   1.53530702e-01 -8.38626847e-02 -1.86689962e-02 -2.70418124e-03
  -2.32851990e-02  5.15586026e-02 -8.13494101e-02  7.11051449e-02
  -1.19156547e-01  1.64730344e-02  2.14404091e-02 -4.26124930e-02
  -7.58614466e-02  3.41765210e-02  4.33261022e-02  1.71321735e-01
  -1.44580662e-01 -4.46063727e-02  2.88061053e-02  4.15235199e-03
  -1.05133533e-01  1.83968637e-02  1.12521172e-01  5.98449074e-02
   2.27536708e-02 -3.94514054e-02  8.82636383e-02 -8.32060277e-02
  -4.92165126e-02  7.84259290e-03 -1.18784890e-01 -9.60832909e-02
  -4.92453715e-03  1.44542158e-01  3.30348462e-02  2.81231338e-03
   6.14521280e-02 -7.35903298e-03 -7.54322633e-02  1.10058203e-01
   5.87815009e-02  1.78886037e-02 -4.85782837e-03  1.84458613e-01
   3.11982278e-02 -7.37933293e-02 -7.51596317e-02  1.04695961e-01
  -9.72250253e-02 -9.44643840e-02  1.27530798e-01  1.23021275e-01
  -9.76756811e-02 -8.43207240e-02  6.96085840e-02  1.64856598e-01
   2.96653248e-02 -2.89077275e-02 -1.12501364e-02  2.36267108e-03
  -3.10793705e-02  8.10181573e-02  3.76056321e-02  5.94174117e-02]]

embedding:
[ 0.03765839  0.09021743 -0.001356    0.04076054  0.04601533  0.25682124
  0.03684118  0.04658685 -0.0683746   0.0922796   0.04687139 -0.00272194
  0.01932732 -0.16777565  0.06045137 -0.03307288 -0.02232558  0.12863097
  0.06122964 -0.09006073  0.20338912 -0.05094699 -0.05211756  0.07307947
  0.14153366 -0.03110684 -0.11104943 -0.2103712   0.088107    0.09068976
  0.10696387  0.05845631 -0.07577723  0.04438741  0.10031617 -0.02361435
 -0.01955461 -0.08868567  0.11458483 -0.10992806  0.10672607 -0.12679504
  0.01632918  0.07699546 -0.07913689 -0.12192447  0.11415054 -0.0351057
 -0.14725251 -0.13427286  0.10578448  0.06842157  0.01293649 -0.02879749
  0.04028381  0.08853597  0.04816869 -0.01133396 -0.0159949  -0.16353707
 -0.02181644 -0.07351912  0.09002206 -0.15716557  0.09319755 -0.02052106
  0.03212938 -0.03629737 -0.03515568  0.13036096 -0.03792502  0.10754489
 -0.15451996 -0.11948325 -0.04193863 -0.02881463 -0.07436965  0.11885778
  0.0090537   0.10868978 -0.15199617  0.11014692  0.12235526  0.03885943
  0.03852987 -0.01098366  0.10460863  0.01727468  0.04457604  0.01060722
  0.00488355 -0.04175444 -0.10867393  0.00945349 -0.09279638 -0.11769478
  0.03810817  0.09189356 -0.06156022 -0.0081004   0.08123636  0.08515859
  0.0019427   0.05686275 -0.00857953  0.03230546  0.03530128  0.04284313
  0.0120915  -0.00855714 -0.06190326 -0.03082059 -0.13773248 -0.13991699
  0.18191327  0.00246803 -0.08906183 -0.16354702  0.04687581  0.09188556
  0.11612693 -0.06407943  0.01638488 -0.01842222  0.03551267  0.05930701
  0.13821986  0.0852181 ]

当我尝试在这两者之间执行 cv2.norm 时,出现以下错误:

OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\core\src\norm.cpp:1081: error: (-2:Unspecified error) in function 'double __cdecl cv::norm(const class cv::_InputArray &,const class cv::_InputArray &,int,const class cv::_InputArray &)'
> Input type mismatch (expected: '_src1.type() == _src2.type()'), where
>     '_src1.type()' is 5 (CV_32FC1)
> must be equal to
>     '_src2.type()' is 6 (CV_64FC1)

我在cv2.normndarray方面不是很有经验。任何人都可以帮助并提出一些好的解决方案来计算 euclidean 距离。请帮忙。谢谢

cv2.norm 期望两个参数的形状相同。当您调用该函数时,您的两个输入具有不同的形状。要克服这个问题,您需要将其中一个重塑为与第二个相同的形状。

# vec.shape (1,128) This means vec is a 2d array, with 128 values in ist row
# embedding.shape (128,) This mean embedding is a 1d array of 128 values

embedding = np.reshape(embedding, (1,128))
# embedding.shape (1,128) same as vec
distance = cv2.norm(vec, embedding)