在c ++ / cx中将SoftwareBitmap或WriteableBitmap转换为cv :: Mat

时间:2016-05-22 14:10:33

标签: c++ opencv c++-cx

我尝试在C ++ / cx Microsoft universial App中将WriteableBitmap转换为cv :: Mat。但是当我尝试使用创建的Mat进行处理时,我收到以下错误:

enter image description here

这是我的代码:

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);
     ...
}

所以我的最后一个问题: 如何将SoftwareBitmap或WriteableBitmap转换为cv :: Mat?

2 个答案:

答案 0 :(得分:1)

问题在于PixelBuffer is not a void *, it is an IBuffer^

要获取原始数据,如果您对COM编程感到满意,可以使用IBufferByteAccess interface,或者如果您希望保留WinRT,则可以初始化DataReader with an IBuffer(尽管这种技术将复制数据)。

答案 1 :(得分:0)

我使用DataReader来解决问题:

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);
     ...
}

向Peter Torr致谢。