MRPT  2.0.1
test.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include <mrpt/gui.h>
12 #include <mrpt/img/TColor.h>
16 #include <mrpt/vision/TKeyPoint.h>
17 #include <mrpt/vision/types.h>
18 #include <iostream>
19 
20 using namespace std;
21 using namespace mrpt;
22 using namespace mrpt::gui;
23 using namespace mrpt::opengl;
24 using namespace mrpt::obs;
25 using namespace mrpt::vision;
26 using namespace mrpt::img;
27 
28 // ------------------------------------------------------
29 // TestVideoBuildPyr
30 // ------------------------------------------------------
31 void TestVideoBuildPyr()
32 {
33  size_t N_OCTAVES = 4;
34  bool do_smooth = false;
35  bool do_grayscale = false;
36 
37  // Ask for a different number of octaves:
38  cout << "Number of octaves to use [4]: ";
39  {
40  std::string s;
41  std::getline(cin, s);
42  int i = atoi(s.c_str());
43  if (i > 0) N_OCTAVES = i;
44  }
45 
46  // Show to the user a list of possible camera drivers and creates and open
47  // the selected camera.
48  cout << "Please, select the input video file or camera...\n";
49 
52  if (!cam) return;
53 
54  cout << "Video stream open OK\n";
55 
56  // Create 3D window:
57  CDisplayWindow3D win("Demo of pyramid building from live video", 800, 600);
58 
59  // Get the smart pointer to the main viewport object in this window,
60  // and create other viewports for the smaller images:
61  std::vector<COpenGLViewport::Ptr> gl_views(N_OCTAVES);
62  {
63  COpenGLScene::Ptr& theScene = win.get3DSceneAndLock();
64  gl_views[0] = theScene->getViewport("main");
65  ASSERT_(gl_views[0]);
66 
67  // Create the other viewports:
68  for (size_t i = 1; i < N_OCTAVES; i++)
69  gl_views[i] = theScene->createViewport(format("view_%i", (int)i));
70 
71  // Assign sizes:
72  // It can be shown mathematically than if we want all viewports to be
73  // one next to each other
74  // horizontally so they fit to the viewport width (="1") and each is
75  // the half the previous one,
76  // the first one must have a width of 2^(n-1)/(2^n - 1)
77  const double W0 =
78  (double(1 << (N_OCTAVES - 1))) / ((1 << N_OCTAVES) - 1);
79 
80  double X = 0;
81  double W = W0;
82  for (size_t i = 0; i < N_OCTAVES; i++)
83  {
84  COpenGLViewport* vw = gl_views[i].get();
85  vw->setViewportPosition(X, .0, W, 1.);
86  // cout << "Created viewport " << i << " at X=" << X << " with
87  // Width=" << W << endl;
88  X += W;
89  W *= 0.5;
90  }
91 
92  // IMPORTANT!!! IF NOT UNLOCKED, THE WINDOW WILL NOT BE UPDATED!
93  win.unlockAccess3DScene();
94  }
95 
96  win.setPos(10, 10);
97 
98  win.addTextMessage(
99  0.51, 5, // X,Y<=1 means coordinates are factors over the entire
100  // viewport area.
101  "Keys: 's'=Smoothing, 'g': Grayscale 'f': Features",
102  10 // An arbitrary ID
103  );
104 
105  // The image pyramid: Initially empty
106  CImagePyramid imgpyr;
107 
108  cout << "Close the window to end.\n";
109  while (win.isOpen())
110  {
111  win.addTextMessage(5, 5, format("%.02fFPS", win.getRenderingFPS()));
112  std::this_thread::sleep_for(1ms);
113 
114  // Grab new video frame:
115  CObservation::Ptr obs = cam->getNextFrame();
116  if (obs)
117  {
118  if (IS_CLASS(*obs, CObservationImage))
119  {
120  // Get the observation object:
122  std::dynamic_pointer_cast<CObservationImage>(obs);
123 
124  // Update pyramid:
125  imgpyr.buildPyramidFast(
126  o->image, // This image is destroyed since we are calling
127  // the *Fast() version
128  N_OCTAVES, do_smooth, do_grayscale);
129 
130  win.get3DSceneAndLock();
131 
132  for (size_t i = 0; i < N_OCTAVES; i++)
133  {
134  COpenGLViewport* vw = gl_views[i].get();
135  vw->setImageView(imgpyr.images[i]);
136  }
137 
138  win.addTextMessage(
139  0.51, 25, // X,Y<=1 means coordinates are factors over the
140  // entire viewport area.
141  format(
142  "Smooth=%i Grayscale=%i", int(do_smooth ? 1 : 0),
143  int(do_grayscale ? 1 : 0)),
144  11 // An arbitrary ID
145  );
146 
147  win.unlockAccess3DScene();
148  win.repaint();
149  }
150 
151  if (win.keyHit())
152  {
153  mrptKeyModifier kmods;
154  int key = win.getPushedKey(&kmods);
155 
156  if (key == MRPTK_ESCAPE) break;
157 
158  if (key == 's' || key == 'S') do_smooth = !do_smooth;
159  if (key == 'g' || key == 'G') do_grayscale = !do_grayscale;
160  }
161  }
162  }
163 }
164 
165 // ------------------------------------------------------
166 // MAIN
167 // ------------------------------------------------------
168 int main()
169 {
170  try
171  {
173 
174  return 0;
175  }
176  catch (const std::exception& e)
177  {
178  std::cerr << "MRPT error: " << mrpt::exception_to_str(e) << std::endl;
179  return -1;
180  }
181  catch (...)
182  {
183  printf("Untyped exception!!");
184  return -1;
185  }
186 }
Declares a class derived from "CObservation" that encapsules an image from a camera, whose relative pose to robot is also stored.
CCameraSensor::Ptr prepareVideoSourceFromUserSelection()
Show to the user a list of possible camera drivers and creates and open the selected camera...
std::string std::string format(std::string_view fmt, ARGS &&... args)
Definition: format.h:26
void setImageView(const mrpt::img::CImage &img)
Set this viewport into "image view"-mode, where an image is efficiently drawn (fitting the viewport a...
mrptKeyModifier
Definition: keycodes.h:156
STL namespace.
void TestVideoBuildPyr()
A viewport within a COpenGLScene, containing a set of OpenGL objects to render.
Holds and builds a pyramid of images: starting with an image at full resolution (octave=1), it builds a number of half-resolution images: octave=2 at 1/2 , octave=3 at 1/2^2, octave=N at 1/2^(N-1).
Definition: CImagePyramid.h:54
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
std::vector< mrpt::img::CImage > images
The individual images:
Definition: CImagePyramid.h:93
This namespace contains representation of robot actions and observations.
Classes for computer vision, detectors, features, etc.
Definition: CDifodo.h:17
#define IS_CLASS(obj, class_name)
True if the given reference to object (derived from mrpt::rtti::CObject) is of the given class...
Definition: CObject.h:146
mrpt::gui::CDisplayWindow3D::Ptr win
bool buildPyramidFast(mrpt::img::CImage &img, const size_t nOctaves, const bool smooth_halves=true, const bool convert_grayscale=false)
Exactly like buildPyramid(), but if the input image has not to be converted from RGB to grayscale...
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:13
std::string exception_to_str(const std::exception &e)
Builds a nice textual representation of a nested exception, which if generated using MRPT macros (THR...
Definition: exceptions.cpp:59
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:14
void setViewportPosition(const double x, const double y, const double width, const double height)
Change the viewport position and dimension on the rendering window.
A graphical user interface (GUI) for efficiently rendering 3D scenes in real-time.



Page generated by Doxygen 1.8.14 for MRPT 2.0.1 Git: 0fef1a6d7 Fri Apr 3 23:00:21 2020 +0200 at vie abr 3 23:20:28 CEST 2020