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