OpenNI2与OpenCV 3

时间:2016-06-21 14:02:45

标签: python opencv opencv3.0 openni

我在OSX 10.11.5

我一直在尝试使用OpenNI支持构建OpenCV3。我有一个Python脚本应该与XBox 360 Kinect一起使用,但是OpenCV没有检测到Kinect。我安装了libfreenect,我可以运行freenect-glview来调出深度视图。

我已经完成了相当广泛的Google搜索,但所有指南似乎都适用于Windows或Linux。谁能帮我? OpenNI 1指南也可以使用。

提前致谢!

1 个答案:

答案 0 :(得分:1)

我在macOS上成功使用Kinect for Xbox 360。步骤如下:

1)安装OpenNI2

$ brew tap homebrew/science

$ brew install openni2

2)安装libfreenect

  

注意:
  适用于Kinect Xbox 360的libfreenect   适用于Kinect Xbox One的libfreenect2

由于这个问题:Using Microsoft Kinect with Opencv 3.0.0,我们应该自己构建它。

2.1)下载

$ curl -O -L https://github.com/OpenKinect/libfreenect/archive/v0.5.5.tar.gz
$ tar zxf v0.5.5.tar.gz
$ cd libfreenect-0.5.5/

2.2)修复问题

# jump to line 173
$ vi OpenNI2-FreenectDriver/src/DepthStream.hpp

        case XN_STREAM_PROPERTY_ZERO_PLANE_DISTANCE:    // unsigned long long or unsigned int (for OpenNI2/OpenCV)
          if (*pDataSize != sizeof(unsigned long long) && *pDataSize != sizeof(unsigned int))
          {
            LogError("Unexpected size for XN_STREAM_PROPERTY_ZERO_PLANE_DISTANCE");
            return ONI_STATUS_ERROR;
          } else {
            if (*pDataSize != sizeof(unsigned long long)) {
              *(static_cast<unsigned long long*>(data)) = ZERO_PLANE_DISTANCE_VAL;
            } else {
              *(static_cast<unsigned int*>(data)) = (unsigned int) ZERO_PLANE_DISTANCE_VAL;
            }
          }
          return ONI_STATUS_OK;

2.3)构建

$ mkdir build
$ cd build
$ cmake .. -DBUILD_OPENNI2_DRIVER=ON
$ make

2.4)安装

$ export OPENNI2_DIR=$(python -c 'import subprocess, glob; \
prefix = subprocess.check_output("brew --prefix", shell=True); \
print(glob.glob("%s/Cellar/openni2/*" % prefix[:-1])[0])')
$ cd lib/OpenNI2-FreenectDriver/
$ export FREENECT_DRIVER_LIB=libFreenectDriver.dylib
$ export FREENECT_DRIVER_LIB=$(python - <<'EOF'
import os
lib = os.environ['FREENECT_DRIVER_LIB']
while os.path.islink(lib):
    lib = os.readlink(lib)
print(lib)
EOF
)
$ ln -sf `pwd`/$FREENECT_DRIVER_LIB $OPENNI2_DIR/lib/ni2/OpenNI2/Drivers/libFreenectDriver.dylib
$ ln -sf `pwd`/$FREENECT_DRIVER_LIB $OPENNI2_DIR/share/openni2/tools/OpenNI2/Drivers/

2.5)预览

$ cd `brew --prefix`/share/openni2/tools
$ ./NiViewer

3)构建OpenCV

3.1)Python

$ brew install python
$ python --version
Python 2.7.13

$ pip install numpy

3.2)来源

$ git clone https://github.com/opencv/opencv.git
$ cd opencv/
$ git checkout 3.2.0

# if use extra modules
$ git clone https://github.com/opencv/opencv_contrib.git
$ cd opencv_contrib/
$ git checkout 3.2.0

3.3)构建

$ cd opencv/
$ mkdir build
$ cd build/

$ export PY_HOME=/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7
$ export PY_NUMPY_DIR=$(python -c 'import os.path, numpy.core; print(os.path.dirname(numpy.core.__file__))')

$ export OPENNI2_INCLUDE=/usr/local/include/ni2
$ export OPENNI2_REDIST=/usr/local/lib/ni2

$ cmake -DCMAKE_BUILD_TYPE=RELEASE \
-DCMAKE_INSTALL_PREFIX=/usr/local \
\
-DBUILD_opencv_python2=ON \
-DPYTHON2_EXECUTABLE=$PY_HOME/bin/python \
-DPYTHON2_INCLUDE_DIR=$PY_HOME/Headers \
-DPYTHON2_LIBRARY=$PY_HOME/lib \
-DPYTHON2_PACKAGES_PATH=/usr/local/lib/python2.7/site-packages \
-DPYTHON2_NUMPY_INCLUDE_DIRS=$PY_NUMPY_DIR/include/ \
\
-DWITH_OPENNI2=ON \
\
-DBUILD_DOCS=OFF \
-DBUILD_EXAMPLES=OFF \
-DBUILD_TESTS=OFF \
-DBUILD_PERF_TESTS=OFF \
\
-DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
..

$ make -j$(python -c 'import multiprocessing as mp; print(mp.cpu_count())')
$ make install

4)最后

我已将详细信息发布到此gist。此处的示例为:C++Python

  

注意:现在无法使用Python代码检索正确的图像。请参阅此issue