OpenCV,C++:两点之间的距离

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8269605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:17:32  来源:igfitidea点击:

OpenCV, C++: Distance between two points

c++opencvmathematical-optimization

提问by Marc Pilgaard

For a group project, we are attempting to make a game, where functions are executed whenever a player forms a set of specific hand gestures in front of a camera. To process the images, we are using Open-CV 2.3.

对于一个小组项目,我们正在尝试制作一个游戏,每当玩家在摄像机前形成一组特定的手势时,就会执行功能。为了处理图像,我们使用 Open-CV 2.3。

During the image-processing we are trying to find the length between two points. We already know this can be done very easily with Pythagoras law, though it is known that Pythagoras law requires much computer power, and we wish to do this as low-resource as possible.

在图像处理过程中,我们试图找到两点之间的长度。我们已经知道使用毕达哥拉斯定律可以很容易地做到这一点,尽管众所周知毕达哥拉斯定律需要大量的计算机能力,并且我们希望以尽可能少的资源来做到这一点。

We wish to know if there exist any build-in function within Open-CV or standard library for C++, which can handle low-resource calculations of the distance between two points. We have the coordinates for the points, which are in pixel values (Of course).

我们想知道 Open-CV 或 C++ 标准库中是否有任何内置函数可以处理两点之间距离的低资源计算。我们有点的坐标,它们以像素值表示(当然)。

Extra info: Previous experience have taught us, that OpenCV and other libraries are heavily optimized. As an example, we attempted to change the RGB values of the live image feed from the camera with a for loop, going through each pixel. This provided with a low frame-rate output. Instead we decided to use an Open-CV build-in function instead, which instead gave us a high frame-rate output.

额外信息:以前的经验告诉我们,OpenCV 和其他库都经过了大量优化。例如,我们尝试使用 for 循环更改来自摄像机的实时图像馈送的 RGB 值,遍历每个像素。这提供了低帧率输出。相反,我们决定改用 Open-CV 内置函数,它为我们提供了高帧率输出。

回答by Yonatan Simson

You should try this

你应该试试这个

cv::Point a(1, 3);
cv::Point b(5, 6);
double res = cv::norm(a-b);//Euclidian distance

回答by Sam

As you correctly pointed out, there's an OpenCV function that does some of your work :)

正如您正确指出的那样,有一个 OpenCV 函数可以完成您的一些工作:)

(Also check the other way)

(也检查其他方式

It is called magnitude()and it calculates the distance for you. And if you have a vector of more than 4 vectors to calculate distances, it will use SSE (i think) to make it faster.

它被称为幅度(),它为您计算距离。如果你有一个超过 4 个向量的向量来计算距离,它会使用 SSE(我认为)来使它更快。

Now, the problem is that it only calculate the square of the powers, and you have to do by hand differences. (check the documentation). But if you do them also using OpenCV functions it should be fast.

现在,问题是它只计算幂的平方,而您必须手动计算差异。(检查文档)。但是如果你也使用 OpenCV 函数来做它们,它应该很快。

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!

The other wayis to use a very fast sqrt:

另一种方法是使用非常快的 sqrt:

// 15 times faster than the classical float sqrt. 
// Reasonably accurate up to root(32500)
// Source: http://supp.iar.com/FilesPublic/SUPPORT/000419/AN-G-002.pdf

unsigned int root(unsigned int x){
    unsigned int a,b;
    b     = x;
    a = x = 0x3f;
    x     = b/x;
    a = x = (x+a)>>1;
    x     = b/x;
    a = x = (x+a)>>1;
    x     = b/x;
    x     = (x+a)>>1;
    return(x);  
}

回答by kebs

This ought to a comment, but I haven't enough rep (50?) |-( so I post it as an answer.

这应该发表评论,但我没有足够的代表(50?)|-(所以我将其发布为答案。

What the guys are trying to tell you in the comments of your questions is that if it's only about comparingdistances, then you can simply use d=(dx*dx+dy*dy) = (x1-x2)(x1-x2) + (y1-y2)(y1-y2) thus avoiding the square root. But you can't of course skip the square elevation.

这些家伙在你的问题的评论中试图告诉你的是,如果它只是关于比较距离,那么你可以简单地使用 d=(dx*dx+dy*dy) = (x1-x2) (x1-x2) + (y1-y2)(y1-y2) 从而避免平方根。但是您当然不能跳过方形高程。

回答by Hannesh

Pythagoras is the fastest way, and it really isn't as expensive as you think. It used to be, because of the square-root. But modern processors can usually do this within a few cycles.

毕达哥拉斯是最快的方法,而且它真的没有你想象的那么贵。它曾经是,因为平方根。但是现代处理器通常可以在几个周期内做到这一点。

If you really need speed, use OpenCL on the graphics card for image processing.

如果您确实需要速度,请使用显卡上的 OpenCL 进行图像处理。