Ask Your Question

Revision history [back]

Opening an OpenCV window in a Windows Runtime component (C++) for a UWP app

I'm stuck with some linker errors when trying to call the imshow function in my WinRT component for a UWP app. The component simply calls imshow after a type conversion on the file name string, which is supposed to show the image in a new window.

This is a proof-of-concept to show that OpenCV can be used within a UWP app without using wrappers such as EmguCV. (In the Change Log for OpenCV, it says one of the versions includes UWP build support, but it's for phones only.)

On Debug x64 mode, calling imshow raises this exception: Exception thrown at 0x00007FFAAC1E3E1F (opencv_highgui310d.dll) in OpenCVImageViewerTest.exe: 0xC0000005: Access violation reading location 0x0000000000000000.

This exception is raised on Release x64 mode: Unhandled exception at 0x00007FFAE6D7A478 (ucrtbase.dll) in OpenCVImageViewerTest.exe: An invalid parameter was passed to a function that considers invalid parameters fatal.

A few StackOverflow questions I came across mentioned something about library mismatches between debug and release versions of OpenCV and its corresponding libraries. However, I checked the Project Properties for my WinRT component for both versions, and the Additional Libraries I specified are the right versions.

OpenCVImageOpener.h:

#pragma once

// OpenCV Dependent Libraries
#include <opencv2\core\core.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2\highgui\highgui.hpp>

// WinRT Dependent Headers
#include <collection.h>
#include <ppl.h>
#include <amp.h>
#include <amp_math.h>

// OpenCV Namespace
using namespace cv;

namespace OpenCVImage
{
    // Have this class be compiled as a WinRT Component for a UWP app
    public ref class OpenCVImageOpener sealed
    {
    public
        void loadImage(Platform::String^ filename); // Loads image and displays it on the screen
    };
};

OpenCVImageOpener.cpp:

// Microsoft Visual Studio friendly includes
#include "pch.h"

// C++ Standard Libraries
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <vector>
#include <memory>
#include <cmath>

// Windows Runtime Component libraries
#include <ppltasks.h>
#include <concurrent_vector.h>

// Custom Header Files
#include "OpenCVImageOpener.h"

// Namespaces
using namespace OpenCVImage;
using namespace cv;
using namespace std;

using namespace concurrency;
using namespace Platform::Collections;
using namespace Windows::Foundation::Collections;
using namespace Windows::Foundation;
using namespace Windows::UI::Core;

void OpenCVImageOpener::loadImage(Platform::String^ filename) {
    // Debugging statements omitted for brevity

    std::wstring filenameBuffer(filename->Begin());
    string rawFilenameString(filenameBuffer.begin(), filenameBuffer.end());

    Mat image = imread(rawFilenameString.c_str());

    imshow("Test image", image); // Error occurs right here
}

The UWP app that calls this code is simply one screen with one button and an event listener.