project/rgbd_xtion_cc.cpp
#include <iostream>
#include <algorithm>
#include <fstream>
#include <chrono>
#include <OpenNI.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace openni;
using namespace cv;
void showdevice(){
// 获取设备信息
Array<DeviceInfo> aDeviceList;
OpenNI::enumerateDevices(&aDeviceList);
cout << "电脑上连接着 " << aDeviceList.getSize() << " 个体感设备." << endl;
for (int i = 0; i < aDeviceList.getSize(); ++i)
{
cout << "设备 " << i << endl;
const DeviceInfo& rDevInfo = aDeviceList[i];
cout << "设备名: " << rDevInfo.getName() << endl;
cout << "设备Id: " << rDevInfo.getUsbProductId() << endl;
cout << "供应商名: " << rDevInfo.getVendor() << endl;
cout << "供应商Id: " << rDevInfo.getUsbVendorId() << endl;
cout << "设备URI: " << rDevInfo.getUri() << endl;
}
}
Status initstream(Status& rc, Device& xtion, VideoStream& streamDepth, VideoStream& streamColor)
{
rc = STATUS_OK;
// 创建深度数据流
rc = streamDepth.create(xtion, SENSOR_DEPTH);
if (rc == STATUS_OK)
{
// 设置深度图像视频模式
VideoMode mModeDepth;
// 分辨率大小
mModeDepth.setResolution(640, 480);
// 每秒30帧
mModeDepth.setFps(30);
// 像素格式
mModeDepth.setPixelFormat(PIXEL_FORMAT_DEPTH_1_MM);
streamDepth.setVideoMode(mModeDepth);
streamDepth.setMirroringEnabled(false);
// 打开深度数据流
rc = streamDepth.start();
if (rc != STATUS_OK)
{
cerr << "无法打开深度数据流:" << OpenNI::getExtendedError() << endl;
streamDepth.destroy();
}
}
else
{
cerr << "无法创建深度数据流:" << OpenNI::getExtendedError() << endl;
}
// 创建彩色图像数据流
rc = streamColor.create(xtion, SENSOR_COLOR);
if (rc == STATUS_OK)
{
// 同样的设置彩色图像视频模式
VideoMode mModeColor;
mModeColor.setResolution(640, 480);
mModeColor.setFps(30);
mModeColor.setPixelFormat(PIXEL_FORMAT_RGB888);
streamColor.setVideoMode(mModeColor);
streamColor.setMirroringEnabled(false);
// 打开彩色图像数据流
rc = streamColor.start();
if (rc != STATUS_OK)
{
cerr << "无法打开彩色图像数据流:" << OpenNI::getExtendedError() << endl;
streamColor.destroy();
}
}
else
{
cerr << "无法创建彩色图像数据流:" << OpenNI::getExtendedError() << endl;
}
if (!streamColor.isValid() || !streamDepth.isValid())
{
cerr << "彩色或深度数据流不合法" << endl;
OpenNI::shutdown();
rc = STATUS_ERROR;
return rc;
}
// 图像模式注册,彩色图与深度图对齐
if (xtion.isImageRegistrationModeSupported(
IMAGE_REGISTRATION_DEPTH_TO_COLOR))
{
xtion.setImageRegistrationMode(IMAGE_REGISTRATION_DEPTH_TO_COLOR);
}
return rc;
}
int main(int argc, char **argv)
{
cout << "Openning Xtion ..." << endl;
Status rc = STATUS_OK;
// 初始化OpenNI环境
OpenNI::initialize();
showdevice();
// 声明并打开Device设备。
Device xtion;
const char * deviceURL = openni::ANY_DEVICE; //设备名
rc = xtion.open(deviceURL);
VideoStream streamDepth;
VideoStream streamColor;
if(initstream(rc, xtion, streamDepth, streamColor) == STATUS_OK)
cout << "Open Xtion Successfully!"<<endl;
else
{
cout << "Open Xtion Failed!"<<endl;
return 0;
}
// Main loop
cv::Mat imRGB, imD;
bool continueornot = true;
// 循环读取数据流信息并保存在VideoFrameRef中
VideoFrameRef frameDepth;
VideoFrameRef frameColor;
namedWindow("RGB Image", CV_WINDOW_AUTOSIZE);
for (double index = 1.0; continueornot; index+=1.0)
{
rc = streamDepth.readFrame(&frameDepth);
if (rc == STATUS_OK)
{
imD = cv::Mat(frameDepth.getHeight(), frameDepth.getWidth(), CV_16UC1, (void*)frameDepth.getData());
Mat imD2;
imD.convertTo(imD2, CV_8U, 255.0/(streamDepth.getMaxPixelValue()));
imshow("D2 Image",imD2);imshow("D Image",imD);
}
rc = streamColor.readFrame(&frameColor);
if (rc == STATUS_OK)
{
const Mat tImageRGB(frameColor.getHeight(), frameColor.getWidth(), CV_8UC3, (void*)frameColor.getData());
//tImageRGB.copyTo(imRGB);
cvtColor(tImageRGB, imRGB, CV_RGB2BGR);
imshow("RGB Image",imRGB);
}
char c = cv::waitKey(5);
switch(c)
{
case 'q':
case 27:
continueornot = false;
break;
case 'p':
cv::waitKey(0);
break;
default:
break;
}
}
cv::destroyAllWindows();
return 0;
}
project/cmake_modules/FindOpenNI2.cmake
FIND_PATH(OpenNI2_INCLUDE_DIR
NAMES OpenNI.h
PATHS /usr/include/openni2
)
FIND_LIBRARY(OpenNI2_LIBRARY
NAMES OpenNI2
PATHS /usr/lib
)
#LIST(APPEND OpenNI2_INCLUDE_DIR /usr/include/openni2/Linux-generic)
#LIST(APPEND OpenNI2_LIBRARY /usr/lib/libOpenNI.so)
#SET(OpenNI2_INCLUDE_DIRS ${OpenNI2_INCLUDE_DIR})
#SET(OpenNI2_LIBRARIES ${OpenNI2_LIBRARY})
#SET(OpenNI2_LIBS ${OpenNI2_LIBRARY})
project/CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(RGBDSLAM)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O3 -march=native ")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -O3 -march=native")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
add_definitions(-DCOMPILEDWITHC11)
LIST(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake_modules)
find_package(OpenCV QUIET)
#find_package(Eigen3 3.1.0 REQUIRED)
#find_package(Pangolin REQUIRED)
find_package(OpenNI2 REQUIRED)
include_directories(
#${PROJECT_SOURCE_DIR}
#${PROJECT_SOURCE_DIR}/include
#${EIGEN3_INCLUDE_DIR}
#${Pangolin_INCLUDE_DIRS}
${OpenNI2_INCLUDE_DIR}
${OpenCV_INCLUDE_DIR}
)
add_executable(rgbd_xtion rgbd_xtion_cc.cpp)
target_link_libraries(rgbd_xtion ${OpenNI2_LIBRARY} ${OpenCV_LIBS})