demo.cpp
Go to the documentation of this file.
00001 #include <cstdio>
00002 #include <opencv2/core/core.hpp>
00003 #include "RPP.h"
00004 
00005 using namespace cv;
00006 
00007 inline double SQ(double x)
00008 {
00009     return x*x;
00010 }
00011 
00012 int main(int argc, char **argv)
00013 {
00014     // Data from the original Matlab code, we'll use it for ground truth. Verify the results with the Matlab output.
00015     // First row is x
00016     // Second row is y
00017     double model_data[20] = {0.0685, 0.6383, 0.4558, 0.7411, -0.7219, 0.7081, 0.7061, 0.2887, -0.9521, -0.2553,
00018                              0.4636, 0.0159, -0.1010, 0.2817, 0.6638, 0.1582, 0.3925, -0.7954, 0.6965, -0.7795};
00019 
00020 
00021     double iprts_data[20] = {-0.0168, 0.0377, 0.0277, 0.0373, -0.0824, 0.0386, 0.0317, 0.0360, -0.1015, -0.0080,
00022                               0.0866, 0.1179, 0.1233, 0.1035,  0.0667, 0.1102, 0.0969, 0.1660,  0.0622, 0.1608};
00023 
00024 
00025     // Results from Matlab/Octave
00026     // R is rotation and t is translation, you should get VERY similar results, or even numerically exact
00027     /*
00028       R =
00029 
00030      0.85763  -0.31179   0.40898
00031      0.16047  -0.59331  -0.78882
00032      0.48859   0.74214  -0.45881
00033 
00034     t =
00035      -0.10825
00036       1.26601
00037      11.19855
00038     */
00039 
00040     Mat model = Mat::zeros(3, 10, CV_64F); // 3D points, z is zero
00041     Mat iprts = Mat::ones(3, 10, CV_64F); // 2D points, homogenous points
00042     Mat rotation;
00043     Mat translation;
00044     int iterations;
00045     double obj_err;
00046     double img_err;
00047 
00048     for(int i=0; i < 10; i++) {
00049         model.at<double>(0,i) = model_data[i];
00050         model.at<double>(1,i) = model_data[i+10];
00051 
00052         iprts.at<double>(0,i) = iprts_data[i];
00053         iprts.at<double>(1,i) = iprts_data[i+10];
00054     }
00055 
00056     if(!RPP::Rpp(model, iprts, rotation, translation, iterations, obj_err, img_err)) {
00057         fprintf(stderr, "Error with RPP\n");
00058         return 1;
00059     }
00060 
00061         printf("Input normalised image points (using 3x3 camera intrinsic matrix):\n");
00062         for(int i=0; i < 10; i++) {
00063                 printf("%d: (%g, %g)\n", i, iprts_data[i*2], iprts_data[i*2+1]);
00064         }
00065 
00066         printf("\n");
00067         printf("Input model points (fixed 3D points chosen by the user, z is fixed to 0.0):\n");
00068         for(int i=0; i < 10; i++) {
00069                 printf("%d: (%g, %g, 0.0)\n", i, model_data[i*2], model_data[i*2+1]);
00070         }
00071 
00072         printf("\n");
00073         printf("Pose found by RPP\n");
00074 
00075         printf("\n");
00076     printf("Rotation matrix\n");
00077     RPP::Print(rotation);
00078 
00079     printf("Translation matrix\n");
00080     RPP::Print(translation);
00081 
00082     printf("Number of iterations: %d\n", iterations);
00083     printf("Object error: %f\n", obj_err);
00084     printf("Image error: %f\n", img_err);
00085 
00086     // Compare against ground truth
00087     double rot_err = 0.0;
00088     double tran_err = 0.0;
00089 
00090     // Using fabs instead of sum square, in case the signs are opposite
00091     rot_err += SQ(rotation.at<double>(0,0) -  0.85763);
00092     rot_err += SQ(rotation.at<double>(0,1) - -0.31179);
00093     rot_err += SQ(rotation.at<double>(0,2) - 0.40898);
00094     rot_err += SQ(rotation.at<double>(1,0) - 0.16047);
00095     rot_err += SQ(rotation.at<double>(1,1) - -0.59331);
00096     rot_err += SQ(rotation.at<double>(1,2) - -0.78882);
00097     rot_err += SQ(rotation.at<double>(2,0) - 0.48859);
00098     rot_err += SQ(rotation.at<double>(2,1) - 0.74214);
00099     rot_err += SQ(rotation.at<double>(2,2) - -0.45881);
00100 
00101     tran_err += SQ(translation.at<double>(0) - -0.10825);
00102     tran_err += SQ(translation.at<double>(1) - 1.26601);
00103     tran_err += SQ(translation.at<double>(2) - 11.19855);
00104 
00105     printf("\n");
00106 
00107     if(rot_err < 1e-4) {
00108         printf("Rotation results look correct! Square error = %g\n", rot_err);
00109     }
00110     else {
00111         printf("Rotation results does not look correct :( Square error = %g\n", rot_err);
00112     }
00113 
00114     if(tran_err < 1e-4) {
00115         printf("Translation results look correct! Square error = %g\n", tran_err);
00116     }
00117     else {
00118         printf("Translation results does not look correct :( Square error = %g\n", tran_err);
00119     }
00120 }


fiducial_pose
Author(s): Jim Vaughan
autogenerated on Thu Jun 6 2019 18:08:11