Accessing Image Data

Short source code snippet that illustrates how to access image data using IC Imaging Control.

Language:.NET C#/Visual Basic
Version:3.3.0.0
Author:IC Imaging Control Support Department

Requirements:
Software:IC Imaging Control 3.3, Visual Studio™ 2010
Hardware:Camera, converter or grabber with WDM Stream Class drivers.
Download C# sampleAccessing image data. - C#

Image of the application

Image data is grabbed from the video capture device's live video stream and copied into IC Imaging Control's ring buffer. While the mouse cursor is moved over the live video display, the RGB color values of the pixel under the mouse cursor are displayed.

The program starts by activating a built-in dialog to select a video capture device. This is done by calling icImagingControl1.ShowDeviceSettingsDialog. After a valid video capture device has been selected, a single call to icImagingControl1.LiveStart starts the image data stream. The property icImagingControl1.LiveCaptureContinuous is automatically set to True. This tells IC Imaging Control to copy each incoming image frame continuously into the ring buffer.

.MemoryCurrentGrabberColorformat = ICRGB24 makes sure that the image data is available based on the RGB24 format:

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();
}

        

A variable ib of type TIS.Imaging.ImageBuffer is declared in the mouse move event handler. This variable is used to access the data of the last grabbed image. The last grabbed image can be queried by calling the property icImagingControl1.ImageActiveBuffer. To prevent the image from being overwritten, the image buffer should be locked by calling ib.Lock(). In this sample application, the function GetImageColorValueRGB24() has been implemented. This function is called from the mouse move event handler and performs the image processing. The C# source code of the mouse move event handler looks as follows:

C#
      
private void icImagingControl1_MouseMove(object sender, 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();
}

        

The function GetImageColorValueRGB24() is used to access the image buffer's pixel at the position that is referenced by the passed mouse position. 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. Consequently, the fourth column contains the second pixel's blue value. The downloadable C# source code (see above) contains the pixel position calculation. Retrieving the color values is implemented as follows:

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 ];
}