emgucv 求n个点两两之间的距离

思路

  • 在求解N个点两两之间的距离时,matlab里有一个dist函数可以快速求解,emgucv里面没有这个函数,如果采用for循环的方式去求解距离,N非常大时,求解的时间会很长,因此考虑一种加速的方法
  • 对于N X 3的点(这是三维情况,二维平面中的点也可以)的位置矩阵,设其每个点的位置是A(i),则距离矩阵dist中每处的值dist(i,j)=A(i)^2+A(j)^2-2A(i)A(j),所以可以用向量的形式求出三个矩阵,然后再做简单的加减运算即可。

代码

    public static Matrix<double> ComputeMatDist( Matrix<double> pos )
    {
       int num = pos.Rows;
       Matrix<double> dists = new Matrix<double>(num, num);
       Matrix<double> xsquare = new Matrix<double>(num, pos.Cols);
       Matrix<double> xsquareSum = new Matrix<double>(num, 1);

       CvInvoke.Multiply(pos, pos, xsquare);

       for (int i = 0; i < pos.Cols;i++ )
       {
           CvInvoke.Add(xsquare.GetCol(i), xsquareSum, xsquareSum);
       }
       CvInvoke.Repeat(xsquareSum, 1, num, dists);
       dists = dists + dists.Transpose();
       dists -= 2 * pos.Mul(pos.Transpose());
       CvInvoke.Sqrt(dists, dists);
       return dists;
   }

效果

  • 在4000个二维点的情况下,for循环的方式所需时间为100s左右,优化方法只需要800ms
  • 但是当点很多时,对内存要求比较高。。。。
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

littletomatodonkey

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值