圖像處理: 訪問圖像數據

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

下面的Visual Basic示例代碼將演示如何使用IC Imaging Control 從視頻設備中讀取實時圖像數據流、 寫入緩存、同時顯示鼠標所在點的RGB值。

示例程序的窗口如圖:

The Testdialog of IC Imaging Control,

程序的第一條語句用來激活內置對話框(.ShowDeviceSettingsDialog),從而選擇視頻設備。 而後,使用.LiveStart開始傳入設備發來的視頻數據流。因為.LiveCaptureContinuous = True, 圖像數據流將同時被寫入內部環形緩存。.MemoryCurrentGrabberColorformat = ICRGB24語句確保圖像數據 為RGB24格式:

Private Sub Form_Load()

    ICImagingControl1.ShowDeviceSettingsDialog

    If Not ICImagingControl1.DeviceValid Then
        Unload Me
        Exit Sub
    End If

    ICImagingControl1.LiveCaptureContinuous = True
    ICImagingControl1.MemoryCurrentGrabberColorformat = ICRGB24

    ICImagingControl1.LiveStart

End Sub

用戶移動鼠標時,程序新建圖像緩存引用ib,將其設為環形緩存中的當前圖像(ImageBuffers.CurrentIndex), 同時鎖定圖像。如果鼠標在圖像內,GetImageColorValueRGB24 將讀取當前像素的RGB值,而後寫入對話框中:

Private Sub ICImagingControl1_MouseMove(ByVal Button As Long, ByVal Shift As Long, ByVal XPos As Integer, ByVal YPos As Integer)

    lblCoords.Caption = XPos & "," & YPos

    Dim ib As ImageBuffer

    Set ib = ICImagingControl1.ImageBuffers(ICImagingControl1.ImageBuffers.CurrentIndex)
    ib.Lock

    If XPos < ib.PixelPerLine And YPos < ib.Lines Then

        Dim red As Byte, green As Byte, blue As Byte
        GetImageColorValueRGB24 ib, XPos, YPos, red, green, blue

        txtImageData.Text = "Red" & vbTab & "=  " & red & vbCrLf & _
                            "Green" & vbTab & "=  " & green & vbCrLf & _
                            "Blue" & vbTab & "=  " & blue

    Else

        txtImageData.Text = "<Outside of image>"

    End If

    ib.Unlock

End Sub

GetImageColorValueRGB24 使用圖像緩存引用ib 讀取當前像素。請註意在RGB24圖像中,其數據存儲順序正好與圖像 顯示上下顛倒,即 0 行中的數據實際是圖像最底行的數據,1行的數據是圖象中自下而上的第二行中的像素數據。 第一列存儲第一個像素的藍色分量值,第二個存儲綠色、第三個存儲紅色。以此類推,第四列存儲的是第二個像素的藍色值:

Private Sub GetImageColorValueRGB24(ib As ImageBuffer, x As Integer, y As Integer, ByRef red As Byte, ByRef green As Byte, ByRef blue As Byte)

    Dim imageData As Variant
    imageData = ib.GetImageData

    Dim indexXred As Integer
    Dim indexXgreen As Integer
    Dim indexXblue As Integer
    Dim indexY As Integer

    indexXred = x * 3 + 2
    indexXgreen = x * 3 + 1
    indexXblue = x * 3

    indexY = ib.Lines - y - 1

    red = imageData(indexXred, indexY)
    green = imageData(indexXgreen, indexY)
    blue = imageData(indexXblue, indexY)

    ib.ReleaseImageData imageData

End Sub

相關源代碼示例

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