0

I tried to convert a WriteableBitmap to a cv::Mat in a c++/cx Microsoft universial App. But when I try to progress with the created Mat, I get the following error:

enter image description here

This is my Code:

void App1::MainPage::processImage(SoftwareBitmap^ bitmap)
{
     WriteableBitmap^ wb = ref new WriteableBitmap(bitmap->PixelWidth, bitmap->PixelHeight);
     bitmap->CopyToBuffer(wb->PixelBuffer);
     Mat img_image(wb->PixelHeight, wb->PixelWidth, CV_8UC3,(void*)wb->PixelBuffer);
     //next step results in error
     cvtColor(img_image, img_image, CV_BGR2BGRA);
     ...
}

So my final question: How to convert the SoftwareBitmap or the WriteableBitmap to a cv::Mat?

Thorben Wi
  • 11
  • 3

2 Answers2

1

The problem is that PixelBuffer is not a void *, it is an IBuffer^.

To get at the raw data, you can either use the IBufferByteAccess interface if you're comfortable with COM programming, or you can initialize a DataReader with an IBuffer if you'd prefer to stay in WinRT (although this technique will make a copy of the data).

Peter Torr - MSFT
  • 11,824
  • 3
  • 18
  • 51
0

I used the DataReader to solve the problem:

void App1::MainPage::processImage(SoftwareBitmap^ bitmap)
{
     WriteableBitmap^ wb = ref new WriteableBitmap(bitmap->PixelWidth, bitmap->PixelHeight);
     bitmap->CopyToBuffer(wb->PixelBuffer);
     IBuffer^ buffer = wb->PixelBuffer;
     auto reader = ::Windows::Storage::Streams::DataReader::FromBuffer(buffer);
     BYTE *extracted = new BYTE[buffer->Length];
     reader->ReadBytes(Platform::ArrayReference<BYTE>(extracted, buffer->Length));
     Mat img_image(wb->PixelHeight, wb->PixelWidth, CV_8UC4, extracted);
     cvtColor(img_image, img_image, CV_RGBA2BGRA);
     ...
}

Thx to Peter Torr for the hint.

Thorben Wi
  • 11
  • 3