File: tutorial-mb-generic-tracker-stereo.cpp

package info (click to toggle)
visp 3.7.0-7
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 166,380 kB
  • sloc: cpp: 392,705; ansic: 224,448; xml: 23,444; python: 13,701; java: 4,792; sh: 206; objc: 145; makefile: 118
file content (192 lines) | stat: -rw-r--r-- 6,574 bytes parent folder | download | duplicates (3)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//! \example tutorial-mb-generic-tracker-stereo.cpp
#include <cstdlib>
#include <visp3/core/vpConfig.h>
#include <visp3/core/vpIoTools.h>
#include <visp3/gui/vpDisplayFactory.h>
#include <visp3/io/vpImageIo.h>
//! [Include]
#include <visp3/mbt/vpMbGenericTracker.h>
//! [Include]
#include <visp3/io/vpVideoReader.h>

int main(int argc, char **argv)
{
#if defined(VISP_HAVE_OPENCV) && defined(VISP_HAVE_PUGIXML) && defined(VISP_HAVE_DISPLAY)
#ifdef ENABLE_VISP_NAMESPACE
  using namespace VISP_NAMESPACE_NAME;
#endif

#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
  std::shared_ptr<vpDisplay> display_left;
  std::shared_ptr<vpDisplay> display_right;
#else
  vpDisplay *display_left = nullptr;
  vpDisplay *display_right = nullptr;
#endif

  try {
    std::string opt_videoname_left = "teabox_left.mp4";
    std::string opt_videoname_right = "teabox_right.mp4";
    int opt_tracker1 = vpMbGenericTracker::EDGE_TRACKER;
    int opt_tracker2 = vpMbGenericTracker::EDGE_TRACKER;

    for (int i = 1; i < argc; i++) {
      if (std::string(argv[i]) == "--name" && i + 2 < argc) {
        opt_videoname_left = std::string(argv[++i]);
        opt_videoname_right = std::string(argv[++i]);
      }
      else if (std::string(argv[i]) == "--tracker" && i + 2 < argc) {
        opt_tracker1 = atoi(argv[++i]);
        opt_tracker2 = atoi(argv[++i]);
      }
      else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
        std::cout << "\nUsage: " << argv[0]
          << " [--name <video name left> <video name right>]"
          << " [--tracker <1=egde|2=klt|3=hybrid> <1=egde|2=klt|3=hybrid>]"
          << " [--help,-h]\n"
          << std::endl;
        return EXIT_SUCCESS;
      }
    }

    if ((opt_tracker1 < 1 || opt_tracker1 > 3) && (opt_tracker2 < 1 || opt_tracker2 > 3)) {
      std::cerr << "Wrong tracker type. Correct values are: "
        "1=egde|2=keypoint|3=hybrid."
        << std::endl;
      return EXIT_SUCCESS;
    }

    std::string parentname = vpIoTools::getParent(opt_videoname_left);
    std::string objectname_left = vpIoTools::getNameWE(opt_videoname_left);
    std::string objectname_right = vpIoTools::getNameWE(opt_videoname_right);

    if (!parentname.empty()) {
      objectname_left = parentname + "/" + objectname_left;
    }

    std::cout << "Video name: " << opt_videoname_left << " ; " << opt_videoname_right << std::endl;
    std::cout << "Tracker requested config files: " << objectname_left << ".[init, cao]"
      << " and " << objectname_right << ".[init, cao]" << std::endl;
    std::cout << "Tracker optional config files: " << opt_videoname_left << ".ppm"
      << " and " << opt_videoname_right << ".ppm" << std::endl;

//! [Images]
    vpImage<unsigned char> I_left, I_right;
    //! [Images]

    vpVideoReader g_left, g_right;
    g_left.setFileName(opt_videoname_left);
    g_left.open(I_left);
    g_right.setFileName(opt_videoname_right);
    g_right.open(I_right);

#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
    display_left = vpDisplayFactory::createDisplay();
    display_right = vpDisplayFactory::createDisplay();
#else
    display_left = vpDisplayFactory::allocateDisplay();
    display_right = vpDisplayFactory::allocateDisplay();
#endif
    display_left->setDownScalingFactor(vpDisplay::SCALE_AUTO);
    display_right->setDownScalingFactor(vpDisplay::SCALE_AUTO);
    display_left->init(I_left, 100, 100, "Model-based tracker (Left)");
    display_right->init(I_right, 110 + static_cast<int>(I_left.getWidth()), 100, "Model-based tracker (Right)");

//! [Constructor]
    std::vector<int> trackerTypes(2);
    trackerTypes[0] = opt_tracker1;
    trackerTypes[1] = opt_tracker2;
    vpMbGenericTracker tracker(trackerTypes);
    //! [Constructor]

#if !defined(VISP_HAVE_MODULE_KLT)
    unsigned int nbTracker = trackerTypes.size();
    for (unsigned int i = 0; i < nbTracker; ++i) {
      if (trackerTypes[i] >= 2) {
        std::cout << "klt and hybrid model-based tracker are not available "
          "since visp_klt module is missing"
          << std::endl;
        return EXIT_SUCCESS;
      }
    }
#endif

    //! [Load config file]
    tracker.loadConfigFile(objectname_left + ".xml", objectname_right + ".xml");
    //! [Load config file]

    //! [Load cao]
    tracker.loadModel(objectname_left + ".cao", objectname_right + ".cao");
    //! [Load cao]
    //! [Set display features]
    tracker.setDisplayFeatures(true);
    //! [Set display features]

    //! [Set camera transformation matrix]
    vpHomogeneousMatrix cRightMcLeft;
    std::ifstream file_cRightMcLeft("cRightMcLeft.txt");
    cRightMcLeft.load(file_cRightMcLeft);

    std::map<std::string, vpHomogeneousMatrix> mapOfCameraTransformationMatrix;
    mapOfCameraTransformationMatrix["Camera1"] = vpHomogeneousMatrix();
    mapOfCameraTransformationMatrix["Camera2"] = cRightMcLeft;

    tracker.setCameraTransformationMatrix(mapOfCameraTransformationMatrix);
    //! [Set camera transformation matrix]

    //! [Init]
    tracker.initClick(I_left, I_right, objectname_left + ".init", objectname_right + ".init", true);
    //! [Init]

    while (!g_left.end() && !g_right.end()) {
      g_left.acquire(I_left);
      g_right.acquire(I_right);

      vpDisplay::display(I_left);
      vpDisplay::display(I_right);

      //! [Track]
      tracker.track(I_left, I_right);
      //! [Track]

      //! [Get pose]
      vpHomogeneousMatrix cLeftMo, cRightMo;
      tracker.getPose(cLeftMo, cRightMo);
      //! [Get pose]

      //! [Display]
      vpCameraParameters cam_left, cam_right;
      tracker.getCameraParameters(cam_left, cam_right);
      tracker.display(I_left, I_right, cLeftMo, cRightMo, cam_left, cam_right, vpColor::red, 2);
      //! [Display]

      vpDisplay::displayFrame(I_left, cLeftMo, cam_left, 0.025, vpColor::none, 3);
      vpDisplay::displayFrame(I_right, cRightMo, cam_right, 0.025, vpColor::none, 3);
      vpDisplay::displayText(I_left, 10, 10, "A click to exit...", vpColor::red);

      vpDisplay::flush(I_left);
      vpDisplay::flush(I_right);

      if (vpDisplay::getClick(I_left, false)) {
        break;
      }
    }
    vpDisplay::getClick(I_left);
  }
  catch (const vpException &e) {
    std::cerr << "Catch a ViSP exception: " << e.what() << std::endl;
  }
#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
  if (display_left != nullptr) {
    delete display_left;
  }
  if (display_right != nullptr) {
    delete display_right;
  }
#endif
#else
  (void)argc;
  (void)argv;
  std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
#endif
}