Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
OpenNI Cookbook
OpenNI Cookbook

OpenNI Cookbook: Learn how to write NIUI-based applications and motion-controlled games

eBook
€25.99 €28.99
Paperback
€37.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

OpenNI Cookbook

Chapter 2. OpenNI and C++

In this chapter, we will cover:

  • Creating a project in Visual Studio 2010

  • OpenNI class and error handling

  • Enumerating a list of connected devices

  • Accessing video streams (depth/IR/RGB) and configuring them

  • Retrieving a list of supported video modes for depth stream

  • Selecting a specific device for accessing depth stream

  • Listening to the device connect and disconnect events

  • Opening an already recorded file (ONI file) instead of a device

Introduction


In this chapter, we will introduce primary datatypes of the OpenNI and the NiTE along with some basic information about how to access and select a data stream. Then we will try to show you some examples of events triggered by devices such as connecting or disconnecting an OpenNI supported device from computer.

But first, let's get some background about the whole OpenNI's principle first.

The OpenNI object

OpenNI object is the starting point of everything in the framework. Using the OpenNI class we can access a list of connected devices as well as the version of OpenNI itself. Then using this information we can access a device object and read data.

This class uses the singleton pattern, which means there is only one instance of this class and all of its methods are static.

Also in OpenNI 2 we have the ability to register two callback functions by OpenNI object for capturing device connected and device disconnected events.

The device object

Device object is representing the actual physical...

Creating a project in Visual Studio 2010


In this recipe we will show you how to prepare a project in Visual Studio to start programming with OpenNI 2 and NiTE 2. Using NiTE 2 is optional but it will offer following features to you::

  • The capability to track hands and recognize hand gestures

  • The capability to recognize one or more users and track their movements

  • The capability to recognize different parts of the user's body and extract his/her skeleton map and joint positions

Tip

Please note that you don't need NiTE if you want to simply work with depth/IR or Image stream. But if you want to go one step forward and work with the middleware layer, you need to install NiTE and use it.

Without NiTE, we can only use low-level data such as the output of different physical sensors of one or more devices.

Getting ready

Download and install the free version of Visual Studio 2010 Express Edition from the following link:

http://www.microsoft.com/visualstudio/eng/products/visual-studio-2010-express

Please note...

OpenNI class and error handling


We will show how to create and initialize a context object in C++ and how to use the openni::Status datatype to handle errors thrown by OpenNI core. We will use openni::Status very often in the later recipes and it is a very important part of any successful application.

In this recipe, we try to show the version number of the current OpenNI environment and then initialize the OpenNI framework. This will ask OpenNI to search for any connected device and load their modules and drivers.

Getting ready

Create a project in Visual Studio 2010 and prepare it for working with OpenNI using the Creating a project in Visual Studio 2010 recipe in this chapter.

How to do it...

