1

在structure官网下载OpenNI的SDK,注意版本对应无误

2

解压后,运行msi文件,安装提示完成安装

3

安装后,环境变量中自动生成了三项内容

4

vs2022中的配置:

我选择了release,x64平台。
需要设置三处地方,如下图所示。

1、VC++——包含目录填写上文提到的环境变量OPENNI_INCLUDE64的路径

2、VC++——库目录填写上文提到的环境变量OPENNI_LIB64的路径

**3、输出——附加依赖项:**填写OpenNI2.lib

5 其他注意点

1、网上有人说要把Redist文件和编译生成的exe放在一个文件夹下,不知道为什么。似乎不进行这步操作也没有影响。保险起见,我也在复制了一份放在里面。

2、程序运行出现如下报错:

Found no files matching 'C:\WINDOWS\SYSTEM32\OpenNI2\Drivers\*.dll'

我直接在System32下自己新建了文件夹,把dll全部复制过去,然后就不会出现这条错误了。

6 测试是否配置成功

test.cpp

#include <opencv2/opencv.hpp>
#include <OpenNI.h>
#include "opencv2/imgproc/types_c.h"
using namespace openni;
using namespace cv;class Grabber
{public:void InitOpenNI();void InitDevice();void InitDepthStream();void InitColorStream();void Run();private:void CapturePsenseDepthFrame();void CapturePsenseColorFrame();cv::Mat ChangeDepthForDisplay(const cv::Mat& mat);openni::Device* device_;openni::VideoStream* depth_stream_;openni::VideoStream* color_stream_;openni::VideoFrameRef* depth_frame_;openni::VideoFrameRef* color_frame_;
};void Grabber::InitOpenNI()
{auto rc = openni::OpenNI::initialize();if (rc != openni::STATUS_OK){printf("Initialize failed\n%s\n", openni::OpenNI::getExtendedError());exit(0);}
}void Grabber::InitDevice()
{device_ = new openni::Device();auto rc = device_->open(openni::ANY_DEVICE);if (rc != openni::STATUS_OK){printf("Couldn't open device\n%s\n", openni::OpenNI::getExtendedError());exit(0);}
}void Grabber::InitDepthStream()
{depth_stream_ = new openni::VideoStream();// Create depth stream from deviceif (device_->getSensorInfo(openni::SENSOR_DEPTH) != nullptr){auto rc = depth_stream_->create(*device_, openni::SENSOR_DEPTH);if (rc != openni::STATUS_OK){printf("Couldn't create depth stream\n%s\n", openni::OpenNI::getExtendedError());exit(0);}}// Get info about depth sensorconst openni::SensorInfo& sensor_info = *device_->getSensorInfo(openni::SENSOR_DEPTH);const openni::Array<openni::VideoMode>& arr = sensor_info.getSupportedVideoModes();// Look for VGA mode in depth sensor and set it for depth streamfor (int i = 0; i < arr.getSize(); ++i){const openni::VideoMode& vmode = arr[i];if (vmode.getPixelFormat() == openni::PIXEL_FORMAT_DEPTH_1_MM &&vmode.getResolutionX() == 640 &&vmode.getResolutionY() == 480){depth_stream_->setVideoMode(vmode);break;}}// Start the depth streamauto rc = depth_stream_->start();if (rc != openni::STATUS_OK){printf("Couldn't start the depth stream\n%s\n", openni::OpenNI::getExtendedError());exit(0);}depth_frame_ = new openni::VideoFrameRef();
}void Grabber::InitColorStream()
{color_stream_ = new openni::VideoStream();if (device_->getSensorInfo(openni::SENSOR_COLOR) != nullptr){auto rc = color_stream_->create(*device_, openni::SENSOR_COLOR);if (rc != openni::STATUS_OK){printf("Couldn't create color stream\n%s\n", openni::OpenNI::getExtendedError());exit(0);}}// Get info about color sensorconst openni::SensorInfo& sensor_info = *device_->getSensorInfo(openni::SENSOR_COLOR);const openni::Array<openni::VideoMode>& arr = sensor_info.getSupportedVideoModes();// Look for VGA mode and set it for color streamfor (int i = 0; i < arr.getSize(); ++i){const openni::VideoMode& vmode = arr[i];if (vmode.getResolutionX() == 640 &&vmode.getResolutionY() == 480){color_stream_->setVideoMode(vmode);break;}}// Note: Doing image registration earlier than this seems to failif (device_->isImageRegistrationModeSupported(openni::IMAGE_REGISTRATION_DEPTH_TO_COLOR)){auto rc = device_->setImageRegistrationMode(openni::IMAGE_REGISTRATION_DEPTH_TO_COLOR);if (rc == openni::STATUS_OK)std::cout << "Depth to color image registration set success\n";elsestd::cout << "Depth to color image registration set failed\n";}else{std::cout << "Depth to color image registration is not supported!!!\n";}// Start color streamauto rc = color_stream_->start();if (rc != openni::STATUS_OK){printf("Couldn't start the depth stream\n%s\n", openni::OpenNI::getExtendedError());exit(0);}color_frame_ = new openni::VideoFrameRef();
}void Grabber::CapturePsenseDepthFrame()
{auto rc = depth_stream_->readFrame(depth_frame_);if (rc != openni::STATUS_OK){printf("Read failed!\n%s\n", openni::OpenNI::getExtendedError());}if (depth_frame_->getVideoMode().getPixelFormat() != openni::PIXEL_FORMAT_DEPTH_1_MM && depth_frame_->getVideoMode().getPixelFormat() != openni::PIXEL_FORMAT_DEPTH_100_UM){printf("Unexpected frame format\n");}// Get pointer to Primesense depth frameopenni::DepthPixel* dev_buf_ptr = (openni::DepthPixel*)depth_frame_->getData();// Copy frame data to OpenCV matcv::Mat depth_mat(depth_frame_->getHeight(), depth_frame_->getWidth(), CV_16U, dev_buf_ptr);cv::Mat disp_mat = ChangeDepthForDisplay(depth_mat);cv::imshow("Depth", disp_mat);
}void Grabber::CapturePsenseColorFrame()
{// Read from stream to frameauto rc = color_stream_->readFrame(color_frame_);if (rc != openni::STATUS_OK){printf("Read failed!\n%s\n", openni::OpenNI::getExtendedError());}// Pointer to Primesense color frameopenni::RGB888Pixel* dev_buf_ptr = (openni::RGB888Pixel*)color_frame_->getData();// Make mat from camera datacv::Mat color_mat(color_frame_->getHeight(), color_frame_->getWidth(), CV_8UC3, dev_buf_ptr);// Convert to BGR format for OpenCVcv::cvtColor(color_mat, color_mat, CV_RGB2BGR);cv::imshow("Color", color_mat);
}void Grabber::Run()
{openni::VideoStream* streams[] = { depth_stream_, color_stream_ };while (true){int readyStream = -1;auto rc = openni::OpenNI::waitForAnyStream(streams, 2, &readyStream, 2000);if (rc != openni::STATUS_OK){printf("Wait failed! (timeout is %d ms)\n%s\n", 2000, openni::OpenNI::getExtendedError());break;}switch (readyStream){case 0:CapturePsenseDepthFrame();break;case 1:CapturePsenseColorFrame();break;default:printf("Unxpected stream\n");}char c = cv::waitKey(10);if ('q' == c)break;}
}cv::Mat Grabber::ChangeDepthForDisplay(const cv::Mat& mat)
{assert(CV_16U == mat.type());const float depth_near = 500;const float depth_far = 5000;const float alpha = 255.0 / (depth_far - depth_near);const float beta = -depth_near * alpha;cv::Mat fmat;mat.convertTo(fmat, CV_32F);for (int r = 0; r < mat.rows; ++r){for (int c = 0; c < mat.cols; ++c){float v = fmat.at<float>(r, c) * alpha + beta;if (v > 255) v = 255;if (v < 0)   v = 0;fmat.at<float>(r, c) = v;}}cv::Mat bmat;fmat.convertTo(bmat, CV_8U);cv::Mat cmat;cv::cvtColor(bmat, cmat, CV_GRAY2BGR);cv::applyColorMap(cmat, cmat, cv::COLORMAP_OCEAN);return cmat;
}int main()
{Grabber grabber;grabber.InitOpenNI();grabber.InitDevice();grabber.InitDepthStream();grabber.InitColorStream();grabber.Run();return 0;
}

