OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Mon, 22 Feb 2016 14:16:16 -0600Calc eucliadian distance between two single point ?http://answers.opencv.org/question/14188/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)Wed, 29 May 2013 03:18:04 -0500http://answers.opencv.org/question/14188/calc-eucliadian-distance-between-two-single-point/Comment by uvts_cvs for <p>I have </p>
<pre><code>Point2f a(10,10);
Point2f b(100,100);
</code></pre>
<p>I would like to calc the distance (Euclidean) between these two points.
Instead to write the manual function:</p>
<pre><code>float euclideanDist(Point& p, Point& q) {
Point diff = p - q;
return cv::sqrt(diff.x*diff.x + diff.y*diff.y);
}
</code></pre>
<p>Is there any OpenCV function? (That's pretty similary to what does descriptormatcher.match() with L2_NORM)</p>
http://answers.opencv.org/question/14188/calc-eucliadian-distance-between-two-single-point/?comment=18024#post-id-18024Just 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/Thu, 01 Aug 2013 11:23:41 -0500http://answers.opencv.org/question/14188/calc-eucliadian-distance-between-two-single-point/?comment=18024#post-id-18024Answer by sturkmen for <p>I have </p>
<pre><code>Point2f a(10,10);
Point2f b(100,100);
</code></pre>
<p>I would like to calc the distance (Euclidean) between these two points.
Instead to write the manual function:</p>
<pre><code>float euclideanDist(Point& p, Point& q) {
Point diff = p - q;
return cv::sqrt(diff.x*diff.x + diff.y*diff.y);
}
</code></pre>
<p>Is there any OpenCV function? (That's pretty similary to what does descriptormatcher.match() with L2_NORM)</p>
http://answers.opencv.org/question/14188/calc-eucliadian-distance-between-two-single-point/?answer=88202#post-id-88202just wondered performance comparison of functions and tested.(
[test code](https://github.com/sturkmen72/opencv_samples/blob/master/14188-calc-eucliadian-distance-between-two-single-point.cpp))
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
Sun, 21 Feb 2016 14:57:33 -0600http://answers.opencv.org/question/14188/calc-eucliadian-distance-between-two-single-point/?answer=88202#post-id-88202Comment by matman for <p>just wondered performance comparison of functions and tested.(
<a href="https://github.com/sturkmen72/opencv_samples/blob/master/14188-calc-eucliadian-distance-between-two-single-point.cpp">test code</a>)</p>
<p>comparison results on my computer as below </p>
<pre><code>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
</code></pre>
http://answers.opencv.org/question/14188/calc-eucliadian-distance-between-two-single-point/?comment=88310#post-id-88310`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.Mon, 22 Feb 2016 14:16:16 -0600http://answers.opencv.org/question/14188/calc-eucliadian-distance-between-two-single-point/?comment=88310#post-id-88310Answer by Guanta for <p>I have </p>
<pre><code>Point2f a(10,10);
Point2f b(100,100);
</code></pre>
<p>I would like to calc the distance (Euclidean) between these two points.
Instead to write the manual function:</p>
<pre><code>float euclideanDist(Point& p, Point& q) {
Point diff = p - q;
return cv::sqrt(diff.x*diff.x + diff.y*diff.y);
}
</code></pre>
<p>Is there any OpenCV function? (That's pretty similary to what does descriptormatcher.match() with L2_NORM)</p>
http://answers.opencv.org/question/14188/calc-eucliadian-distance-between-two-single-point/?answer=14190#post-id-14190Yes there is the norm-function: [http://docs.opencv.org/modules/core/doc/operations_on_arrays.html?highlight=cv2.norm#norm](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);
Wed, 29 May 2013 03:32:46 -0500http://answers.opencv.org/question/14188/calc-eucliadian-distance-between-two-single-point/?answer=14190#post-id-14190Comment by SR for <p>Yes there is the norm-function: <a href="http://docs.opencv.org/modules/core/doc/operations_on_arrays.html?highlight=cv2.norm#norm"></a><a href="http://docs.opencv.org/modules/core/doc/operations_on_arrays.html?highlight=cv2.norm#norm">http://docs.opencv.org/modules/core/doc/operations_on_arrays.html?highlight=cv2.norm#norm</a>. </p>
<p>Example-usage:</p>
<pre><code>Point2f a(10,10);
Point2f b(100,100);
double res = cv::norm(cv::Mat(a),cv::Mat(b));
</code></pre>
<p><strong>EDIT: to avoid the creation of Mat-headers:</strong></p>
<pre><code>double res = cv::norm(a-b);
</code></pre>
http://answers.opencv.org/question/14188/calc-eucliadian-distance-between-two-single-point/?comment=14215#post-id-14215This is overkill and will be slow as you create two matrices just to wrap two points.Wed, 29 May 2013 11:14:11 -0500http://answers.opencv.org/question/14188/calc-eucliadian-distance-between-two-single-point/?comment=14215#post-id-14215Comment by Micka for <p>Yes there is the norm-function: <a href="http://docs.opencv.org/modules/core/doc/operations_on_arrays.html?highlight=cv2.norm#norm"></a><a href="http://docs.opencv.org/modules/core/doc/operations_on_arrays.html?highlight=cv2.norm#norm">http://docs.opencv.org/modules/core/doc/operations_on_arrays.html?highlight=cv2.norm#norm</a>. </p>
<p>Example-usage:</p>
<pre><code>Point2f a(10,10);
Point2f b(100,100);
double res = cv::norm(cv::Mat(a),cv::Mat(b));
</code></pre>
<p><strong>EDIT: to avoid the creation of Mat-headers:</strong></p>
<pre><code>double res = cv::norm(a-b);
</code></pre>
http://answers.opencv.org/question/14188/calc-eucliadian-distance-between-two-single-point/?comment=26570#post-id-26570that 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)?Thu, 16 Jan 2014 03:54:34 -0600http://answers.opencv.org/question/14188/calc-eucliadian-distance-between-two-single-point/?comment=26570#post-id-26570Comment by Guanta for <p>Yes there is the norm-function: <a href="http://docs.opencv.org/modules/core/doc/operations_on_arrays.html?highlight=cv2.norm#norm"></a><a href="http://docs.opencv.org/modules/core/doc/operations_on_arrays.html?highlight=cv2.norm#norm">http://docs.opencv.org/modules/core/doc/operations_on_arrays.html?highlight=cv2.norm#norm</a>. </p>
<p>Example-usage:</p>
<pre><code>Point2f a(10,10);
Point2f b(100,100);
double res = cv::norm(cv::Mat(a),cv::Mat(b));
</code></pre>
<p><strong>EDIT: to avoid the creation of Mat-headers:</strong></p>
<pre><code>double res = cv::norm(a-b);
</code></pre>
http://answers.opencv.org/question/14188/calc-eucliadian-distance-between-two-single-point/?comment=14277#post-id-14277You are right, I updated a better solution.Thu, 30 May 2013 06:38:07 -0500http://answers.opencv.org/question/14188/calc-eucliadian-distance-between-two-single-point/?comment=14277#post-id-14277Comment by Guanta for <p>Yes there is the norm-function: <a href="http://docs.opencv.org/modules/core/doc/operations_on_arrays.html?highlight=cv2.norm#norm"></a><a href="http://docs.opencv.org/modules/core/doc/operations_on_arrays.html?highlight=cv2.norm#norm">http://docs.opencv.org/modules/core/doc/operations_on_arrays.html?highlight=cv2.norm#norm</a>. </p>
<p>Example-usage:</p>
<pre><code>Point2f a(10,10);
Point2f b(100,100);
double res = cv::norm(cv::Mat(a),cv::Mat(b));
</code></pre>
<p><strong>EDIT: to avoid the creation of Mat-headers:</strong></p>
<pre><code>double res = cv::norm(a-b);
</code></pre>
http://answers.opencv.org/question/14188/calc-eucliadian-distance-between-two-single-point/?comment=26578#post-id-26578cv::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.Thu, 16 Jan 2014 05:25:05 -0600http://answers.opencv.org/question/14188/calc-eucliadian-distance-between-two-single-point/?comment=26578#post-id-26578Comment by yes123 for <p>Yes there is the norm-function: <a href="http://docs.opencv.org/modules/core/doc/operations_on_arrays.html?highlight=cv2.norm#norm"></a><a href="http://docs.opencv.org/modules/core/doc/operations_on_arrays.html?highlight=cv2.norm#norm">http://docs.opencv.org/modules/core/doc/operations_on_arrays.html?highlight=cv2.norm#norm</a>. </p>
<p>Example-usage:</p>
<pre><code>Point2f a(10,10);
Point2f b(100,100);
double res = cv::norm(cv::Mat(a),cv::Mat(b));
</code></pre>
<p><strong>EDIT: to avoid the creation of Mat-headers:</strong></p>
<pre><code>double res = cv::norm(a-b);
</code></pre>
http://answers.opencv.org/question/14188/calc-eucliadian-distance-between-two-single-point/?comment=14253#post-id-14253I agree. you shouldnt create a mat to calc a single normWed, 29 May 2013 23:06:51 -0500http://answers.opencv.org/question/14188/calc-eucliadian-distance-between-two-single-point/?comment=14253#post-id-14253