File: recon2v.cpp

package info (click to toggle)
opencv 3.2.0%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 238,480 kB
  • sloc: xml: 901,650; cpp: 703,419; lisp: 20,142; java: 17,843; python: 17,641; ansic: 603; cs: 601; sh: 516; perl: 494; makefile: 117
file content (126 lines) | stat: -rw-r--r-- 3,126 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <opencv2/sfm.hpp>
#include <opencv2/core.hpp>
#include <opencv2/viz.hpp>

#include <iostream>
#include <fstream>
#include <string>

using namespace std;
using namespace cv;
using namespace cv::sfm;

static void help() {
  cout
    << "\n------------------------------------------------------------------\n"
    << " This program shows the two view reconstruction capabilities in the \n"
    << " OpenCV Structure From Motion (SFM) module.\n"
    << " It uses the following data from the VGG datasets at ...\n"
    << " Usage:\n"
    << "       reconv2_pts.txt \n "
    << " where the first line has the number of points and each subsequent \n"
    << " line has entries for matched points as: \n"
    << " x1 y1 x2 y2 \n"
    << "------------------------------------------------------------------\n\n"
    << endl;
}

int main(int argc, char** argv)
{
  // Do projective reconstruction
  bool is_projective = true;

  // Read 2D points from text file

  Mat_<double> x1, x2;
  int npts;

  if (argc < 2) {
    help();
    exit(0);
  } else {
    ifstream myfile(argv[1]);
    if (!myfile.is_open()) {
      cout << "Unable to read file: " << argv[1] << endl;
      exit(0);

    } else {
      string line;

      // Read number of points
      getline(myfile, line);
      npts = (int) atof(line.c_str());

      x1 = Mat_<double>(2, npts);
      x2 = Mat_<double>(2, npts);

      // Read the point coordinates
      for (int i = 0; i < npts; ++i) {
        getline(myfile, line);
        stringstream s(line);
        string cord;

        s >> cord;
        x1(0, i) = atof(cord.c_str());
        s >> cord;
        x1(1, i) = atof(cord.c_str());

        s >> cord;
        x2(0, i) = atof(cord.c_str());
        s >> cord;
        x2(1, i) = atof(cord.c_str());

      }

      myfile.close();

    }
  }

  // Call the reconstruction function

  std::vector < Mat_<double> > points2d;
  points2d.push_back(x1);
  points2d.push_back(x2);
  Matx33d K_estimated;
  Mat_<double> points3d_estimated;
  std::vector < cv::Mat > Ps_estimated;

  reconstruct(points2d, Ps_estimated, points3d_estimated, K_estimated, is_projective);


  // Print output

  cout << endl;
  cout << "Projection Matrix of View 1: " << endl;
  cout << "============================ " << endl;
  cout << Ps_estimated[0] << endl << endl;
  cout << "Projection Matrix of View 2: " << endl;
  cout << "============================ " << endl;
  cout << Ps_estimated[1] << endl << endl;


  // Display 3D points using VIZ module

  // Create the pointcloud
  std::vector<cv::Vec3f> point_cloud;
  for (int i = 0; i < npts; ++i) {
    cv::Vec3f point3d((float) points3d_estimated(0, i),
                      (float) points3d_estimated(1, i),
                      (float) points3d_estimated(2, i));
    point_cloud.push_back(point3d);
  }

  // Create a 3D window
  viz::Viz3d myWindow("Coordinate Frame");

  /// Add coordinate axes
  myWindow.showWidget("Coordinate Widget", viz::WCoordinateSystem());

  viz::WCloud cloud_widget(point_cloud, viz::Color::green());

  myWindow.showWidget("cloud", cloud_widget);
  myWindow.spin();

  return 0;
}