运行,没有问题,可以顺利看到摄像头画面。显示depth和color图。

参考资料

https://structure.io/openni
https://documentation.help/OpenNI-2.0/getting_started.html

【学习日记】win64配置openni的vs2022编译环境相关推荐

  1. 配置Abaqus2021 + VS2019 + IVF2020编译环境

    配置Abaqus2021 + VS2019 + IVF2020编译环境 背景介绍 研三需要对Abaqus进行UMAT二次开发,虽然不喜欢Fortran,但是还是需要进行安装. 本文介绍的是我第二次安装 ...

  2. c程序语言设定定义域,4.22C语言学习日记:变量的定义域及编译预处理

    学习笔记: 变量的存储类型: 静态存储方式:在程序运行期间由系统分配固定存储空间:从程序开始执行到程序结束: 动态存储方式:根据需要进行动态的分配存储空间:从包含该变量定义的函数开始执行至函数执行结束 ...

  3. vscode配置C/C++ windows编译环境。

    1. vscode安装c/c++插件 在vscode右边侧边栏的插件扩展中安装c/c++调试插件 2. mingw-w64的安装,推荐直接下载离线版安装 第一种方式:通过自动安装程序指引安装. 安装程 ...

  4. Windows MinGW配置C、C++编译环境

    写在前面的前面:这篇文章vscode和cpp插件版本有点老了,仅供大家参考,最新的和最详细的更新见我的另一篇文章:https://blog.csdn.net/bat67/article/details ...

  5. 配置Mingw64和MSYS2编译环境

    快速配置MSYS2 下载MSYS2 安装 更换国内镜像源 安装新库 下载MSYS2 MSYS2是一组工具和库,提供一个易于使用的环境,用于构建.安装和运行本地 Windows 软件.下载地址:MSYS ...

  6. linux 编译opencl,OpenCL编译环境配置(VS+Nvidia)

    英伟达的显卡首先要下载安装CUDA开发包,可以参考这里的步骤:   VS2015编译环境下CUDA安装配置 安装好CUDA之后,OpenCL的配置就已经完成了80%了,剩下的工作就是把OpenCL的路 ...

  7. python安装c编译的软件_Notepad++配置C/C++、C#、Java、Python编译环境详细教程

    如果只是测试小程序可以用这种方法 比较方便,如果对于大程序建议使用专业的IDE. 经常需要写一些小程序来运行,又不想运行Visual Studio.Eclipse这样的环境,而Notepad++是一个 ...

  8. 为Notepad++配置C/C++、C#、Java、Python编译环境

    本文转自:http://blog.csdn.net/freewaywalker/article/details/8005468 如果只是测试小程序可以用这种方法 比较方便,如果对于大程序建议使用专业的 ...

  9. 【PHP学习】—apache配置虚拟主机(基于域名)

    什么是虚拟主机 虚拟主机是指在网络服务器上分出一定的磁盘空间,用户可以租用此部分空间,以供用户放置站点及应用组件,提供必要的数据存放和传输功能. (简而言之就是服务器) 配置基于域名的虚拟主机 假设我 ...

