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 
12 #include <mrpt/system/CTicTac.h>
13 #include <mrpt/img/CImage.h>
14 #include <iostream>
15 
16 using namespace mrpt;
17 using namespace mrpt::gui;
18 using namespace mrpt::obs;
19 using namespace mrpt::hwdrivers;
20 using namespace mrpt::img;
21 using namespace std;
22 
23 // ------------------------------------------------------
24 // TestCameraCaptureAsk
25 // ------------------------------------------------------
27 {
29 
30  if (!cam)
31  {
32  cerr << "The user didn't pick any camera. Exiting." << endl;
33  return;
34  }
35 
36  CDisplayWindow win("Live video");
37 
38  cout << "Press 's' to save frames.\nClose the window to exit.\n";
39 
40  double counter = 0;
41  mrpt::system::CTicTac tictac;
42 
43  while (win.isOpen())
44  {
45  if (!counter) tictac.Tic();
46 
47  mrpt::obs::CObservation::Ptr obs = cam->getNextFrame();
48  ASSERT_(obs);
49 
50  CImage* img = nullptr;
51 
52  if (IS_CLASS(obs, CObservationImage))
53  {
55  std::dynamic_pointer_cast<CObservationImage>(obs);
56  img = &o->image;
57  }
58  else if (IS_CLASS(obs, CObservationStereoImages))
59  {
61  std::dynamic_pointer_cast<CObservationStereoImages>(obs);
62  img = &o->imageRight;
63  }
64  else if (IS_CLASS(obs, CObservation3DRangeScan))
65  {
67  std::dynamic_pointer_cast<CObservation3DRangeScan>(obs);
68  if (o->hasIntensityImage) img = &o->intensityImage;
69  }
70 
71  if (img) win.showImage(*img);
72 
73  if (++counter == 10)
74  {
75  double t = tictac.Tac();
76  cout << "Frame Rate: " << counter / t << " fps" << endl;
77  counter = 0;
78  }
79 
80  // Process keystrokes:
82  {
83  const int key_code = mrpt::system::os::getch();
84  switch (key_code)
85  {
86  case 's':
87  case 'S':
88  {
89  static int cnt = 0;
90  const std::string sFile =
91  mrpt::format("frame%05i.png", cnt++);
92  cout << "Saving frame to: " << sFile << endl;
93  img->saveToFile(sFile);
94  }
95  break;
96  default:
97  break;
98  };
99  }
100 
101  std::this_thread::sleep_for(2ms);
102  }
103 
104  cout << "Closing..." << endl;
105 }
106 
107 // ------------------------------------------------------
108 // MAIN
109 // ------------------------------------------------------
110 int main()
111 {
112  try
113  {
115 
116  return 0;
117  }
118  catch (std::exception& e)
119  {
120  std::cout << "MRPT exception caught: " << e.what() << std::endl;
121  return -1;
122  }
123  catch (...)
124  {
125  printf("Untyped exception!!");
126  return -1;
127  }
128 }
mrpt::system::os::kbhit
bool kbhit() noexcept
An OS-independent version of kbhit, which returns true if a key has been pushed.
Definition: os.cpp:390
mrpt::obs::CObservation::Ptr
std::shared_ptr< CObservation > Ptr
Definition: CObservation.h:45
t
GLdouble GLdouble t
Definition: glext.h:3689
mrpt::system::CTicTac
A high-performance stopwatch, with typical resolution of nanoseconds.
Definition: system/CTicTac.h:19
mrpt::hwdrivers
Contains classes for various device interfaces.
Definition: C2DRangeFinderAbstract.h:22
CCameraSensor.h
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
mrpt::obs
This namespace contains representation of robot actions and observations.
Definition: CParticleFilter.h:17
mrpt::system::CTicTac::Tac
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:90
main
int main()
Definition: vision_stereo_rectify/test.cpp:78
mrpt::obs::CObservationStereoImages::Ptr
std::shared_ptr< CObservationStereoImages > Ptr
Definition: CObservationStereoImages.h:43
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
mrpt::obs::CObservation3DRangeScan
Declares a class derived from "CObservation" that encapsules a 3D range scan measurement,...
Definition: CObservation3DRangeScan.h:224
win
mrpt::gui::CDisplayWindow3D::Ptr win
Definition: vision_stereo_rectify/test.cpp:31
mrpt::format
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
mrpt::obs::CObservationImage::Ptr
std::shared_ptr< CObservationImage > Ptr
Definition: CObservationImage.h:37
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
mrpt::system::CTicTac::Tic
void Tic() noexcept
Starts the stopwatch.
Definition: CTicTac.cpp:79
mrpt::gui::CDisplayWindow
This class creates a window as a graphical user interface (GUI) for displaying images to the user.
Definition: CDisplayWindow.h:30
mrpt::gui
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:16
mrpt::img::CImage
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:130
img
GLint GLvoid * img
Definition: glext.h:3763
CTicTac.h
mrpt::obs::CObservation3DRangeScan::Ptr
std::shared_ptr< CObservation3DRangeScan > Ptr
Definition: CObservation3DRangeScan.h:226
mrpt::system::os::getch
int getch() noexcept
An OS-independent version of getch, which waits until a key is pushed.
Definition: os.cpp:370
string
GLsizei const GLchar ** string
Definition: glext.h:4101
mrpt::hwdrivers::CCameraSensor::Ptr
std::shared_ptr< CCameraSensor > Ptr
Definition: CCameraSensor.h:351
CImage.h
CDisplayWindow.h
mrpt::obs::CObservationImage
Declares a class derived from "CObservation" that encapsules an image from a camera,...
Definition: CObservationImage.h:35
TestCameraCaptureAsk
void TestCameraCaptureAsk()
Definition: vision_stereo_rectify/test.cpp:26
mrpt::obs::CObservationStereoImages
Observation class for either a pair of left+right or left+disparity images from a stereo camera.
Definition: CObservationStereoImages.h:41



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