Ask Your Question
7

Calc eucliadian distance between two single point ?

asked 2013-05-29 03:18:04 -0600

yes123 gravatar image

updated 2020-11-30 02:55:40 -0600

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 flag offensive 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/

uvts_cvs gravatar imageuvts_cvs ( 2013-08-01 11:23:41 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
12

answered 2013-05-29 03:32:46 -0600

Guanta gravatar image

updated 2013-05-30 06:39:27 -0600

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);
edit flag offensive delete link more

Comments

1

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

SR gravatar imageSR ( 2013-05-29 11:14:11 -0600 )edit
1

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

yes123 gravatar imageyes123 ( 2013-05-29 23:06:51 -0600 )edit
1

You are right, I updated a better solution.

Guanta gravatar imageGuanta ( 2013-05-30 06:38:07 -0600 )edit
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)?

Micka gravatar imageMicka ( 2014-01-16 03:54:34 -0600 )edit
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.

Guanta gravatar imageGuanta ( 2014-01-16 05:25:05 -0600 )edit
0

answered 2016-02-21 14:57:33 -0600

updated 2016-02-21 16:57:33 -0600

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
edit flag offensive delete link 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.

matman gravatar imagematman ( 2016-02-22 14:16:16 -0600 )edit

Question Tools

1 follower

Stats

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

Seen: 95,865 times

Last updated: Feb 21 '16