none
Using OpenCV in a Windows Runtime component (C++) for a UWP app RRS feed

  • Question

  • Can you write a Windows Runtime component that calls some function from OpenCV using C++ and integrate it into a UWP app? This is for a UWP app to be built and run on Surface tablets. (In the Change Log for OpenCV, it says one of the versions includes UWP build support, but it's for phones only.)

    What I'm stuck with are some linker errors when trying to call the imshow function in my WinRT component. 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.

    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.


    Thursday, February 15, 2018 9:13 PM

All replies

  • Hi GregPD696LM5,

    Since this problem is more related to OpenCV, it is a third part library and it is out of the scope of the UWP forum. I suggest that you could ask about this in forum for OpenCV.

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, February 16, 2018 7:53 AM
  • Why, sure thing.

    Thought I'd ask first over here because the issue may be related to some of the window abstract toolkits used between OpenCV and UWP, or that there's something in the UWP libraries that conflict with those in OpenCV.

    Thanks for following me up, Roy.

    Friday, February 16, 2018 2:17 PM