IC Imaging Control C++

YGB0

YGB0

YGB0 is a 10 bit grayscale format. Every pixel is stored in two bytes. Only 10 of the 16 bits of these two bytes contain relevant image data, the other bits are unused. The organization of the pixels in the image buffer is from left to right and top down.

Memory Layout

image

As illustrated above, the low byte contains the bits 2 to 9, while the least significant bits 0 and 1 are stored in the high byte. To convert a 16-bit-pixel from the image buffer to a 16-bit-value (in range 0-1023) that can be used in image processing, the bytes have to be swapped and a right-shift has to be performed. The following code shows how to convert to and from the YGB0 format:

unsigned __int16 YGB0PixelFromInt16Value( unsigned __int16 value )
{
    return byteSwap( value << 6 );
}
 
unsigned __int16 Int16ValueFromYGB0Pixel( unsigned __int16 pixel )
{
    return byteSwap( pixel ) >> 6;
}

The function Int16ValueFromYGB0Pixel takes a 16-bit word from the image buffer and converts it to a logical brightness value for that pixel in the range 0 to 1023.

YGB0PixelFromInt16Value reverses that operation by converting a brightness value to a number that can be stored in the image buffer.

The functions rely on the byteSwap function that is implemented as follows:

unsigned __int16 byteSwap( unsigned __int16 x )
{
    BYTE* p = (BYTE*)&x;
    return ((unsigned int)p[0] << 8) | p[1];
}

How to read and write pixel data

A video capture device, video format, FrameHandlerSink with a MemBufferCollection, which defines the image data color format must first have been setup. The following code fragments show step-by-step how to access and manipulate the pixel data of YGB0.

First of all, we have to capture an image. Otherwise, the image buffer would be empty. To do so, live mode is started and Grabber::snapImages is called.

Accessing the buffer

The following code retrieves a pointer to the image data. Please note that getPtr() returns a BYTE pointer which will be type-casted to an unsigned __int16 pointer. This makes it much easier to access the pixel data since YGB0 is a 16 bit format.

// YGB0 Images are stored top-down, so the upper left pixel starts at byte 0.
// 2 Byte represent one pixel.
unsigned __int16* pImgData = (unsigned __int16*)pActiveBuf->getPtr();

In this example, we want to output the first (upper left hand) pixel of the image and manipulate the first 3. As previously mentioned, the image data is stored top down. Therefore, pImgData points to the first pixel of the first line in the buffer.

To convert the pixel data to brightness values, the function YGB0PixelFromInt16Value from above is used:

// To get a "value" (in range 0-1023) for the pixel, use
// Int16ValueFromYGB0Pixel:
unsigned __int16 val0 = Int16ValueFromYGB0Pixel( pImgData[0] );
unsigned __int16 val1 = Int16ValueFromYGB0Pixel( pImgData[1] );
 
printf( "\nImage buffer pixel format is eYGB0\n" );
printf( "Pixel 1 (Y16): %d\n", val0 );
printf( "Pixel 2 (Y16): %d\n", val1 );

Manipulating Image Data

Now we set the upper left hand three pixels to black, gray and white. The function YGB0PixelFromInt16Value from above is used to convert from brightness values to pixel data:

pImgData[0] = YGB0PixelFromInt16Value( 0 );        // Set the first pixel to BLACK
pImgData[1] = YGB0PixelFromInt16Value( 511 );    // Set the second pixel to GRAY
pImgData[2] = YGB0PixelFromInt16Value( 1023 );    // Set the third pixel to WHITE
 
// We save the buffer as RGB24, so that other software can view it.
saveToFileBMP( *pActiveBuf, "YGB0.bmp", eRGB24 );

To check the result, just open the saved image and examine the upper left pixels. They should look as follows:

image

<< Accessing an Image Buffer

Get in touch with us


About The Imaging Source

Established in 1990, The Imaging Source is one of the leading manufacturers of industrial cameras, frame grabbers and video converters for production automation, quality assurance, logistics, medicine, science and security.

Our comprehensive range of cameras with USB 3.1, USB 3.0, USB 2.0, GigE interfaces and other innovative machine vision products are renowned for their high quality and ability to meet the performance requirements of demanding applications.

Automated Imaging Association ISO 9001:2015 certified

Contact us