Ask Your Question

# Calc eucliadian distance between two single point ? I have

Point2f a(10,10);
Point2f b(100,100);


I would like to calc the distance (Euclidean) between these two points. Instead to write the manual function:

float euclideanDist(Point& p, Point& q) {
Point diff = p - q;
return cv::sqrt(diff.x*diff.x + diff.y*diff.y);
}


Is there any OpenCV function? (That's pretty similary to what does descriptormatcher.match() with L2_NORM)

edit retag close merge delete

## Comments

Just a comment about your euclideanDist function: if diff.x (or diff.y) is too big it can overflow and the function will give you an incorrect distance. You can avoid the problem using the C/C++ standard function called hypot or _hypot. For more info see http://www.johndcook.com/blog/2010/06/02/whats-so-hard-about-finding-a-hypotenuse/

## 2 answers

Sort by » oldest newest most voted

Yes there is the norm-function: http://docs.opencv.org/modules/core/doc/operations_on_arrays.html?highlight=cv2.norm#norm.

Example-usage:

Point2f a(10,10);
Point2f b(100,100);
double res = cv::norm(cv::Mat(a),cv::Mat(b));


EDIT: to avoid the creation of Mat-headers:

double res = cv::norm(a-b);

more

## Comments

1

This is overkill and will be slow as you create two matrices just to wrap two points.

1

I agree. you shouldnt create a mat to calc a single norm

1
2

that solution seems to work, but seems to be quite slow?!? Looping computation of cv::norm(a) adding the result to a variable and updating variable a (by adding another point) takes on my computer about 5 times longer (for 9000 < #loops < 1000000000) than computation of sqrt(a.x*a.x + a.y*a.y + a.z*a.z) and doing the same other stuff (in c++ code, didnt check assembler code). Can anyone confirm that and/or explain it (I didnt check cv code either)?

1

cv::norm is optimized via SSE instructions, however the optimizations take only effect if the proper compilerflags are used (sse2). Furthermore, it maybe that these optimizations are more suited to longer vectors, since a point is just 2-dimensional, the optimization-effect may be reduced or be even worse than a normal computation, since additional checks and functions are called. If you are interested, have a look at 'stat.cpp' of OpenCV. just wondered performance comparison of functions and tested.( test code)

comparison results on my computer as below

a : [0, 0] - b : [2.14748e+009, 2.14748e+009]

euclideanDist : 3.037e+009
distanceBtwPoints : 3.037e+009
cv::norm : 3.037e+009

max_distance euclideanDist :     3.02456e+009 time passed :8.7467
max_distance distanceBtwPoints : 3.02456e+009 time passed :16.8687 // from min_enclosing_triangle.cpp
max_distance cv::norm :          3.02456e+009 time passed :19.4353

more

## Comments

cv::norm can't be faster than simple euclideanDist in case of just calculating one point, because the advantage of SSE is only given when vectors are used. So here we have a huge overhead in checking if SSE is available and in the SSE loop that is not used, because the "vector length" is only 1.

I think for one point calculation there is nothing faster than simple euclideanDist.

Official site

GitHub

Wiki

Documentation

## Stats

Asked: 2013-05-29 03:18:04 -0500

Seen: 88,247 times

Last updated: Feb 21 '16