Skip to content

Instantly share code, notes, and snippets.

@jinyu121
Created March 29, 2017 12:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jinyu121/6d3e1d42edbb3c8bdf9cf17b7afe2562 to your computer and use it in GitHub Desktop.
Save jinyu121/6d3e1d42edbb3c8bdf9cf17b7afe2562 to your computer and use it in GitHub Desktop.
将oni拆分成RGB图和D图
cmake_minimum_required(VERSION 3.6)
project(onion)
# compile with C++11 support
add_definitions("-std=c++0x -Dlinux -D__STANDALONE_ONI_EXTRACTOR")
# OpenCV
find_package(OpenCV REQUIRED)
# OpenNI
set(OPENNI_INCLUDE_DIRS "$ENV{OPEN_NI_INSTALL_PATH}/usr/include/ni")
include_directories(${OPENNI_INCLUDE_DIRS})
find_library(OPEN_NI_LIBRARY NAMES OpenNI PATHS $ENV{OPEN_NI_INSTALL_PATH}/usr/lib)
SET(LIBS ${LIBS} ${OPEN_NI_LIBRARY})
set(SOURCE_FILES main.cpp)
add_executable(onion ${SOURCE_FILES})
target_link_libraries(onion ${LIBS} ${OpenCV_LIBS})
#include <string>
#include <vector>
#include <chrono>
#include <iostream>
#include <algorithm>
#include <XnCppWrapper.h>
#include <opencv2/opencv.hpp>
#define RETURN_ON_ERROR(status, errmsg) {if (status != XN_STATUS_OK) {std::cerr<<errmsg<<std::endl; return false;}}
int main(int argc, char **argv) {
if (argc < 3) {
std::cerr << "参数1是.oni文件的路径" << std::endl;
std::cerr << "参数2是输出文件夹的路径" << std::endl;
return 1;
}
std::string oni_file_path(argv[1]);
std::string output_dir_name(argv[2]);
xn::Context xContext;
xn::Player xPlayer;
xn::ImageGenerator xImageGenerator;
xn::DepthGenerator xDepthGenerator;
unsigned int numFrames = 0;
// 初始化
RETURN_ON_ERROR(xContext.Init(), "Unable to initialize Context");
RETURN_ON_ERROR(xContext.OpenFileRecording(oni_file_path.c_str(), xPlayer),
"Unable to open file " << oni_file_path);
xPlayer.SetRepeat(static_cast<XnBool >(false));
RETURN_ON_ERROR(xDepthGenerator.Create(xContext), "Unable to create DepthGenerator!");
RETURN_ON_ERROR(xImageGenerator.Create(xContext), "Unable to create ImageGenerator!");
xImageGenerator.SetPixelFormat(XN_PIXEL_FORMAT_RGB24);
RETURN_ON_ERROR(xPlayer.GetNumFrames(xImageGenerator.GetName(), numFrames), "Unable to get number of frames!");
std::cout << "Number of frames: " << numFrames << std::endl;
RETURN_ON_ERROR(xContext.StartGeneratingAll(), "Unable to start generating images!");
const xn::NodeInfo info = xImageGenerator.GetInfo();
// 两个窗口
cv::namedWindow("RGB", CV_WINDOW_AUTOSIZE);
cv::namedWindow("Depth", CV_WINDOW_AUTOSIZE);
// 两个原始数据
xn::ImageMetaData xImageMap;
xn::DepthMetaData xDepthMap;
// 两个Mat
cv::Mat imgRGB;
cv::Mat imgDepth;
// 临时变量
cv::Mat imgRW;
int index = 0;
int numPx = 0;
int xRes = 0, yRes = 0, h = 0, w = 0;
const XnDepthPixel *pDepth = nullptr;
const XnRGB24Pixel *xx = nullptr;
std::vector<cv::Mat> xyd;
char output_file_name[20];
// 逐帧拆开
for (unsigned long int i = 0; i < numFrames; ++i) {
std::cout << i << std::endl;
xImageGenerator.WaitAndUpdateData();
xDepthGenerator.WaitAndUpdateData();
if ((i % 30) != 0) {
continue;
}
// 获取RGB图像
xImageGenerator.GetMetaData(xImageMap);
h = xImageMap.YRes();
w = xImageMap.XRes();
xx = xImageMap.RGB24Data();
imgRGB = cv::Mat(h, w, CV_8UC3, (void *) xx);
cvtColor(imgRGB, imgRGB, CV_BGR2RGB);
// 获取深度图像
xDepthGenerator.GetMetaData(xDepthMap);
xRes = xDepthMap.FullXRes();
yRes = xDepthMap.FullYRes();
pDepth = xDepthMap.Data();
numPx = xRes * yRes;
XnPoint3D ptProj[numPx];
XnPoint3D ptWorld[numPx];
for (int y = 0; y < yRes; ++y) {
for (int x = 0; x < xRes; ++x) {
index = x + y * xRes;
ptProj[index].X = (XnFloat) x;
ptProj[index].Y = (XnFloat) y;
ptProj[index].Z = pDepth[index];
}
}
xDepthGenerator.ConvertProjectiveToRealWorld((XnUInt32) numPx, ptProj, ptWorld);
(cv::Mat(yRes, xRes, CV_32FC3, (void *) ptWorld)).copyTo(imgRW);
cv::split(imgRW, xyd);
imgDepth = xyd.at(2);
imgDepth.convertTo(imgDepth, CV_16U);
// 显示
cv::imshow("RGB", imgRGB);
cv::imshow("Depth", imgDepth);
sprintf(output_file_name, output_dir_name + "/depth/depth_%ld.png", i);
cv::imwrite(output_file_name, imgDepth);
sprintf(output_file_name, output_dir_name + "/rgb/rgb_%ld.png", i);
cv::imwrite(output_file_name, imgDepth);
cv::waitKey(1);
}
// 停止
xContext.StopGeneratingAll();
xContext.Release();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment