Windows Runtime Component (C++ based) in UWP - Load and display image
This is the first time I'm using OpenCV Answers to post questions.
I want to use OpenCV to load an image and display it in a window, but through a Windows Runtime component that a UWP app instantiates and calls a function from. I'm stuck on this error, though:
Exception thrown at 0x00007FFAAC1E3E1F (opencv_highgui310d.dll) in OpenCVImageViewerTest.exe: 0xC0000005: Access violation reading location 0x0000000000000000.
This is from this line:
imshow("Test image", image);
The component is built to run on x64, Debug. The libraries I specified in Linker > Input > Additional Dependencies all have the d suffix before the .lib extension.
This is the code for my Windows Runtime Component: [This is my first time writing a Windows Runtime component; please don't mind if I have a few inconsistencies such as having too many includes]
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 the function loadImage is simply a window with one button with an event listener that runs the function in a thread separate from the UI Thread.
I'm using Visual Studio 2017 with five NuGet packages:
- OpenCV.Win.Core.310.6.1
- OpenCV.Win.HighGUI.310.6.1
- OpenCV.Win.ImgCodecs.310.6.1
- OpenCV.Win.ImgProc.310.6.1
- OpenCV.Win.VideoIO.310.6.1
I looked around online to see if other users encountered the same issue. This question posted here caught my attention, but I do not know if it still has to do with the version of Visual Studio I'm using.
Otherwise, why is this error happening?