圖像獲取: 找回丟失的圖像幀

這一節將介紹如何使用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.