IC Imaging Control .NET

Y16

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. In this case, the least significant bits are don't-care values.

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 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 FrameHandlerSink.SnapImage is called.

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 UInt16 ReadY16(TIS.Imaging.ImageBuffer buf, int row, int col) { // Y16 is top-down, the first line has index 0 int offset = row * buf.BytesPerLine + col * 2; return (UInt16)System.Runtime.InteropServices.Marshal.ReadInt16(buf.GetIntPtr(), offset); }
[VB.NET]
Private Function ToUInt16(ByVal s As Int16) As UInt16 If (s And &H8000) = 0 Then Return CType(s, UInt16) Else Return CType(UInt16.MaxValue + 1 + CType(s, Int32), UInt16) End If End Function Private Function ReadY16(ByVal buf As TIS.Imaging.ImageBuffer, ByVal row As Integer, ByVal col As Integer) As UInt16 ' Y16 is top-down, the first line has index 0 Dim offset As Integer = row * buf.BytesPerLine + col * 2 Dim val As Int16 = System.Runtime.InteropServices.Marshal.ReadInt16(buf.GetIntPtr(), offset) Return ToUInt16(val) End Function

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;
[VB.NET]
Dim val0 As UInt16 = ReadY16(buf, 0, 0) Dim val1 As UInt16 = ReadY16(buf, 0, 1) txtOutput.Text = "Image buffer pixel format is Y16" & vbCrLf txtOutput.Text &= "Pixel 1: " & val0 & vbCrLf txtOutput.Text &= "Pixel 2: " & val1

Manipulating Image Data

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

[C#]
private void WriteY16(TIS.Imaging.ImageBuffer buf, int row, int col, UInt16 value) { int offset = row * buf.BytesPerLine + col * 2; System.Runtime.InteropServices.Marshal.WriteInt16(buf.GetIntPtr(), offset, (short)value); }
[VB.NET]
Private Function ToInt16(ByVal us As UInt16) As Int16 If (us And &H8000) = 0 Then Return CType(us, Int16) Else Return CType(CType(us, Int32) - UInt16.MaxValue - 1, Int16) End If End Function Private Sub WriteY16(ByVal buf As TIS.Imaging.ImageBuffer, ByVal row As Integer, ByVal col As Integer, ByVal value As UInt16) Dim offset As Integer = row * buf.BytesPerLine + col * 2 System.Runtime.InteropServices.Marshal.WriteInt16(buf.GetIntPtr(), offset, ToInt16(value)) End Sub

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 buf.SaveAsTiff("y16.tiff");
[VB.NET]
WriteY16(buf, 0, 0, &H0) ' Black WriteY16(buf, 0, 1, &H8000) ' Gray WriteY16(buf, 0, 2, &HFFFF) ' White buf.SaveAsTiff("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

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