Image Processing: Accessing image data

The following programming example shows how you can use IC Imaging Control to access image data.
Language:Visual Basic 6
Version:3.0.3
Released on:April 7, 2005
Author:IC Imaging Control Support Department
Requirements:IC Imaging Control >2.1
Camera, converter or grabber with WDM Stream Class drivers.

The following programming example (Visual Basic) shows how you can use IC Imaging Control to grab an image from a device's live image data stream, to write it into a buffer and to display the RGB value of the pixel at the current mouse position.

The sample application's window looks as follows:

The Testdialog of IC Imaging Control,

The program starts by activating a built-in dialog to select a device (.ShowDeviceSettingsDialog). Then, this device's live image data stream is started using .LiveStart but due to ICImagingControl1.LiveDisplay = False it is not displayed (see dialog on the right). Because .LiveCaptureContinuous = True, the image data stream simultaneously streams into an internal ring buffer. .MemoryCurrentGrabberColorformat = ICRGB24 makes sure that the image data is available based on the RGB24 format:

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

When the user moves the mouse, the program creates the image buffer reference ib, sets it to the ring buffer's current image (ImageBuffers.CurrentIndex) and locks the image. If the mouse position is inside the image, GetImageColorValueRGB24 acquires the current pixel's RGB value and writes it to the dialog (see image on the right):

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

Using the image buffer reference ib, GetImageColorValueRGB24 accesses the image buffer's current pixel. Please note that in the case of RGB24 images, the data is stored "upside down". Row 0 contains the data of the last image row, row 1 contains the data of the row before last, etc. The first column contains the blue value, the second column contains the green value and the third column contains the red value of a row's first pixel. Thus, the fourth column contains the second pixel's blue value:

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

Related Source Code Samples

Disclaimer
The source code that appears in the IC Imaging Control Source Code Library is indented for educational purposes only. The Imaging Source Europe GmbH, the manufacturer of IC Imaging Control, does not assume any kind of warranty expressed or implied, resulting from the use of the content of this page.

This site is part of The Imaging Source Network. Other sites include Company Portal, Image Processing, Astronomy Cameras, Astronomy Cameras Blog, Blog caméras d'astronomie, Astronomy Cameras Competition, TX Text Control, LiveDocx, phpLiveDocx and Forums.