OpenCV and Universal Windows Platform UWP

To get OpenCV running on Windows 10 via an UWP project you have to do the following steps:

  • Download and install
    latest Microsoft Visual Studio 2015 Community (or an earlier version, must support UWP)
    latest OpenCV (this tutorial with 3.4.1)
    latest CMake (this tutorial with 3.1)
  • Open CMake
    CMakeGUI OpenCV
    I unchecked BUILD_EXAMPLES, BUILD_PERF_TESTS, BUILD_TESTS.
    Press configure and select Visual Studio 2015 x64. After that generate the project by pressing ‘Generate’. (If you plan to compile the ARM Version, only static libraries can be build, simply uncheck BUILD_SHARED_LIBS).
  • Open the user defined CMake project folder and open the created project solution (*.sln) file. Build both – the debug and the release version. This should work without and problems. After I built OpenCV I copied the debug and release dlls in a separate bin folder and the libs also in a separate libs folder. Because of the OpenCV separation with the suffix d for debug both (debug and release) can exist in one directory.
  • Create a new Visual Studio 2015 Project and select ‘Blank App (Universal Windows)’Visual Studio 2015 OpenCV Project
  • Download and modify the paths inside the file with your favorite text editor: Visual Studio User Defined Properties. This separate properties file can be modified very easily and can be shared with other projects too.
  • Add the properties file to your current Visual Studio 2015 project.
    Visual Studio 2015 Property Window
    Visual Studio 2015 Properties
    You can do this again for the Debug | x64 too. Simply right click on the build type and select ‘Add Existing Property Sheet’. Choose the opencv_x64.probs file again. A macro inside the file will switch between debug and release configuration automatically.
  • Here is the content of my MainPage.cpp
    //
    // MainPage.xaml.cpp
    // Implementation of the MainPage class.
    //
    #include “pch.h”
    #include “MainPage.xaml.h”
    #include <opencv2\core.hpp>
    #include <opencv2\imgcodecs.hpp>
    #include <opencv2\imgproc.hpp>
    #include <robuffer.h>
    using namespace CVProj;
    using namespace Platform;
    using namespace Windows::Foundation;
    using namespace Windows::Foundation::Collections;
    using namespace Windows::UI::Xaml;
    using namespace Windows::UI::Xaml::Controls;
    using namespace Windows::UI::Xaml::Controls::Primitives;
    using namespace Windows::UI::Xaml::Data;
    using namespace Windows::UI::Xaml::Input;
    using namespace Windows::UI::Xaml::Media;
    using namespace Windows::UI::Xaml::Navigation;
    using namespace Windows::UI::Xaml::Media::Imaging;
    using namespace Windows::Storage::Streams;
    using namespace Microsoft::WRL;
    MainPage::MainPage()
    {
    InitializeComponent();
     cv::Mat input;
    input = cv::imread(“Image file from the ‘Assets’ folder of your project.“);
     cv::Mat img = cv::Mat(input.rows, input.cols, CV_8UC4);
    cv::cvtColor(input, img, CV_BGR2BGRA);
     cv::resize(img, img, cv::Size(640, 480));
     BitmapImage ^bitImg = ref new BitmapImage();
     WriteableBitmap;
    WriteableBitmap^ wbmp = ref new WriteableBitmap(img.cols, img.rows);
    IBuffer^ buffer = wbmp->PixelBuffer;
    unsigned char* dstPixels;
    ComPtr<IBufferByteAccess> pBufferByteAccess;
    ComPtr<IInspectable> pBuffer((IInspectable*)buffer);
    pBuffer.As(&pBufferByteAccess);
    pBufferByteAccess->Buffer(&dstPixels);
    memcpy(dstPixels, img.data, img.step.buf[1] * img.cols * img.rows);
    cvImg->Source = wbmp;
    }
  • Add this entry to your MainPage.xaml file
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Image Name="cvImg"></Image>
    </Grid>
  • Now it should compile without any problems otherwise you do have to check your properties file.
  • You successfully created a Universal Windows Application with OpenCV support!