Main MRPT website > C++ reference for MRPT 1.9.9
test.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include <mrpt/gui.h>
14 #include <mrpt/vision/types.h>
17 #include <mrpt/img/TColor.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  bool do_features = false;
37 
38  // Ask for a different number of octaves:
39  cout << "Number of octaves to use [4]: ";
40  {
41  std::string s;
42  std::getline(cin, s);
43  int i = atoi(s.c_str());
44  if (i > 0) N_OCTAVES = i;
45  }
46 
47  // Show to the user a list of possible camera drivers and creates and open
48  // the selected camera.
49  cout << "Please, select the input video file or camera...\n";
50 
53  if (!cam) return;
54 
55  cout << "Video stream open OK\n";
56 
57  // Create 3D window:
58  CDisplayWindow3D win("Demo of pyramid building from live video", 800, 600);
59 
60  // Get the smart pointer to the main viewport object in this window,
61  // and create other viewports for the smaller images:
62  std::vector<COpenGLViewport::Ptr> gl_views(N_OCTAVES);
63  {
64  COpenGLScene::Ptr& theScene = win.get3DSceneAndLock();
65  gl_views[0] = theScene->getViewport("main");
66  ASSERT_(gl_views[0]);
67 
68  // Create the other viewports:
69  for (size_t i = 1; i < N_OCTAVES; i++)
70  gl_views[i] = theScene->createViewport(format("view_%i", (int)i));
71 
72  // Assign sizes:
73  // It can be shown mathematically than if we want all viewports to be
74  // one next to each other
75  // horizontally so they fit to the viewport width (="1") and each is
76  // the half the previous one,
77  // the first one must have a width of 2^(n-1)/(2^n - 1)
78  const double W0 =
79  (double(1 << (N_OCTAVES - 1))) / ((1 << N_OCTAVES) - 1);
80 
81  double X = 0;
82  double W = W0;
83  for (size_t i = 0; i < N_OCTAVES; i++)
84  {
85  COpenGLViewport* vw = gl_views[i].get();
86  vw->setViewportPosition(X, .0, W, 1.);
87  // cout << "Created viewport " << i << " at X=" << X << " with
88  // Width=" << W << endl;
89  X += W;
90  W *= 0.5;
91  }
92 
93  // IMPORTANT!!! IF NOT UNLOCKED, THE WINDOW WILL NOT BE UPDATED!
94  win.unlockAccess3DScene();
95  }
96 
97  win.setPos(10, 10);
98 
99  win.addTextMessage(
100  0.51, 5, // X,Y<=1 means coordinates are factors over the entire
101  // viewport area.
102  "Keys: 's'=Smoothing, 'g': Grayscale 'f': Features", TColorf(.8, .8, 0),
103  "sans", 10, // font name & size
105  10 // An arbitrary ID to always overwrite the same, previous 2D text
106  // message
107  );
108 
109  // The image pyramid: Initially empty
110  CImagePyramid imgpyr;
111 
112  cout << "Close the window to end.\n";
113  while (win.isOpen())
114  {
115  win.addTextMessage(5, 5, format("%.02fFPS", win.getRenderingFPS()));
116  std::this_thread::sleep_for(1ms);
117 
118  // Grab new video frame:
119  CObservation::Ptr obs = cam->getNextFrame();
120  if (obs)
121  {
122  if (IS_CLASS(obs, CObservationImage))
123  {
124  // Get the observation object:
126  std::dynamic_pointer_cast<CObservationImage>(obs);
127 
128  // Update pyramid:
129  imgpyr.buildPyramidFast(
130  o->image, // This image is destroyed since we are calling
131  // the *Fast() version
132  N_OCTAVES, do_smooth, do_grayscale);
133 
134  // Also detect features?
135  if (do_features)
136  {
137  static const int threshold = 20;
138 
139  for (unsigned int level = 0; level < N_OCTAVES; ++level)
140  {
141  CImage gray_img(
143 
144  TSimpleFeatureList feats;
145  CFeatureExtraction::detectFeatures_SSE2_FASTER12(
146  gray_img, feats, threshold);
147 
148  imgpyr.images[level].drawFeaturesSimple(
149  feats, TColor::blue());
150  }
151  }
152 
153  win.get3DSceneAndLock();
154 
155  for (size_t i = 0; i < N_OCTAVES; i++)
156  {
157  COpenGLViewport* vw = gl_views[i].get();
158  vw->setImageView(imgpyr.images[i]);
159  }
160 
161  win.addTextMessage(
162  0.51, 25, // X,Y<=1 means coordinates are factors over the
163  // entire viewport area.
164  format(
165  "Smooth=%i Grayscale=%i Features=%i",
166  int(do_smooth ? 1 : 0), int(do_grayscale ? 1 : 0),
167  int(do_features ? 1 : 0)),
168  TColorf(.8, .8, 0), "sans", 10, // font name & size
170  11 // An arbitrary ID to always overwrite the same,
171  // previous 2D text message
172  );
173 
174  win.unlockAccess3DScene();
175  win.repaint();
176  }
177 
178  if (win.keyHit())
179  {
180  mrptKeyModifier kmods;
181  int key = win.getPushedKey(&kmods);
182 
183  if (key == MRPTK_ESCAPE) break;
184 
185  if (key == 's' || key == 'S') do_smooth = !do_smooth;
186  if (key == 'g' || key == 'G') do_grayscale = !do_grayscale;
187  if (key == 'f' || key == 'F') do_features = !do_features;
188  }
189  }
190  }
191 }
192 
193 // ------------------------------------------------------
194 // MAIN
195 // ------------------------------------------------------
196 int main()
197 {
198  try
199  {
201 
202  return 0;
203  }
204  catch (std::exception& e)
205  {
206  std::cout << "MRPT exception caught: " << e.what() << std::endl;
207  return -1;
208  }
209  catch (...)
210  {
211  printf("Untyped exception!!");
212  return -1;
213  }
214 }
mrpt::obs::CObservation::Ptr
std::shared_ptr< CObservation > Ptr
Definition: CObservation.h:45
mrpt::vision::CImagePyramid::buildPyramidFast
void 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:70
format
GLenum GLsizei GLenum format
Definition: glext.h:3531
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:883
s
GLdouble s
Definition: glext.h:3676
CImagePyramid.h
mrpt::vision
Classes for computer vision, detectors, features, etc.
Definition: CCamModel.h:20
CCameraSensor.h
TestVideoBuildPyr
void TestVideoBuildPyr()
Definition: vision_stereo_rectify/test.cpp:31
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
ASSERT_
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:113
level
GLint level
Definition: glext.h:3600
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:1436
mrpt::img
Definition: CCanvas.h:17
COpenGLScene.h
win
mrpt::gui::CDisplayWindow3D::Ptr win
Definition: vision_stereo_rectify/test.cpp:31
TSimpleFeature.h
mrpt::obs::CObservationImage::Ptr
std::shared_ptr< CObservationImage > Ptr
Definition: CObservationImage.h:37
mrpt::img::TColorf
A RGB color - floats in the range [0,1].
Definition: TColor.h:79
IS_CLASS
#define IS_CLASS(ptrObj, class_name)
Evaluates to true if the given pointer to an object (derived from mrpt::rtti::CObject) is of the give...
Definition: CObject.h:103
CFeatureExtraction.h
mrpt::gui
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:16
mrpt::opengl::FILL
@ FILL
renders glyphs as filled polygons
Definition: opengl_fonts.h:38
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:111
mrpt::img::CImage
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:130
mrpt::img::FAST_REF_OR_CONVERT_TO_GRAY
@ FAST_REF_OR_CONVERT_TO_GRAY
Definition: img/CImage.h:51
mrpt::gui::MRPTK_ESCAPE
@ MRPTK_ESCAPE
Definition: keycodes.h:30
mrpt::gui::mrptKeyModifier
mrptKeyModifier
Definition: keycodes.h:159
mrpt::opengl::COpenGLViewport
A viewport within a COpenGLScene, containing a set of OpenGL objects to render.
Definition: COpenGLViewport.h:60
blue
GLclampf GLclampf blue
Definition: glext.h:3525
gui.h
string
GLsizei const GLchar ** string
Definition: glext.h:4101
mrpt::opengl::COpenGLScene::Ptr
std::shared_ptr< COpenGLScene > Ptr
Definition: COpenGLScene.h:61
mrpt::hwdrivers::CCameraSensor::Ptr
std::shared_ptr< CCameraSensor > Ptr
Definition: CCameraSensor.h:351
mrpt::vision::CImagePyramid::images
std::vector< mrpt::img::CImage > images
The individual images:
Definition: CImagePyramid.h:94
types.h
mrpt::vision::CImagePyramid
Holds and builds a pyramid of images: starting with an image at full resolution (octave=1),...
Definition: CImagePyramid.h:57
mrpt::opengl
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
mrpt::obs::CObservationImage
Declares a class derived from "CObservation" that encapsules an image from a camera,...
Definition: CObservationImage.h:35
mrpt::vision::TSimpleFeatureList_templ< TSimpleFeature >
mrpt::gui::CDisplayWindow3D
A graphical user interface (GUI) for efficiently rendering 3D scenes in real-time.
Definition: CDisplayWindow3D.h:117
TColor.h



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST