2

BitmapSource在 UI 线程中创建了一个,我想将其像素数据复制到cv::Mat一个单独的线程上进行处理。当然,如果我用Dispatcher.Invokecall 包装复制代码,但我会浪费同步时间。

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

我尝试冻结该bitmapSource对象,现在它在调用之前一直有效,CopyPixels但在尝试将其内容复制到inputImage数据时失败。如果对象被冻结,为什么CopyPixels不能将其冻结的缓冲区复制到另一个内存缓冲区?关于如何避免在主线程上调用调用的任何想法?是否有任何其他 WPF 位图对象能够做到这一点?

4

1 回答 1

0

你可以参考这里,虽然代码行是用C#写的,但是对你很有价值

于 2017-06-20T22:47:56.730 回答