问题描述
我的kinect应用程序有主线程,该线程可用于UI线程和其他线程之间的通信.我无法复制从kinect生成的WriteableBitmap,并将此WriteableBitmap图像通过EmguCV处理传递给单独的线程.我正在尝试所有操作:Clone,CloneCurrentValue,BlockingCollection,但是仍然存在一些问题,例如:
或者处理数据是错误的. 这是我应用中的主循环;
没有一个很好的,最小,完整代码示例,即使不是不可能也不知道确切的问题是困难的,也不必介意如何解决.那说…
您可能已经知道,WriteableBitmap
继承了DispatcherObject
,并且只能在拥有它的调度程序线程中进行访问.
大概是真正创建对象的是对kinect.updateFrames()
的调用,因此一个明显的解决方案是在被调用的匿名方法中而不是在其之前调用该方法. >
如果由于某些原因不可行,则可以选择冻结位图,然后再尝试在错误的线程中使用位图.例如:
kinect.updateFrames();
color = kinect.video.getBitmapColor();
depth = kinect.video.getBitmapDepth();
color.Freeze();
depth.Freeze();
ctrlMainWindow.Dispatcher.BeginInvoke(new Action(() =>
{
// use color and depth in other thread
}));
除非有任何限制,否则您可以直接访问位图的数据(例如CopyPixels()
或Lock()
/BackBuffer
),并使用该数据在正确的线程上创建新的位图.
如果上述方法都不对您有用,请按照上面提供的链接中的说明提供一个良好的代码示例.
I my kinect application I have main thread which is resposible for comunication between UI thread and other threads. I am no able to make a copy of WriteableBitmap generated from kinect and pass this WriteableBitmap image to separated thread with EmguCV processing. I was trying everything: Clone, CloneCurrentValue, BlockingCollection, but there are alwas some problems like:
Or processing data is wrong. This is main loop in my app;
Without a good, minimal, complete code example that reliably reproduces the problem, it's difficult if not impossible to know what the exact problem is, never mind how to fix it. That said…
As you're probably aware, WriteableBitmap
inherits DispatcherObject
, and must be accessed only within the dispatcher thread that owns it.
Presumably, the call to kinect.updateFrames()
is what actually creates the objects, and so one obvious solution would be to call that method in the invoked anonymous method instead of just before it.
If for some reason that's not feasible, an alternative would be to freeze the bitmaps before trying to use them in the wrong thread. E.g.:
kinect.updateFrames();
color = kinect.video.getBitmapColor();
depth = kinect.video.getBitmapDepth();
color.Freeze();
depth.Freeze();
ctrlMainWindow.Dispatcher.BeginInvoke(new Action(() =>
{
// use color and depth in other thread
}));
Barring any of that, you can access the bitmaps' data directly (e.g. CopyPixels()
or Lock()
/BackBuffer
) and use that data to create new bitmaps on the correct thread.
If none of the above proves useful to you, please provide a good code example as described in the link provide above.
这篇关于如何在单独的线程中复制和处理WriteableBitmap?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!