图像获取: 找回丢失的图像帧

这一节将介绍如何使用 IC Imaging Control 的 ImageAvailable 事件获取丢失的图像帧。
语言:.NET C#/Visual Basic
版本:3.0.3
发布于:2007年2月23日
作者:IC Imaging Control 技术支持部
系统要求:IC Imaging Control >2.1
由WDM数据流类驱动程序驱动的相机、视频转换器或图像采集卡

当某个图象处理操作对系统资源需求很大时,事件处理程序 ImageAvailable 中的图像帧有可能丢失。 IC Imaging Congtrol 的环形缓存技术为程序员使得访问未被 ImageAvailable 处理的图像帧的成为可能。 在这篇文档中,被捕捉但未被处理的图像帧被称为“丢失的帧”。

首先声明全局变量 LastHandledFrameIndex 用以估测丢失帧的数目,它用于存储最近被处理的一帧在环形缓存中的索引号。

[C#]
// Save the ring buffer index of the last processed frame.
private int LastHandledFrameIndex;

为了接收丢失的帧,需要将 .OverlayUpdateEventEnable 属性设置为 false。 属性 .LiveCaptureContinuous 应被设为 true,该属性使得 IC Imaging Control 捕捉每一帧图像并存入内存。 因为所有图像帧都是在 ImageAvailable 事件处理程序中从图象缓存中读出显示的,所以属性 .LiveDisplay 应被设为 false。 .ImageRingBufferSize 必须足够大才能存储丢失的帧。 在本例中,属性 .ImageRingBufferSize 被设为 60,就是说 60 帧图像被存储于内存中。 初始化代码如下:

[C#]
private void Form1_Load(object sender, EventArgs e)
{

    icImagingControl1.ShowDeviceSettingsDialog();

    if (icImagingControl1.DeviceValid ){

        icImagingControl1.LiveDisplayDefault = false;
        icImagingControl1.LiveDisplayHeight = icImagingControl1.Height;
        icImagingControl1.LiveDisplayWidth = icImagingControl1.Width;

        icImagingControl1.LiveCaptureContinuous = true;
        icImagingControl1.LiveDisplay = false;

        // To recieve dropped frames it is necessary to set
        // OverlayUpdateEventEnable to false.
        icImagingControl1.OverlayUpdateEventEnable = false;

        // Set the imagebuffer (must be large enough).
        icImagingControl1.ImageRingBufferSize = 60;

        icImagingControl1.LiveStart();

    }

}

ImageAvailable 事件处理程序中,丢失帧的侦测算法分两步实施: 第一步,计算丢失帧的数目并将其存储在变量 DroppedFramesCount 中。 如 DroppedFramesCount 的值大于1,则说明有帧丢失。 第二步,使用一个循环,在这个循环内,所有丢失的帧都被显示出来,同时 DroppedFramesCount 自减1,直到它的值小于2。

[C#]

private void icImagingControl1_ImageAvailable(object sender, TIS.Imaging.ICImagingControl.ImageAvailableEventArgs e)
{
    TIS.Imaging.ImageBuffer buffer  = icImagingControl1.ImageBuffers[e.bufferIndex];

    // Calculate the difference between the last processed buffer index and the current buffer
    // index. This will return the count of dropped frames.
    int DroppedFramesCount = e.bufferIndex - LastHandledFrameIndex;

    // If DroppedFramesCount is less than zero then the ring buffer index has been started
    // again at zero between the LastHandledFrameIndex and then current e.bufferIndex.
    if ( DroppedFramesCount < 0 )
        DroppedFramesCount = icImagingControl1.ImageRingBufferSize + DroppedFramesCount;

    // If frames have been dropped, then display all non processed frames in the following
    // loop.
    while (DroppedFramesCount > 1)
    {
        // Calculate the index of a dropped frame.
        int DroppedFrameIndex = LastHandledFrameIndex - DroppedFramesCount - 1;
        // Handle an overflow of the RingBufferIndex.
        if (DroppedFrameIndex < 0)
        {
            //Keep in mind DroppedFrameIndex is negative!
            DroppedFrameIndex = icImagingControl1.ImageRingBufferSize + DroppedFrameIndex;
        }

        // Get the dropped frame.
        TIS.Imaging.ImageBuffer LostFrame = icImagingControl1.ImageBuffers[DroppedFrameIndex];
        // Display the dropped frame.
        icImagingControl1.DisplayImageBuffer(LostFrame);

        DroppedFramesCount--;
    }

    //Sleep for simulate time expensive image processing.
    System.Threading.Thread.Sleep(300);

    icImagingControl1.DisplayImageBuffer(buffer);

    // Save the ring buffer index of the current frame into LastHandledFrameIndex. This will
    // be used next time ImageAvailable is called again.
    LastHandledFrameIndex = e.bufferIndex;
}

相关源代码示例

责任声明
IC Imaging Control 源代码库中的所有代码均只用于教学目的,The Imaging Source Europe GmbH 作为IC Imaging Control的开发制造商,不对任何由于使用本文或其中源代码所产生的后果承担责任。

该网站为The Imaging Source网络的一部分。其它的站点包括 公司, Imaging, 天文相机, Astronomy Cameras Blog, Blog caméras d'astronomie, 天文相机有奖竞答, TX Text Control, LiveDocx, phpLiveDocxForum.