IC Imaging Control .NET

YGB1

YGB1

YGB1 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 high byte contains the bits 0 to 7, while the most significant bits 8 and 9 are stored in the low 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. The following code shows how to convert to and from the YGB1 format:

[C#]
private UInt16 UInt16ValueFromYGB1Bytes(byte b1, byte b2) { return (UInt16)((b1 << 8) | b2); } private void YGB1BytesFromUInt16Value(UInt16 val, out byte b1, out byte b2) { b1 = (byte)(val >> 8); b2 = (byte)(val & 0xff); }
[VB.NET]
Private Function IntValueFromYGB1Bytes(ByVal b1 As Byte, ByVal b2 As Byte) As Integer Return (CInt(b1) << 8) Or CInt(b2) End Function Private Function YGB1BytesFromIntValue(ByVal val As Integer, ByRef b1 As Byte, ByRef b2 As Byte) b1 = CByte(val >> 8) b2 = CByte(val And 255) End Function

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

YGB1BytesFromUInt16Value 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 YGB1.

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 hand) 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 UInt16ValueFromYGB1Bytes from above is used:

[C#]
// YGB1 is top-down, the first line has index 0 int y = 0; UInt16 val0 = UInt16ValueFromYGB1Bytes(buf[0, y], buf[1, y]); UInt16 val1 = UInt16ValueFromYGB1Bytes(buf[2, y], buf[3, y]); txtOutput.Text = "Image buffer pixel format is YGB1\r\n"; txtOutput.Text += "Pixel 1: " + val0 + "\r\n"; txtOutput.Text += "Pixel 2: " + val1;
[VB.NET]
' YGB1 is top-down, the first line has index 0 Dim y As Integer = 0 Dim val0 As Integer = IntValueFromYGB1Bytes(buf(0, y), buf(1, y)) Dim val1 As Integer = IntValueFromYGB1Bytes(buf(2, y), buf(3, y)) txtOutput.Text = "Image buffer pixel format is YGB1" & 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 YGB1BytesFromUInt16Value from above is used to convert from brightness values to pixel data:

[C#]
byte b1, b2; YGB1BytesFromUInt16Value(0, out b1, out b2); buf[0, y] = b1; buf[1, y] = b2; YGB1BytesFromUInt16Value(512, out b1, out b2); buf[2, y] = b1; buf[3, y] = b2; YGB1BytesFromUInt16Value(1023, out b1, out b2); buf[4, y] = b1; buf[5, y] = b2; buf.SaveAsBitmap("ygb1.png", TIS.Imaging.ICImagingControlColorformats.ICY800);
[VB.NET]
Dim b1, b2 As Byte YGB1BytesFromIntValue(0, b1, b2) buf(0, y) = b1 buf(1, y) = b2 YGB1BytesFromIntValue(512, b1, b2) buf(2, y) = b1 buf(3, y) = b2 YGB1BytesFromIntValue(1023, b1, b2) buf(4, y) = b1 buf(5, y) = b2 buf.SaveAsBitmap("ygb1.png", TIS.Imaging.ICImagingControlColorformats.ICY800)

To check the result, open the saved image and examine the upper left hand 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