OpenNI2 与 OpenCV 3

OpenNI2 with OpenCV 3

我在 OSX 10.11.5

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

我进行了相当广泛的 Google 搜索,但所有指南似乎都是针对 Windows 或 Linux 的。谁能帮我? OpenNI 1 指南也适用。

提前致谢!

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

1) 安装 OpenNI2

$ brew tap homebrew/science

$ brew install openni2

2) 安装libfreenect

Note:
  libfreenect for Kinect Xbox 360
  libfreenect2 for Kinect Xbox One

由于这个问题: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. Samples here: C++, Python

NOTE: Unable to retrieve correct image using Python code now. Please see this issue.