2012-07-28 83 views
0

这是我的相关代码:帧在循环不更新

在窗体的负载方法:

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()方法,这是不是没有在当前时间返回的是从摄像头的代码的一个总结,或者是和设置currentFrame正在其旧值,而不管:

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。

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

回答

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); 
} 
+0

谢谢!这就是诀窍! – Froskoy 2012-07-28 19:46:44

+0

您好,很高兴为您效劳 – 2012-07-29 02:32:30

+0

嗨,最好的办法是使用Backgroundworker,如果您在Windows窗体应用程序下更新您的GUI ;-) – 2012-12-23 19:26:18