Y16

Y16 is a 16-bit grayscale format. The organization of the pixels in the image buffer is from left to right and top down.

Memory Layout

Every pixel is stored as an little-endian unsigned 16-bit integer value.

Some sensors only support 10-bit or 12-bit pixel data, but calculations in the camera or in the driver do have impact on the lower bits, so you shouldn't discard any bits of the 16-bit data available in the image buffers.

How to read and write pixel data

A video capture device, video format, FrameSnapSink must have been setup. The following code fragments show step-by-step how to access and manipulate the pixel data of Y16.

First of all, we have to capture an image. Otherwise, the image buffer would be empty. To do so, live mode is started and FrameSnapSink.SnapSingle is called. This is all done by the GrabImage method.

Accessing the buffer

To access the bytes of the image buffer, you just have to interpret the image buffer as 16-bit unsigned integers. The helper function ReadY16 does that by utilizing System.Runtime.InteropServices.Marshal.ReadInt16:

[C#]
private unsafe UInt16 ReadY16(TIS.Imaging.IFrameQueueBuffer buf, int row, int col) { // Y16 is top-down, the first line has index 0 int offset = row * buf.FrameType.BytesPerLine + col * 2; return (UInt16)System.Runtime.InteropServices.Marshal.ReadInt16(new IntPtr(buf.Ptr), offset); }

In this example, we want to read out the first (upper left hand) two pixels of the image:

[C#]
UInt32 val0 = ReadY16(buf, 0, 0); UInt32 val1 = ReadY16(buf, 0, 1); txtOutput.Text = "Image buffer pixel format is Y16\r\n"; txtOutput.Text += "Pixel 1: " + val0 + "\r\n"; txtOutput.Text += "Pixel 2: " + val1;

Manipulating Image Data

The function WriteY16 is used to write the brightness values into the image data:

[C#]
private unsafe void WriteY16(TIS.Imaging.IFrameQueueBuffer buf, int row, int col, UInt16 value) { int offset = row * buf.FrameType.BytesPerLine + col * 2; System.Runtime.InteropServices.Marshal.WriteInt16( new IntPtr( buf.Ptr ), offset, (short)value); }

Now we set the upper left three pixels to black, gray and white:

[C#]
WriteY16(buf, 0, 0, 0x0000); // Black WriteY16(buf, 0, 1, 0x8000); // Gray WriteY16(buf, 0, 2, 0xFFFF); // White TIS.Imaging.FrameExtensions.SaveAsTiff( buf, "y16.tiff");

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