IC Imaging Control .NET



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


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 two bytes 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 YGB0 format:

private UInt16 UInt16ValueFromYGB0Bytes(byte b1, byte b2) { return (UInt16)(((b1 << 8) | b2) >> 6); } private void YGB0BytesFromUInt16Value(UInt16 val, out byte b1, out byte b2) { val <<= 6; b1 = (byte)(val >> 8); b2 = (byte)(val & 0xff); }
Private Function IntValueFromYGB0Bytes(ByVal b1 As Byte, ByVal b2 As Byte) As Integer Return (((CInt(b1) << 8) Or CInt(b2)) >> 6) End Function Private Function YGB0BytesFromIntValue(ByVal val As Integer, ByRef b1 As Byte, ByRef b2 As Byte) val <<= 6 b1 = CByte(val >> 8) b2 = CByte(val & 255) End Function

The function UInt16ValueFromYGB0Bytes 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.

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

How to read and write pixel data

A video capture device, video format, FrameHandlerSink with a ImageBuffers collections, which defines the image data color format must 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 FrameHandlerSink.SnapImage is called.

Accessing the buffer

To access the bytes of the image buffer, you can write buf[column,line] in C# and buf(column,line) in VB.NET.

In this example, we want to read out the first (upper left) two pixels of the image. In a second step we manipulate the first 3 pixels. Because RGB images are stored bottom-up, the index of the first line is lines-1.

To calculate the x-coordinate of the pixel data, the number of bytes required for one pixel (2) is multiplied with the column of the pixel to be retrieved. After that, an offset is added to access the two bytes of one pixel.

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

// YGB0 is top-down, the first line has index 0 int y = 0; UInt16 val0 = UInt16ValueFromYGB0Bytes(buf[0 * 2 + 0, y], buf[0 * 2 + 1, y]); UInt16 val1 = UInt16ValueFromYGB0Bytes(buf[1 * 2 + 0, y], buf[1 * 2 + 1, y]); txtOutput.Text = "Image buffer pixel format is YGB0\r\n"; txtOutput.Text += "Pixel 1: " + val0 + "\r\n"; txtOutput.Text += "Pixel 2: " + val1;
' YGB0 is top-down, the first line has index 0 Dim y As Integer = 0 Dim val0 As Integer = IntValueFromYGB0Bytes(buf(0, y), buf(1, y)) Dim val1 As Integer = IntValueFromYGB0Bytes(buf(2, y), buf(3, y)) txtOutput.Text = "Image buffer pixel format is YGB0" & vbCrLf txtOutput.Text &= "Pixel 1: " & val0 & vbCrLf txtOutput.Text &= "Pixel 2: " & val1

Manipulating Image Data

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

byte b1, b2; YGB0BytesFromUInt16Value(0, out b1, out b2); buf[0 * 2 + 0, y] = b1; buf[0 * 2 + 1, y] = b2; YGB0BytesFromUInt16Value(512, out b1, out b2); buf[1 * 2 + 0, y] = b1; buf[1 * 2 + 1, y] = b2; YGB0BytesFromUInt16Value(1023, out b1, out b2); buf[2 * 2 + 0, y] = b1; buf[2 * 2 + 1, y] = b2; buf.SaveAsBitmap("ygb0.png", TIS.Imaging.ICImagingControlColorformats.ICY800);
Dim b1, b2 As Byte YGB0BytesFromIntValue(0, b1, b2) buf(0, y) = b1 buf(1, y) = b2 YGB0BytesFromIntValue(512, b1, b2) buf(2, y) = b1 buf(3, y) = b2 YGB0BytesFromIntValue(1023, b1, b2) buf(4, y) = b1 buf(5, y) = b2 buf.SaveAsBitmap("ygb0.png", TIS.Imaging.ICImagingControlColorformats.ICY800)

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


<< 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