帧不在循环中更新

时间:2012-07-28 17:38:57

标签: c# emgucv

这是我的相关代码:

在表单的加载方法中:

Application.Idle += new EventHandler(delegate(object s, EventArgs ea)
{
    if (!recording)
    {
        currentFrame = getFrame();
        ib.Image = currentFrame;
    }
    else
    {
        ib.Image = recFrame;
    }
    });

按下按钮(ib是表单上的ImageBox):

Measurements measurements = new Measurements();
String name = txtSampleName.Text;
ib.Image = recFrame;
stopwatch.Start();
recording = true;
while ((count = (int)Math.Floor(stopwatch.ElapsedMilliseconds/(double)1000)) < finalTime)
{
    currentFrame = getFrame();        
    Measurement currentMeasurement = new Measurement(name, currentFrame);
    measurements.add(currentMeasurement);
}
recording = false;
stopwatch.Stop();
measurements.write(txtDirectory.Text);

问题是框架实际上没有更新。 currentFrame获取循环开始之前帧的值。 recFrame在ib.Image中不可见 - ib只是在循环的持续时间内挂起 - 而不是应该填充recFrame)。 currentMeasurement应该添加到测量列表中,但其中的帧不是来自摄像机的当前帧。以下是getFrame()代码的摘要,该代码要么不返回当前时间网络摄像头的内容,要么当前帧和当前帧都采用旧值:

private Image<Bgr, Byte> getFrame()
{
    // get the frame
    Image<Bgr, Byte> frame = capture.QueryFrame();

    // draw some stuff on top of the frame
    // ...

    // return
    return frame;
}

我是否需要添加某种延迟来等待某些事情完成?我试过放入一个Thread.Sleep并没有任何帮助。我不明白问题是什么,所以无法找到合理的解决方案。当我稍后将测量中的帧导出到AVI文件时,所有帧在循环开始之前都具有currentFrame的值,并且不会更改。

有什么想法吗?

非常感谢,

Froskoy。

编辑:在行currentMeasurement = new上使用currentFrame.Copy()测量(name,currentFrame)意味着当前捕获的帧实际上已放入列表中,然后写入AVI文件。我不明白为什么呢?此外,recFrame仍未显示在ib中,所以我不明白这里发生了什么?

1 个答案:

答案 0 :(得分:1)

尝试在你的while循环中进行DoEvents调用

while ((count = (int)Math.Floor(stopwatch.ElapsedMilliseconds/(double)1000)) < finalTime) 
{ 
    currentFrame = getFrame(); 
    ib.Image = currentFrame; 
Application.DoEvents();               
    Measurement currentMeasurement = new Measurement(name, currentFrame); 
    measurements.add(currentMeasurement); 
}