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 CopyPixels
can'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?