C++

Examples

Name
Description

Help you make sure build environment is setup correctly

How to connect to camera and get camera info

Use OpenCV to display Color, depth and IR images

How to set the ROI of image frame

Get the depth value by pixel coordinate (x,y)

Align depth and RGB frame to the same coordinate

Get the data only in desired distance only

Use range filter to filter out the background

Record camera streams for NiViewer playback

Compute pointcloud data and display

Change resolution, FPS and pixel format

Get camera parameters

Prerequisite

Install cmake and opencv.

Linux:

sudo apt install cmake build-essential
sudo apt install libopencv-dev

Windows:

Install cmake, OpenCV and Visual Studio (we use VS2019).

Installation Guide on Windows

Create two files, example.cpp and CMakeLists.txt with following content.

# CMakeLists.txt
cmake_minimum_required(VERSION 3.3)

project( example CXX )

if(MSVC)
    include_directories("C:/Program Files/OpenNI2/Include")
    link_directories("C:/Program Files/OpenNI2/Lib")
endif()


add_executable( ${PROJECT_NAME} project.cpp )

find_package(OpenCV)

target_link_libraries(${PROJECT_NAME} OpenNI2)
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})
// project_setup.cpp
#include <openni2/OpenNI.h>
#include <opencv2/core/core.hpp>

int main()
{
    auto version = new openni::Version();
    printf("OpenNI Version:%d.%d.%d.%d\n", version->build, version->major, version->minor, version->maintenance);

    printf("OpenCV Version:%s\n", CV_VERSION);
    return 0;
}

Build

Linux

Create a build folder next to CMakeLists.txt. Change directory to build folder. Use cmake to generate build files. Then use make to build.

mkdir build
cd build
cmake ..
make

Windows

1. Open Visual Studio and create a new project

2. Create a new cmake project

3. Modify CMakeLists.txt and project_setup.cpp

Expect output

OpenNI Version:0.0.0.0
OpenCV Version:4.2.0.4.2.0

Last updated