12

I want to calculate the euclidean distance between two vectors (or two Matrx rows, doesn't matter). Is there a good function for that in OpenCV?

farahm
  • 1,326
  • 6
  • 32
  • 70

3 Answers3

28

yes.

Mat a,b; // num of rows/cols/channels does not matter, they just have to be equal for both

double dist = norm(a,b,NORM_L2);
berak
  • 39,159
  • 9
  • 91
  • 89
4

Same for python :

    dist = cv2.norm(pts - dst, cv2.NORM_L2)
ZettaCircl
  • 835
  • 10
  • 15
1

Source: OpenCV, C++: Distance between two points

Mat pts1(nPts, 1, CV_8UC2), pts2(nPts, 1, CV_8UC2);
// populate them
Mat diffPts = pts1-pts2;
Mat ptsx, ptsy;
// split your points in x and y vectors. maybe separate them from start
Mat dist;
magnitude(ptsx, ptsy, dist); // voila!
Community
  • 1
  • 1
rafaoc
  • 586
  • 7
  • 21
  • Why do you need `diffPts` variable? – Scott May 21 '20 at 02:23
  • @Sherzod. This answer was a long time ago that I do not remember exactly. However, what I can see from the code is, after you compute diffPts, you can extract the x part into ptsx and the y part into ptsy. Finally, you can use the magnitude function – rafaoc May 23 '20 at 15:17