2

I have a BitmapSource created in the UI thread and I would like to copy its pixel data to a cv::Mat for processing on a separate thread. Of course it works if I wrap the copy code with Dispatcher.Invoke call but then I waste synchronization time.

int width = bitmapSource->PixelWidth;
int height = bitmapSource->PixelHeight;
int bytesPerPixel = bitmapSource->Format.BitsPerPixel / 8;
int stride = 4 * ((width * bytesPerPixel + 3) / 4);

...

cv::Mat &inputImage = wrapper.getRawImage();  // This is a refe
if (inputImage.size().width != width || inputImage.size().height != height || inputImage.type() != newType) 
    inputImage = cv::Mat::zeros(height, width, newType);
bitmapSource->CopyPixels(Int32Rect::Empty, IntPtr(inputImage.data), height*stride, stride);

I tried freezing the bitmapSource object and now it works until the call to CopyPixels but it fails when trying to copy its content to the inputImage data. If the object is frozen why CopyPixelscan't copy its frozen buffer to another memory buffer? Any ideas on how to avoid invoking the call on the main thread? Is there any other WPF bitmap object capable of doing this?

Darien Pardinas
  • 5,910
  • 1
  • 41
  • 48

1 Answers1

0

You can refer to here, although the code lines is written by C#, but it's very valuable to you

  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Sᴀᴍ Onᴇᴌᴀ Jun 20 '17 at 23:05