dorbird

PointCloudRotation

Dec 14th, 2017
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.28 KB | None | 0 0
  1. Point2f calcCenterOfMass(vector<Point2f> pointCloud) {
  2.     Point2f COM = Point2f(0, 0);
  3.     if (!pointCloud.empty()) {
  4.         for (auto &point : pointCloud)
  5.             COM = COM + point;
  6.         COM = COM / (float) pointCloud.size();
  7.     }
  8.     return COM;
  9. }
  10.  
  11. Point2f normalize(Point2f point){
  12.     float magnitude = sqrt(point.x*point.x+point.y*point.y);
  13.     return point/magnitude;
  14. }
  15.  
  16. float calcRotation(vector<KeyPoint> matched1, vector<KeyPoint> matched2) {
  17.     //make keypoints to Points
  18.     vector<Point2f> pointCloudOne;
  19.     vector<Point2f> pointCloudTwo;
  20.     for (auto &match : matched1)
  21.         pointCloudOne.push_back(match.pt);
  22.     for (auto &match : matched2)
  23.         pointCloudTwo.push_back(match.pt);
  24.     //calc centerofmasses
  25.     Point2f COM1 = calcCenterOfMass(pointCloudOne);
  26.     Point2f COM2 = calcCenterOfMass(pointCloudTwo);
  27.  
  28.     //move second pointcloud to first
  29.     Point2f difference = COM1 - COM2;
  30.     for(auto &point: pointCloudTwo)
  31.         point += difference;
  32.  
  33.     float angle=0;
  34.     for(int i=0; i<pointCloudOne.size(); i++){
  35.         Point2f p1normed = normalize(pointCloudOne.at(i));
  36.         Point2f p2normed = normalize(pointCloudTwo.at(i));
  37.         angle += acosf(p1normed.dot(p2normed));
  38.     }
  39.     return angle/pointCloudOne.size();
  40. }
Advertisement
Add Comment
Please, Sign In to add comment