Have a look at the following steps:

  1. Open your project and then the project's main source code file. Locate this line:

    int _tmain(int argc, _TCHAR* argv[])
    {
  2. Write the following code snippet above the preceding line of code:

    char ReadLastCharOfLine()
    {
      int newChar = 0;
      int lastChar;
      fflush(stdout);
    ...

Enumerating a list of connected devices


We learned how to initialize OpenNI and how to retrieve the version number of OpenNI using the openni::OpenNI::getVersion() method and how to ask OpenNI to read all modules and drivers by calling openni::OpenNI::initialize() in the last recipe. In this recipe we will get the list of all the connected devices, their hardware Uri (location), and product ID (that can be used to identify the device).

Getting ready

Create a project in Visual Studio 2010 and prepare it for working with OpenNI using the Creating a project in Visual Studio 2010 recipe in this chapter.

How to do it...

Open your project and then the project's main source code file. Locate this line:

int _tmain(int argc, _TCHAR* argv[])
{

Write the following code snippet above the preceding line of code:

char ReadLastCharOfLine()
{
  int newChar = 0;
  int lastChar;
  fflush(stdout);
  do 
  {
    lastChar = newChar;
    newChar = getchar();
  }
  while ((newChar != '\n') && (newChar != EOF...

Accessing video streams (depth/IR/RGB) and configuring them


In OpenNI 2 there is only one class that is responsible for giving us access to the output of all video-based sensors (depth/IR/RGB) that have made our work very simple compared to the OpenNI 1.x era, where we needed to use three different classes to access sensors. In this recipe we will show you how to access the depth sensor and initialize it. For accessing the IR sensor and RGB sensor we need to follow the same procedure that we will discuss more in the How It Works… section of this recipe. We will show you how to select an output video mode for a sensor too. Also we will show you how to ask a device to see if an output is supported or not.

We will not cover other configurable properties of the openni::VideoStream class including cropping and mirroring in this recipe; read Chapter 4, More about Low-level Outputs about this topic.

Getting ready

Create a project in Visual Studio 2010 and prepare it for working with OpenNI using the...

Retrieving a list of supported video modes for depth stream


From the previous recipe we learned that we can use different video modes for the sensor's output, different resolutions, fps, and pixel formats. We also wrote a table of known supported resolutions and pixel formats. But in case you want to be sure about the list of supported types or if you have a new device and want to know the possible resolutions and pixel formats of one of its sensors, you can easily ask device driver. OpenNI gives us this possibility to retrieve a list of supported video modes for each sensor from the driver itself. In this recipe, we will try to show you how to retrieve a list of supported video modes for the Depth sensor and we will let the user select what video mode to use for our sensor.

Getting ready

Create a project in Visual Studio 2010 and prepare it for working with OpenNI using the Creating a project in Visual Studio 2010 recipe in this chapter.

How to do it...

Have a look at the following steps:

  1. Open...

Selecting a specific device for accessing depth stream


We discussed about how to retrieve a list of connected devices but we never used this data to select one of those devices; we always used ANY_DEVICE as the parameter for the openni::Device::open() method without even asking ourselves what this parameter is.

In this recipe, we will show you how to select your desired device and open it. Then create openni::VideoStream for the depth sensor of this device.

But first let's talk about the openni::Device::open() method. Actually this method has a parameter of type string (actually a character array) containing the hardware location (Uri) of the desired device. We always used ANY_DEVICE as the parameter of this method until now, but actually ANY_DEVICE is equal to null and is used only for better reading of code. When passing null as the parameter of this method, OpenNI automatically selects the first device in the list of loaded and recognized devices. So if we want to select our desired device...

Listening to the device connect and disconnect events


We learned how to get a list of connected devices from OpenNI, but here we want to use one of the new features of OpenNI 2. OpenNI 2 lets us introduce two methods that we want to be executed when there is any new device connected or disconnected. Using this ability we want to define two methods and introduce them to OpenNI as a callback for these two events (connect and disconnect). Our methods will only print a line to the console to show the user what happened, but you can use this feature to wait until the user connects a device or warn the user when a device is disconnected; or you can at least update the list of connected devices without using a timer to refresh it.

Getting ready

Create a project in Visual Studio 2010 and prepare it for working with OpenNI using the Creating a project in Visual Studio 2010 recipe in this chapter.

How to do it...

Have a look at the following steps:

  1. Open your project and then the project's main source code...

Opening an already recorded file (ONI file) instead of a device


Prior to the Selecting a specific device for accessing depth stream recipe of this chapter we used ANY_DEVICE as the parameter of the openni::Device::open() method to open the first device and access its sensors. In the Selecting a specific device for accessing depth stream recipe of this chapter we learned that we can select what device to open by sending its hardware Uri to the openni::OpenNI::open() method. And in this recipe, we will learn another use of the openni::OpenNI::open() method.

You will find out how to record data from different sensors of a device to a file in the later sections of the chapter, but now we want to show how to re-open them as an individual device (just like when we open a physical device) and access saved data streams).

Getting ready

Create a project in Visual Studio 2010 and prepare it for working with OpenNI using the Creating a project in Visual Studio 2010 recipe in this chapter.

You can find a...

Left arrow icon Right arrow icon

Key benefits

  • Use OpenNI for all your needs from games and application UI to low-level data processing or motion detection
  • Learn more about the Natural Interaction features of OpenNI
  • The book is useful for both beginners and professionals because it covers the most basic to advanced concepts in the OpenNi technology.
  • Full of illustrations, examples, and tips for understanding different aspects of topics, with clear step-by-step instructions to get different parts of OpenNI working for you

Description

The release of Microsoft Kinect, then PrimeSense Sensor, and Asus Xtion opened new doors for developers to interact with users, re-design their application’s UI, and make them environment (context) aware. For this purpose, developers need a good framework which provides a complete application programming interface (API), and OpenNI is the first choice in this field. This book introduces the new version of OpenNI. "OpenNI Cookbook" will show you how to start developing a Natural Interaction UI for your applications or games with high level APIs and at the same time access RAW data from different sensors of different hardware supported by OpenNI using low level APIs. It also deals with expanding OpenNI by writing new modules and expanding applications using different OpenNI compatible middleware, including NITE. "OpenNI Cookbook" favors practical examples over plain theory, giving you a more hands-on experience to help you learn. OpenNI Cookbook starts with information about installing devices and retrieving RAW data from them, and then shows how to use this data in applications. You will learn how to access a device or how to read data from it and show them using OpenGL, or use middleware (especially NITE) to track and recognize users, hands, and guess the skeleton of a person in front of a device, all through examples.You also learn about more advanced aspects such as how to write a simple module or middleware for OpenNI itself. "OpenNI Cookbook" shows you how to start and experiment with both NIUI designs and OpenNI itself using examples.

Who is this book for?

If you are a beginner or a professional in NIUI and want to write serious applications or games, then this book is for you. Even OpenNI 1 and OpenNI 1.x programmers who want to move to new versions of OpenNI can use this book as a starting point.This book uses C++ as the primary language but there are some examples in C# and Java too, so you need to have about a basic working knowledge of C or C++ for most cases.

What you will learn

  • Retrieve and use depth, vision, and audio from compatible devices
  • Get basic information about the environment
  • Recognize hands, humans, and their skeleton and track their moves
  • Customize frames right from the device itself
  • Identify basic gestures like pushing or swapping
  • Select between devices or use more than one device to read data
  • Recognize pre-defined hand gestures and detect user poses

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jul 26, 2013
Length: 324 pages
Edition : 1st
Language : English
ISBN-13 : 9781849518475
Languages :
Concepts :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Jul 26, 2013
Length: 324 pages
Edition : 1st
Language : English
ISBN-13 : 9781849518475
Languages :
Concepts :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 104.97
Mastering OpenCV with Practical Computer Vision Projects
€37.99
OpenNI Cookbook
€37.99
Augmented Reality with Kinect
€28.99
Total 104.97 Stars icon

Table of Contents

7 Chapters
Getting Started Chevron down icon Chevron up icon
OpenNI and C++ Chevron down icon Chevron up icon
Using Low-level Data Chevron down icon Chevron up icon
More about Low-level Outputs Chevron down icon Chevron up icon
NiTE and User Tracking Chevron down icon Chevron up icon
NiTE and Hand Tracking Chevron down icon Chevron up icon
NiTE and Skeleton Tracking Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.2
(5 Ratings)
5 star 20%
4 star 80%
3 star 0%
2 star 0%
1 star 0%
Chilijung Dec 07, 2013
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I really love the book very much!I've just started to learn OpenNI recently, but I found out there is lack of documentations and tutorials. In the book the author have teach openNI from the basic to advance level. The book also taught some low-level data usage, that makes me easy to know more about the details.But I wish the book could also teach some installation in Linux or MacOS, cause I'm mostly using these two platforms for my project development, otherwise the book is really great.I'll definitely recommend the book to friends that started openNI.
Amazon Verified review Amazon
carles Feb 17, 2014
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
As a previous user of OpenNI I already knew some of the usage of its API. This book has completed my understanding of both the underlying technology and API.This book is a cookbook on OpenNI (version 2.x), so it is basically a structured list of recipes "How to ...". It is structured to be read in a linear way: it covers from the most basic uses (the inner workings of a depth sensor) to the most high level purposes (skeleton tracking), but every recipe is independent enough to be understood alone.Every recipe is structured in the same way: first the goal description, then the code and finally an explanation. While this structure may be useful for the reader that only wants to read a single recipe, I feel that just printing the code on a book is not the best way to put it. Fortunately, the source code is distributed electronically in the original format, so the reader can read it in an appropriate code editor.I found the introduction about the depth sensor technologies present at the beginning of the book to be extremely useful and interesting. It seemed well documented with links to Wikipedia articles allowing the reader to enhance his knowledge of the field. Introductions in other chapters explain not only the most common settings, but all the options. This is also very useful in order to get the whole picture. I would say that this part, the documentation, is where the value of this book lies.On the other side, I think its coding standards are not the best. Use of vectors, for instance, is not consistent through the book and may lead to confusion. Although I understand that this book may be designed to be suited to beginners, I would have recommended better C++ - centered standards, instead of mixing C idioms; after all the book is centered on the C++ bindings of OpenNI, and not its C API. Coding styles apart, the explanation of the code is good enough.A valuable addition included in the book is a step by step guide to setup the programming environment. Newbies will love this, as it guarantees that they will focus on the problem, and not on the some times over-complex configuration system of the IDE.If you are beginning to use OpenNI or you plan to, and want to get a list of common solutions that you may use that also gives the necessary details to go more in depth, this book is for you.
Amazon Verified review Amazon
Googigo Nov 21, 2013
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
This is the kind of book that would be helpful if you just started developing OpenNI applications in Windows. The code in this book uses the OpenNI2 library, which is the latest version of OpenNI. See more review in the link below.[...]
Amazon Verified review Amazon
SeaLark Jan 16, 2014
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
This book looks great and was certainly needed for all the people interested in OpenNI. That said, go to the OpenNI website Blog and read the comments about the absence of the entire staff and total lack of new support. I only report this since anyone considering basing an expensive development on OpenNI should consider the likelihood that the open source code's future is very dark!
Amazon Verified review Amazon
videoman Nov 15, 2013
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
Nice Book; I am a beginner at OPenNi and found this book useful in getting started and understanding the dev. environment along with the details of the API.In the 'Getting Ready' section, there are plenty of screenshots to get you thru the installation, along with background of both the hardware and software tecnology to start you off in Chapter 1.Chapter 2 continues with the step by step 'How To Do It' instructions and screenshot showing how to setup a new visual studio 2010 project.I like the technique of walking you step by step through a procedure, then summarizing the steps afterwards in the 'How it works' section; each 'recipe' has this format.Chapter 2 continues with examples on how to get OpenNi version info, device info etc along with showing how to capture errors. This chapter and following chapters are chock full of code samples; easily cut and pasted from PDF.Another nice touch is the book explains C++ syntax and constructs (things like Templates for example.The book then dives into the details of frame capture and processing.--Being a beginner at openni, chapter 2 thru 4 were of particular interest to me; working thru some of the examples;The rest of the book goes on to discuss and teach the Nite middleware SDK;The author describes the object model in details, followed by many examples in the 'Getty Ready', 'How to do it' and 'How it works' format.All in all, a great tutorial and reference for getting up and running with OpenNi;About Me: I am a long time software engineer who is interested in the latest enabling technologies; always looking for a good foundation to work upon when using new technologies.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.