Mar*_*ard 13 c++ opencv mathematical-optimization
对于小组项目,我们正在尝试制作游戏,只要玩家在相机前面形成一组特定的手势,就会执行功能.要处理图像,我们使用的是Open-CV 2.3.
在图像处理过程中,我们试图找到两点之间的长度.我们已经知道,毕达哥拉斯定律可以很容易地做到这一点,尽管众所周知,毕达哥拉斯定律需要很多计算机能力,我们希望尽可能减少资源.
我们想知道在Open-CV或C++标准库中是否存在任何内置函数,它可以处理两点之间距离的低资源计算.我们有点的坐标,它们是像素值(当然).
额外信息:以前的经验告诉我们,OpenCV和其他库都经过了大量优化.作为示例,我们尝试使用for循环通过每个像素来更改来自摄像机的实时图像馈送的RGB值.这提供了低帧速率输出.相反,我们决定使用Open-CV内置函数,而不是给我们一个高帧率输出.
Yon*_*son 29
你应该试试这个
cv::Point a(1, 3);
cv::Point b(5, 6);
double res = cv::norm(a-b);//Euclidian distance
Run Code Online (Sandbox Code Playgroud)
Sam*_*Sam 20
正如你正确指出的那样,有一个OpenCV函数可以完成你的一些工作:)
(另请查看另一种方式)
它被称为magnitude(),它为你计算距离.如果你有一个超过4个向量的向量来计算距离,它将使用SSE(我认为)使其更快.
现在,问题是它只计算权力的平方,而你必须通过手的差异来做.(查看文档).但如果你也使用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!
Run Code Online (Sandbox Code Playgroud)
另一种方法是使用非常快的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);
}
Run Code Online (Sandbox Code Playgroud)