MRPT  2.0.4
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 }
mrpt::obs::CObservation::Ptr
std::shared_ptr< CObservation > Ptr
Definition: CObservation.h:45
mrpt::opengl::COpenGLViewport::setImageView
void setImageView(const mrpt::img::CImage &img)
Set this viewport into "image view"-mode, where an image is efficiently drawn (fitting the viewport a...
Definition: COpenGLViewport.cpp:906
CImagePyramid.h
mrpt::vision
Copyright (C) 2010 Hauke Strasdat Imperial College London Copyright (c) 2005-2020,...
Definition: bundle_adjustment.h:35
mrpt::obs::CObservationImage::Ptr
std::shared_ptr< mrpt::obs ::CObservationImage > Ptr
Definition: CObservationImage.h:34
IS_CLASS
#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
CCameraSensor.h
TestVideoBuildPyr
void TestVideoBuildPyr()
Definition: vision_stereo_rectify/test.cpp:31
TKeyPoint.h
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: BaseAppDataSource.h:15
ASSERT_
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
mrpt::obs
This namespace contains representation of robot actions and observations.
Definition: CParticleFilter.h:17
main
int main()
Definition: vision_stereo_rectify/test.cpp:78
mrpt::hwdrivers::prepareVideoSourceFromUserSelection
CCameraSensor::Ptr prepareVideoSourceFromUserSelection()
Show to the user a list of possible camera drivers and creates and open the selected camera.
Definition: CCameraSensor.cpp:1473
mrpt::img
Definition: CCanvas.h:16
COpenGLScene.h
win
mrpt::gui::CDisplayWindow3D::Ptr win
Definition: vision_stereo_rectify/test.cpp:31
CFeatureExtraction.h
mrpt::gui
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:14
mrpt::opengl::COpenGLViewport::setViewportPosition
void setViewportPosition(const double x, const double y, const double width, const double height)
Change the viewport position and dimension on the rendering window.
Definition: COpenGLViewport.cpp:65
mrpt::opengl::COpenGLScene::Ptr
std::shared_ptr< mrpt::opengl ::COpenGLScene > Ptr
Definition: COpenGLScene.h:58
mrpt::gui::MRPTK_ESCAPE
@ MRPTK_ESCAPE
Definition: keycodes.h:27
mrpt::gui::mrptKeyModifier
mrptKeyModifier
Definition: keycodes.h:156
mrpt::opengl::COpenGLViewport
A viewport within a COpenGLScene, containing a set of OpenGL objects to render.
Definition: COpenGLViewport.h:61
gui.h
mrpt::hwdrivers::CCameraSensor::Ptr
std::shared_ptr< CCameraSensor > Ptr
Definition: CCameraSensor.h:353
mrpt::vision::CImagePyramid::images
std::vector< mrpt::img::CImage > images
The individual images:
Definition: CImagePyramid.h:93
mrpt::exception_to_str
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
types.h
mrpt::vision::CImagePyramid
Holds and builds a pyramid of images: starting with an image at full resolution (octave=1),...
Definition: CImagePyramid.h:54
mrpt::opengl
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:13
mrpt::obs::CObservationImage
Declares a class derived from "CObservation" that encapsules an image from a camera,...
Definition: CObservationImage.h:32
mrpt::gui::CDisplayWindow3D
A graphical user interface (GUI) for efficiently rendering 3D scenes in real-time.
Definition: CDisplayWindow3D.h:117
TColor.h
mrpt::format
std::string std::string format(std::string_view fmt, ARGS &&... args)
Definition: format.h:26
mrpt::vision::CImagePyramid::buildPyramidFast
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,...
Definition: CImagePyramid.cpp:66



Page generated by Doxygen 1.8.17 for MRPT 2.0.4 at Fri Jul 17 08:43:33 UTC 2020