Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
kinectAcquisition.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See https://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Kinect example.
33  *
34 *****************************************************************************/
35 
44 #include <iostream>
45 #include <visp3/core/vpConfig.h>
46 #ifdef VISP_HAVE_LIBFREENECT_AND_DEPENDENCIES
47 
48 #if (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_OPENCV) || defined(VISP_HAVE_GDI))
49 
50 #include <visp3/core/vpImage.h>
51 #include <visp3/core/vpTime.h>
52 #include <visp3/gui/vpDisplayGDI.h>
53 #include <visp3/gui/vpDisplayGTK.h>
54 #include <visp3/gui/vpDisplayOpenCV.h>
55 #include <visp3/gui/vpDisplayX.h>
56 #include <visp3/sensor/vpKinect.h>
57 
58 int main()
59 {
60 #ifdef ENABLE_VISP_NAMESPACE
61  using namespace VISP_NAMESPACE_NAME;
62 #endif
63  try {
64  // Init Kinect
65 #ifdef VISP_HAVE_LIBFREENECT_OLD
66  // This is the way to initialize Freenect with an old version of
67  // libfreenect packages under ubuntu lucid 10.04
68  Freenect::Freenect<vpKinect> freenect;
69  vpKinect &kinect = freenect.createDevice(0);
70 #else
71  Freenect::Freenect freenect;
72  vpKinect &kinect = freenect.createDevice<vpKinect>(0);
73 #endif
74 
75  // Set tilt angle in degrees
76  if (0) {
77  float angle = -3;
78  kinect.setTiltDegrees(angle);
79  }
80 
81  // Init display
82 #if 1
83  kinect.start(vpKinect::DMAP_MEDIUM_RES); // Start acquisition thread with
84  // a depth map resolution of
85  // 480x640
86  vpImage<unsigned char> Idmap(480, 640); // for medium resolution
87  vpImage<float> dmap(480, 640); // for medium resolution
88 #else
89  kinect.start(vpKinect::DMAP_LOW_RES); // Start acquisition thread with a
90  // depth map resolution of 240x320
91  // (default resolution)
92  vpImage<unsigned char> Idmap(240, 320); // for low resolution
93  vpImage<float> dmap(240, 320); // for low resolution
94 #endif
95  vpImage<vpRGBa> Irgb(480, 640), Iwarped(480, 640);
96 
97 #if defined(VISP_HAVE_X11)
98  vpDisplayX display, displayRgb, displayRgbWarped;
99 #elif defined(VISP_HAVE_GTK)
100  vpDisplayGTK display;
101  vpDisplayGTK displayRgb;
102  vpDisplayGTK displayRgbWarped;
103 #elif defined(HAVE_OPENCV_HIGHGUI)
104  vpDisplayOpenCV display;
105  vpDisplayOpenCV displayRgb;
106  vpDisplayOpenCV displayRgbWarped;
107 #elif defined(VISP_HAVE_GDI)
108  vpDisplayGDI display;
109  vpDisplayGDI displayRgb;
110  vpDisplayGDI displayRgbWarped;
111 #endif
112 
113  display.init(Idmap, 100, 200, "Depth map");
114  displayRgb.init(Irgb, 900, 200, "Color Image");
115  displayRgbWarped.init(Iwarped, 900, 700, "Warped Color Image");
116 
117  // A click to stop acquisition
118  std::cout << "Click in one image to stop acquisition" << std::endl;
119 
120  while (!vpDisplay::getClick(Idmap, false) && !vpDisplay::getClick(Irgb, false)) {
121  kinect.getDepthMap(dmap);
122  kinect.getDepthMap(dmap, Idmap);
123  kinect.getRGB(Irgb);
124 
125  vpDisplay::display(Idmap);
126  vpDisplay::flush(Idmap);
127  vpDisplay::display(Irgb);
128  vpDisplay::flush(Irgb);
129 
130  // Warped RGB image:
131  kinect.warpRGBFrame(Irgb, dmap, Iwarped);
132  vpDisplay::display(Iwarped);
133  vpDisplay::flush(Iwarped);
134  }
135  std::cout << "Stop acquisition" << std::endl;
136  kinect.stop(); // Stop acquisition thread
137  return EXIT_SUCCESS;
138  }
139  catch (const vpException &e) {
140  std::cout << "Catch an exception: " << e << std::endl;
141  return EXIT_FAILURE;
142  }
143  catch (...) {
144  std::cout << "Catch an exception " << std::endl;
145  return EXIT_FAILURE;
146  }
147 }
148 
149 #else
150 
151 int main()
152 {
153  std::cout << "You do not have X11, or GDI (Graphical Device Interface), or GTK, or OpenCV functionalities to display "
154  "images..."
155  << std::endl;
156  std::cout << "Tip if you are on a unix-like system:" << std::endl;
157  std::cout << "- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
158  std::cout << "Tip if you are on a windows-like system:" << std::endl;
159  std::cout << "- Install GDI, configure again ViSP using cmake and build again this example" << std::endl;
160  return EXIT_SUCCESS;
161 }
162 #endif
163 
164 #else
165 int main()
166 {
167  std::cout << "You do not have Freenect functionality enabled" << std::endl;
168  std::cout << "Tip if you are on a unix-like system:" << std::endl;
169  std::cout << "- Install libfreenect, configure again ViSP using cmake and build again this example" << std::endl;
170  return EXIT_SUCCESS;
171 }
172 #endif
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:130
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:133
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
error that can be emitted by ViSP classes.
Definition: vpException.h:60
Driver for the Kinect-1 device.
Definition: vpKinect.h:115
void stop()
Definition: vpKinect.cpp:115
void warpRGBFrame(const vpImage< vpRGBa > &Irgb, const vpImage< float > &Idepth, vpImage< vpRGBa > &IrgbWarped)
Definition: vpKinect.cpp:242
bool getDepthMap(vpImage< float > &map)
Definition: vpKinect.cpp:170
void start(vpKinect::vpDMResolution res=DMAP_LOW_RES)
Definition: vpKinect.cpp:75
@ DMAP_LOW_RES
Definition: vpKinect.h:131
@ DMAP_MEDIUM_RES
Definition: vpKinect.h:132
bool getRGB(vpImage< vpRGBa > &IRGB)
Definition: vpKinect.cpp:228