win10(64bit)安装以及vs2013下配置openNI2(32bit)方法

时间:2022-03-15 06:40:01

win10(64bit)安装以及vs2013下配置openNI2(32bit)方法


  • vs2013下配置openNI2

    打开一个工程test,打开test属性,需要更改以下几个方面:

    1.VC++目录选项

    • 包含目录: 添加$(OPENI2_INCLUDE);
    • 库目录$(OPENNI2_LIB);
      win10(64bit)安装以及vs2013下配置openNI2(32bit)方法


      2. c/c++

      • 附加包含目录: 添加$(OPENNI2_INCLUDE);
        win10(64bit)安装以及vs2013下配置openNI2(32bit)方法

    3.链接器
    - 附加库目录:添加$(OPENNI2_LIB);
    win10(64bit)安装以及vs2013下配置openNI2(32bit)方法
    win10(64bit)安装以及vs2013下配置openNI2(32bit)方法


    注意:

    最后一步是把openni2安装路径中的Redist到系统变量path中去,我的是D:\ProgramFiles\openNI\Redist; 这样就可以运行成功
    如果程序编译无错误,在运行时,提示 “无法启动此程序,因为计算机中丢失OpenNI2.dll。尝试重新安装该程序以解决此问题”那么说明上面的redist环境变量未添加成功,按照上面的方法添加即可。


最后附上kinect1在openni2下的代码:

#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <OpenNI.h>
#include "opencv2/core/core.hpp"
#include<opencv2/opencv.hpp>
#include "opencv2/imgproc/imgproc.hpp"

#include <vector>
using namespace std;
using namespace cv;
using namespace openni;

void CheckOpenNIError(Status result, string status)
{
if (result != STATUS_OK)
cerr << status << " Error: " << OpenNI::getExtendedError() << endl;
}

int main(int argc, char** argv)
{
Status result = STATUS_OK;
//OpenNI2 image
VideoFrameRef oniDepthImg;
VideoFrameRef oniColorImg;

//OpenCV image
cv::Mat cvDepthImg;
cv::Mat cvDepthImg1;
cv::Mat cvBGRImg;
cv::Mat cvFusionImg;
cv::namedWindow("depth");
cv::namedWindow("image");
cv::namedWindow("fusion");
char key = 0;

//??1??
// initialize OpenNI2
result = OpenNI::initialize();
CheckOpenNIError(result, "initialize context");

// open device
Device device;
result = device.open(openni::ANY_DEVICE);
//??2??
// create depth stream
VideoStream oniDepthStream;
result = oniDepthStream.create(device, openni::SENSOR_DEPTH);
CheckOpenNIError(result, "create DepthStream");
//??3??
// set depth video mode
VideoMode modeDepth;
modeDepth.setResolution(640, 480);
modeDepth.setFps(30);
modeDepth.setPixelFormat(PIXEL_FORMAT_DEPTH_1_MM);
oniDepthStream.setVideoMode(modeDepth);
// start depth stream
result = oniDepthStream.start();

// create color stream
VideoStream oniColorStream;
result = oniColorStream.create(device, openni::SENSOR_COLOR);
// set color video mode
VideoMode modeColor;
modeColor.setResolution(640, 480);
modeColor.setFps(30);
modeColor.setPixelFormat(PIXEL_FORMAT_RGB888);
oniColorStream.setVideoMode(modeColor);

//??4??
// set depth and color imge registration mode
if (device.isImageRegistrationModeSupported(IMAGE_REGISTRATION_DEPTH_TO_COLOR))
{
device.setImageRegistrationMode(IMAGE_REGISTRATION_DEPTH_TO_COLOR);
}
// start color stream
result = oniColorStream.start();
//int i=1;
//int j=1;
//int n=0;
//char ImagesName1[10000];
//char ImagesName2[10000];

//vector<mat> buff;

while (key != 27)
{
// read frame
if (oniColorStream.readFrame(&oniColorImg) == STATUS_OK)
{
// convert data into OpenCV type
cv::Mat cvRGBImg(oniColorImg.getHeight(), oniColorImg.getWidth(), CV_8UC3, (void*)oniColorImg.getData());
cv::cvtColor(cvRGBImg, cvBGRImg, CV_RGB2BGR);

//sprintf(ImagesName1,"../data/rgb_png/%d.png",i);
//cv::imwrite(ImagesName1,cvBGRImg);
//imencode(".png",cvBGRImg,buff,0);
//i++;
cv::imshow("image", cvBGRImg);
}

if (oniDepthStream.readFrame(&oniDepthImg) == STATUS_OK)
{
cv::Mat cvRawImg16U(oniDepthImg.getHeight(), oniDepthImg.getWidth(), CV_16UC1, (void*)oniDepthImg.getData());
cvRawImg16U.convertTo(cvDepthImg, CV_8U, 255.0 / (oniDepthStream.getMaxPixelValue()));
cvRawImg16U.convertTo(cvDepthImg1, CV_16U, 1);
//??5??
// convert depth image GRAY to BGR
cv::cvtColor(cvDepthImg, cvFusionImg, CV_GRAY2BGR);

//sprintf(ImagesName2,"../data/depth_png/%d.png",j);
//cv::imwrite(ImagesName2,cvRawImg16U);
//j++;
cv::imshow("depth", cvDepthImg);
cv::imshow("depth", 30000 - cvDepthImg1);
}
//??6??

cv::addWeighted(cvBGRImg, 0.5, cvFusionImg, 0.5, 0, cvFusionImg);
cv::imshow("fusion", cvFusionImg);
key = cv::waitKey(20);

}

//cv destroy
cv::destroyWindow("depth");
cv::destroyWindow("image");
cv::destroyWindow("fusion");

//OpenNI2 destroy
oniDepthStream.destroy();
oniColorStream.destroy();
device.close();
OpenNI::shutdown();

return 0;
}