最新文章

  1. 零基础学习大数据:零基础学习大数据最完整的学习路线
  2. 全球及中国汽车紧急呼叫终端行业运营模式及未来投资方向建议报告2022版
  3. GoldenGate应用拓扑结构(三)
  4. win10一直卡在自动修复_分享:win10自动修复过程中无法正确启动怎么办?
  5. 生成微信蓝色昵称,原来如此简单
  6. 路由算法之距离矢量算法和链路状态算法
  7. 28181之安装SPVMN的视频插件
  8. MySQL 高阶语句
  9. linux opendir路径_Linux目录遍历opendir()
  10. 交换机分布缓存_一种交换机的缓存管理方法
  11. 感应加热计算机仿真软件,一种新型感应加热电源调功方式的研究与计算机仿真...
  12. 蓝桥杯第十届国赛C++研究生组 试题 A: 三升序列
  13. 2020CCFBDCI通用音频分类CNN方案(0.90+方案)
  14. 奥地利和德国的装饰艺术
  15. gmail邮箱延迟收到问题
  16. java乱码 java使用的编码是utf-8还是utf-16还是unicode
  17. 18 副为程序员定制的对联,总有一副适合你...流泪
  18. DMG转ISO文件在windows系统下的多种方法
  19. Oracle 预借差旅费,关于申请出差费用实报的请示-柳州工人医院.DOC
  20. 数据库MySQL存储图片数据

热门文章

  1. 铁轨(UVa 514) 经典数据结构算法,铁轨问题
  2. 5大移动应用加固平台评测
  3. 锐捷防火墙RG-WALL 1600-M6600E授权激活
  4. CSS背景图片,文字设置背景
  5. seetaface6 windows编译
  6. zookeeper以Windows服务安装运行
  7. oracle procmpt,Oracle--表有LONG类型复制或导数报ORA00990
  8. vscode的安装、切换为中文简体、集成sass
  9. 解决java.sql.SQLException: Access denied for user ‘root‘@‘localhost‘ (using password: YES)
  10. maven多模块项目报-BeanCreationException Error creating bean with name