【问题标题】:EmguCV ImageGrabbed Event WPF AppEmguCV ImageGrabbed 事件 WPF 应用程序
【发布时间】:2015-11-26 16:19:10
【问题描述】:

以下代码有问题

namespace MyApp
{    
    public partial class PhotoWindow : Window
    {
        private Capture _capture;

        public PhotoWindow ()
        {
            InitializeComponent();    
            _capture = new Capture();

            if (_capture != null)
            {
                //<Image> in XAML
                CaptureSource.Width = 150;
                CaptureSource.Height = 180;

                _capture.ImageGrabbed += ProcessFrame;
                _capture.Start();                
            }

            Activated += (s, e) => _capture.Start();
            Closing += (s, e) =>
            {
                if (_capture == null) return;
                _capture.Stop();                
                _capture.Dispose();
            };
        }

        private void ProcessFrame(object sender, EventArgs e)
        {
            try
            {
                Image<Bgr, Byte> frame = _capture.RetrieveBgrFrame();                   
                CaptureSource.Source = Helper.ToBitmapSource(frame);
            }
            catch (Exception exception)
            {

                System.Windows.MessageBox.Show(exception.ToString());
            }
        }

    }
}

当我运行应用程序时,我在CaptureSource.Source = Helper.ToBitmapSource(frame); 线上收到异常System.InvalidOperationException: The thread that this call can not access this object because the owner is another thread

我能解决这个问题吗?

【问题讨论】:

    标签: c# multithreading emgucv


    【解决方案1】:

    似乎 ImageGrabbed 事件是从 Capture 的后台线程引发的,因此您的处理程序在该线程中运行,而不是在窗口的 UI 线程中运行。

    您可以使用 Dispatcher 在控件的 UI 线程中调用代码。

    CaptureSource.Dispatcher.Invoke(() =>
    {
       Image<Bgr, Byte> frame = _capture.RetrieveBgrFrame();                   
       CaptureSource.Source = Helper.ToBitmapSource(frame);
    });
    

    【讨论】:

      【解决方案2】:

      我在 UI 中使用 Emgu.CV.UI.ImageBox 来显示来自 Capture 的 Image 帧

      Image<Bgr, Byte> frame = _capture.RetrieveBgrFrame();    
      imageBox.Invoke(new Action(() => imageBox.Image = frame));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-30
        • 2010-10-11
        • 1970-01-01
        • 2022-01-17
        • 1970-01-01
        相关资源
        最近更新 更多