2 minute read

You can run computer vision algorithms written in C++ supported by the newest OpenCV library directly on the HoloLens 2. One way to do this is via a C++/WinRT Plugin, which can be integrated with a Unity App.

To use OpenCV within this Plugin, you will need a build of OpenCV that is compatible with the HoloLens 2, i.e., with ARM64 and Universal Windows Platform (UWP). This tutorial will cover how to compile OpenCV for ARM64/UWP and the HoloLens 2.

Get the tools

  • Download the required OpenCV release from the opencv GitHub and extract the folder.
  • Clone the OpenCV contrib from the opencv_contrib GitHub.
  • Download and install Cmake.
  • Download and install Visual Studio (Community version is fine).
    • Make sure to install the following workloads:
      • Desktop development with C++
      • Universal Windows Platform development -> including a Windows 10 SDK
      • .NET desktop development
      • Game development with Unity (if you want to use Unity)
    • Also, the following individual components should be included:
      • USB Device Connectivity
      • C++/WinRT (pre-installed when using VS2022)
      • Visual C++ compilers and libraries for ARM64/x86
      • C++ Universal Windows Platform tools for ARM64

Generate the build files using Cmake

  • Create a new folder for your build, e.g., OpenCV_4.8.0_UWP_ARM64_build.
  • Open Cmake Gui.
  • Set “Where is the source code” in Cmake Gui to the path of your OpenCV release, i.e., to opencv-4.8.0.
  • Set “Where to build the binaries” to your build folder, i.e. to OpenCV_4.8.0_UWP_ARM64_build.

  • Click on “Add Entry” and the following values as STRING:
    • CMAKE_SYSTEM_PROCESSOR=aarch64
    • CMAKE_SYSTEM_VERSION=10.0
    • CMAKE_SYSTEM_NAME=WindowsStore

  • Click on “Configure”. In the prompt, select your Visual Studio compiler as generator, and “ARM64” as optional platform. The geneneration will take some time.

  • Now, it is time to add the contrib modules. Locate the OPENCV_EXTRA_MODULES_PATH in the Cmake variables, and set it to the path of your opencv_contrib/modules folder.
  • Click on “Configure” again.
  • Not all OpenCV modules are compatible with UWP/ARM64. You need to disable them by de-selecting the checkmark next to them. Following modules are known to cause problems:
    • BUILD_opencv_videoio; BUILD_opencv_python_bindings_generator; BUILD_opencv_python_java_generator; BUILD_opencv_ts; BUILD_opencv_hdf; BUILD_opencv_xfeatures2d; BUILD_opencv_js; BUILD_opencv_dnn_objdetect; BUILD_opencv_gapi, BUILD_opencv_saliency, BUILD_opencv_line_descriptor, BUILD_opencv_bgsegm; BUILD_opencv_rgbd, BUILD_opencv_tracking
    • [Optional] You may disable the BUILD_TESTS and BUILD_PERF_TESTS. This leads to a faster build & install
  • Click “Configure” again. Configuration should complete without any errors. In the following summary, check for “Windows RT support: YES”

    • If you still encounter errors during the configuration, you may need to disable additional modules.
  • Finally, click on “Generate”. There should be no errors during generation.

Build and install using Visual Studio

  • In your build folder OpenCV_4.8.0_UWP_ARM64_build, locate the OpenCV.sln solution and open it in Visual Studio.
  • Make sure your configuration is set to “Release” and platform to “ARM64”.

  • Under CMakeTargets, locate the INSTALL project, right click and click on “Build”.

  • After the build finishes, find the build files in OpenCV_4.8.0_UWP_ARM64_build/install/ARM64/vcxx/!