圖像處理: 尋找激光點

这个 C# 示例程序將演示如何找到視頻圖像裡的一個激光點。
語言:.NET C#/Visual Basic
版本:3.0.3
发布于:2007年7月27日
作者:IC Imaging Control 技術支持部
需求:IC Imaging Control >3.0.3, Visual Studio™ 2005
由WDM數據流類驅動程序驅動的相機、視頻轉換器或圖像採集卡

簡介

本示例程序將演示如何找到視頻圖像裡的一個激光點,並在這個點上標記出一個十字準心。

程序窗口如圖所示:

Image Sequence

程序開始後首先調用用於選擇視頻設備的內置對話框 (.ShowDeviceSettingsDialog)。 而後,依次執行以下幾個步驟:

  • 創建一個全局變量 overlayBitmap 用於訪問 OverlayBitmap;
  • 將圖像緩存的色彩格式設置為 ICRGB24 (.MemoryCurrentGrabberColorformat = ICRGB24),這樣程序的算法就能慮除紅色;
  • 開啟 OverlayBitmap (.OverlayBitmap.Enable),並將其設置與顯示分支中,這樣一來,十字準心就不在圖像緩存中了,進而不會影響到搜尋算法。

[C#]
private TIS.Imaging.OverlayBitmap overlayBitmap;
[C#]
private void Form1_Load(object sender, EventArgs e)
{

    icImagingControl1.ShowDeviceSettingsDialog();

    if (!icImagingControl1.DeviceValid)
    {
        cmdStartLive.Enabled = false;
        cmdSettings.Enabled = false;
    }

    // The color format of the image buffers is set to ICRGB24.
    icImagingControl1.MemoryCurrentGrabberColorformat = TIS.Imaging.ICImagingControlColorformats.ICRGB24;

    // The overlaybitmap position is set in the display path.
    icImagingControl1.OverlayBitmapPosition = TIS.Imaging.PathPositions.Display;
    overlayBitmap = icImagingControl1.OverlayBitmapAtPath[TIS.Imaging.PathPositions.Display];

    // Enable the overlay.
    overlayBitmap.Enable = true;

    // Save current frame to the ImageActiveBuffer.
    icImagingControl1.LiveCaptureContinuous = true;

    cmdFindLaser.Enabled = false;
    cmdStoplive.Enabled = false;
}

用戶點擊 find! 按鈕後,程序將調用 findLaserPoint。 如果找到了激光點,則這個方法返回True值,同时通過變量 xLaseryLaser 獲取這個點的坐標值。 而後在這個點的位置(xLaseryLaser)上標記出十字準心。

[C#]
private void cmdFindLaser_Click(object sender, EventArgs e)
{
    int xLaser, yLaser;

    // Clear the overlay.
    overlayBitmap.Fill(overlayBitmap.DropOutColor);

    if (findLaserPoint(out xLaser, out yLaser))
    {
        DrawCrosshairs(xLaser, yLaser);
    }
    else
    {
        MessageBox.Show("No laser point found!");
    }
}

cmdFindLaser_Click中調用的方法 findLaserPoint執行搜尋圖像中激光點的具體操作。

通過調用ImageBuffers.Activebuffer,它將圖像緩存中的最近一幀圖像賦值給 imagebuffer

激光點的搜尋工作只針對每個像素的紅色分量值。

圖像緩存中像素的引用方式是:行 x 與列 y 組成的坐標。

為了獲取每個像素的紅色分量值,每個x的值都首先需要乘以3,因為RGB24色彩格式為每個像素提供了3bytes, 而後,因為色彩值分量的存儲順序為“藍、綠、紅”,所以需要再加2。

縱向坐標y也需要重新計算一遍,因為每幅圖像都是上下顛倒地被存儲在圖像緩存中的。

這些計算結果都被存儲在變量 indexXred indexY中,imagebuffer 將使用它們作為訪問每個像素點的參數。

激光點在圖像中都不只一個像素那麼大。 為了計算出激光點的中心位置,程序使用 redPixel 用以統計所有激光點像素。 此外,坐標值x與y也通過變量 xLaseryLaser 進行累積。 在整幅圖像處理完之後,經過累積的x和y的值-xLaseryLaser 將被所找到的像素點的數目 redPixel 除。 這樣就獲得了激光點的中心位置。

具體的處理過程需要在兩個 for 循環內完成, 它們分別被用於遍歷一幅圖像的每一行和每一列。 如果當前像素點的紅色分量值大於事先設定的閾值 (230),該像素就被視為一個激光點像素。 這個方法最後計算所有激光點像素區域的中心, 並返回其坐標值。

[C#]
private bool findLaserPoint(out int xLaser, out int yLaser)
{
    TIS.Imaging.ImageBuffer imagebuffer = icImagingControl1.ImageActiveBuffer;
    int redPixel = 0;
    xLaser = 0;
    yLaser = 0;

    // Check each pixel in the image if the red value is greater than the desired threshold (230).


    for (int y = 0; y < imagebuffer.Lines; y++)
    {
        for (int x = 0; x < imagebuffer.PixelPerLine; x++)
        {
            // x and y index for the imagebuffer.
            int indexXred = x * 3 + 2;
            int indexY = imagebuffer.Lines - y - 1;

            if (imagebuffer[indexXred, indexY] > 230)
            {
                // Count red pixels.
                redPixel++;

                xLaser = xLaser + x;
                yLaser = yLaser + y;
            }
        }
    }
    // Calculate the center of all red pixels.
    if (redPixel > 0)
    {
        xLaser = xLaser / redPixel;
        yLaser = yLaser / redPixel;

        return true;
    }
    else
    {
        return false;
    }
}

十字準心由兩條紅線交叉構成,同時附有白色文字用以顯示其位置坐標。具體做法是兩次調用 overlayBitmap.DrawLine 用於劃線,一次調用 overlayBitmap.DrawText 用以生成坐標文字。

[C#]
private void DrawCrosshairs(int x, int y)
{
    overlayBitmap.DrawLine(Color.Red, x, y - 10, x, y + 10);
    overlayBitmap.DrawLine(Color.Red, x - 10, y, x + 10, y);
    overlayBitmap.DrawText(Color.White, x + 3, y + 2, x.ToString() + "," + y.ToString());
}

相關源代碼示例

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