圖像處理: 訪問圖像數據

下面的源代碼將展示如何使用IC Imaging Control訪問圖像數據。
語言:.NET C#/Visual Basic
版本:3.0.3
发布于:2005年4月7日
作者:IC Imaging Control 技術支持部
需求:IC Imaging Control >2.1
由WDM數據流類驅動程序驅動的相機、視頻轉換器或圖像采集卡

Image of the application

從視頻捕捉設備那裏獲得圖像數據並存入 IC Imaging Control 的環形緩存。 當鼠標在畫面中移動時,程序將顯示其所指點像素的RGB色彩值。

程序開始後將首先調用一個內置對話框,用以選擇一個視頻設備。 具體語句為 icImagingControl1.ShowDeviceSettingsDialog。 當選擇一個有效設備後,調用 icImagingControl1.LiveStart 將開啟視頻。 屬性 icImagingControl1.LiveCaptureContinuous 自動被設置為 True。 這將使得 IC Imaging Control 連續不斷地將每一幀圖像復制到環形緩存中去。

.MemoryCurrentGrabberColorformat = ICRGB24 將確保圖像數據格式為RGB24:

[C#]
private void Form1_Load(object sender, System.EventArgs e)
{
    icImagingControl1.ShowDeviceSettingsDialog();

    if( !icImagingControl1.DeviceValid )
    {
        Close();
        return;
    }

    icImagingControl1.LiveCaptureContinuous = true;
    icImagingControl1.MemoryCurrentGrabberColorformat = TIS.Imaging.ICImagingControlColorformats.ICRGB24;

    icImagingControl1.LiveStart();
}

在鼠標移動事件處理程序中,先定義一個TIS.Imaging.ImageBuffer類型變量ib。 程序使用該變量訪問最近抓取的一幀圖像。 調用屬性icImagingControl1.ImageActiveBuffer即可獲得最近一幀圖像。 為了防止圖像被覆蓋,可使用ib.Lock()鎖住該幀圖像所處的緩存單元。 示例程序還在鼠標移動事件處理程序中使用了函數GetImageColorValueRGB24(),用以完成圖像處理功能。 。 鼠標移動事件處理程序的 C# 源代碼如下:

[C#]
private void icImagingControl1_MouseMove(object sender, TIS.Imaging.ICImagingControl.MouseEventArgs e)
{
    lblCoords.Text = e.x + "," + e.y;

    TIS.Imaging.ImageBuffer ib = icImagingControl1.ImageActiveBuffer;
    ib.Lock();

    if( e.x < ib.PixelPerLine && e.y < ib.Lines )
    {
        byte red, green, blue;
        getImageColorValueRGB24( ib, e.x, e.y, out red, out green, out blue );

        txtImageData.Text = string.Format( "Red\t=  {0}\r\nGreen\t=  {1}\r\nBlue\t=  {2}", red, green, blue );
    }
    else
    {
        txtImageData.Text = "<Outside of image>";
    }

    ib.Unlock();
}

函數 GetImageColorValueRGB24() 用於訪問圖象緩存中鼠標所指的像素點。 需要註意的是,RGB24格式的圖像數據的存儲都是上下顛倒的。 第0行保存的是圖像中最後一行的色彩值,第1行保存的是圖像中倒數第二行的色彩值,如此類推。 第一列保存的是第一個象素點的藍色分量值,第二列是綠色值,第三列是紅色之。 依此類推,第四列保存的就是第二個象素點的藍色值。 C# 源代碼(可下載)中含有像素位置計算的相關語句。 具體如下:

[C#]
private void getImageColorValueRGB24( TIS.Imaging.ImageBuffer ib, int x, int y, out byte red, out byte green, out byte blue )
{
    int indexXred = x * 3 + 2;
    int indexXgreen = x * 3 + 1;
    int indexXblue = x * 3;

    int indexY = ib.Lines - y - 1;

    red = ib[ indexXred, indexY ];
    green = ib[ indexXgreen, indexY ];
    blue = ib[ indexXblue, indexY ];
}

相關源代碼示例

責任聲明
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.