【发布时间】:2014-04-15 00:07:46
【问题描述】:
我在 UI 线程中创建了 BitmapSource,我想将其像素数据复制到 cv::Mat 以在单独的线程上进行处理。当然,如果我用Dispatcher.Invoke 调用来包装复制代码,它会起作用,但是我会浪费同步时间。
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 位图对象能够做到这一点?
【问题讨论】:
标签: c++ wpf multithreading opencv bitmapsource