Converting a SoftwareBitmap or WriteableBitmap to cv::Mat in c++/cx

…衆ロ難τιáo~ 提交于 2019-12-02 17:50:41

问题


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:

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?


回答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).




回答2:


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.



来源:https://stackoverflow.com/questions/37375414/converting-a-softwarebitmap-or-writeablebitmap-to-cvmat-in-c-cx

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!