demo.cpp
Go to the documentation of this file.
1 #include <cstdio>
2 #include <opencv2/core/core.hpp>
3 #include "RPP.h"
4 
5 using namespace cv;
6 
7 inline double SQ(double x)
8 {
9  return x*x;
10 }
11 
12 int main(int argc, char **argv)
13 {
14  // Data from the original Matlab code, we'll use it for ground truth. Verify the results with the Matlab output.
15  // First row is x
16  // Second row is y
17  double model_data[20] = {0.0685, 0.6383, 0.4558, 0.7411, -0.7219, 0.7081, 0.7061, 0.2887, -0.9521, -0.2553,
18  0.4636, 0.0159, -0.1010, 0.2817, 0.6638, 0.1582, 0.3925, -0.7954, 0.6965, -0.7795};
19 
20 
21  double iprts_data[20] = {-0.0168, 0.0377, 0.0277, 0.0373, -0.0824, 0.0386, 0.0317, 0.0360, -0.1015, -0.0080,
22  0.0866, 0.1179, 0.1233, 0.1035, 0.0667, 0.1102, 0.0969, 0.1660, 0.0622, 0.1608};
23 
24 
25  // Results from Matlab/Octave
26  // R is rotation and t is translation, you should get VERY similar results, or even numerically exact
27  /*
28  R =
29 
30  0.85763 -0.31179 0.40898
31  0.16047 -0.59331 -0.78882
32  0.48859 0.74214 -0.45881
33 
34  t =
35  -0.10825
36  1.26601
37  11.19855
38  */
39 
40  Mat model = Mat::zeros(3, 10, CV_64F); // 3D points, z is zero
41  Mat iprts = Mat::ones(3, 10, CV_64F); // 2D points, homogenous points
42  Mat rotation;
43  Mat translation;
44  int iterations;
45  double obj_err;
46  double img_err;
47 
48  for(int i=0; i < 10; i++) {
49  model.at<double>(0,i) = model_data[i];
50  model.at<double>(1,i) = model_data[i+10];
51 
52  iprts.at<double>(0,i) = iprts_data[i];
53  iprts.at<double>(1,i) = iprts_data[i+10];
54  }
55 
56  if(!RPP::Rpp(model, iprts, rotation, translation, iterations, obj_err, img_err)) {
57  fprintf(stderr, "Error with RPP\n");
58  return 1;
59  }
60 
61  printf("Input normalised image points (using 3x3 camera intrinsic matrix):\n");
62  for(int i=0; i < 10; i++) {
63  printf("%d: (%g, %g)\n", i, iprts_data[i*2], iprts_data[i*2+1]);
64  }
65 
66  printf("\n");
67  printf("Input model points (fixed 3D points chosen by the user, z is fixed to 0.0):\n");
68  for(int i=0; i < 10; i++) {
69  printf("%d: (%g, %g, 0.0)\n", i, model_data[i*2], model_data[i*2+1]);
70  }
71 
72  printf("\n");
73  printf("Pose found by RPP\n");
74 
75  printf("\n");
76  printf("Rotation matrix\n");
77  RPP::Print(rotation);
78 
79  printf("Translation matrix\n");
80  RPP::Print(translation);
81 
82  printf("Number of iterations: %d\n", iterations);
83  printf("Object error: %f\n", obj_err);
84  printf("Image error: %f\n", img_err);
85 
86  // Compare against ground truth
87  double rot_err = 0.0;
88  double tran_err = 0.0;
89 
90  // Using fabs instead of sum square, in case the signs are opposite
91  rot_err += SQ(rotation.at<double>(0,0) - 0.85763);
92  rot_err += SQ(rotation.at<double>(0,1) - -0.31179);
93  rot_err += SQ(rotation.at<double>(0,2) - 0.40898);
94  rot_err += SQ(rotation.at<double>(1,0) - 0.16047);
95  rot_err += SQ(rotation.at<double>(1,1) - -0.59331);
96  rot_err += SQ(rotation.at<double>(1,2) - -0.78882);
97  rot_err += SQ(rotation.at<double>(2,0) - 0.48859);
98  rot_err += SQ(rotation.at<double>(2,1) - 0.74214);
99  rot_err += SQ(rotation.at<double>(2,2) - -0.45881);
100 
101  tran_err += SQ(translation.at<double>(0) - -0.10825);
102  tran_err += SQ(translation.at<double>(1) - 1.26601);
103  tran_err += SQ(translation.at<double>(2) - 11.19855);
104 
105  printf("\n");
106 
107  if(rot_err < 1e-4) {
108  printf("Rotation results look correct! Square error = %g\n", rot_err);
109  }
110  else {
111  printf("Rotation results does not look correct :( Square error = %g\n", rot_err);
112  }
113 
114  if(tran_err < 1e-4) {
115  printf("Translation results look correct! Square error = %g\n", tran_err);
116  }
117  else {
118  printf("Translation results does not look correct :( Square error = %g\n", tran_err);
119  }
120 }
int main(int argc, char **argv)
Definition: demo.cpp:12
double SQ(double x)
Definition: demo.cpp:7
bool Rpp(const cv::Mat &model_3D, const cv::Mat &iprts, cv::Mat &Rlu, cv::Mat &tlu, int &it1, double &obj_err1, double &img_err1, std::vector< Solution > &sol)
void Print(const cv::Mat &m)


fiducial_pose
Author(s): Jim Vaughan
autogenerated on Thu Dec 28 2017 04:06:58