Common Vision Blox 15.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Friends Modules Pages
Image Pixel Access in .NET

In CVB .NET, there are two ways to access pixel data:

Fast Linear Access

If the memory layout is linear, the most efficient way to access pixel data is through linear access:

int width = 16;
int height = 4;
using (var image = new Image(width, height, 1, PixelDataType.UInt, 8)) // (1)
{
var dataType = image.Planes[0].DataType;
LinearAccessData<byte> linearAccess;
if (image.Planes[0].TryGetLinearAccess<byte>(out linearAccess)) // (2)
{
unsafe
{
var pBase = (byte*)linearAccess.BasePtr; // (3)
var yInc = (byte)linearAccess.YInc;
var xInc = (byte)linearAccess.XInc;
for (int y = 0; y < height; y++)
{
byte* pLine = pBase + yInc * y; // (4)
for (int x = 0; x < width; x++)
{
byte* pPixel = pLine + xInc * x; // (5)
*pPixel = 42; // (6)
// Alternatively, you can access pixel values more conveniently, though at the cost of performance.
linearAccess[x, y] = 42; // (7)
}
}
}
}
}
__int3264 Image

(1) A monochrome 8-bit unsigned image is created. For monochrome images, the Create function generates an image with a contiguous linear layout.
(2) Linear access is obtained for the first image plane.
(3) The base pointer to the image is obtained as well as the x and y increments.
(4) The pointer to the current line is computed.
(5) The pointer to the current pixel is computed.
(5) Pixel value is read.
(6) Pixel value is modified.
(7) Alternatively, you can access pixel values more conveniently using linearAccess[x, y], though at the cost of performance.

If linear access is not possible, the last resort is to use the slowest method: accessing the image via VPAT.

General Access with VPAT

When linear access is not available, the next option is to access the image through the slower method: VPAT-based access, as described in VPAT Memory Layout.

var vpat = image.Planes[0].GetVPATAccess<byte>(); // (1)
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
var value = vpat[x, y]; // (2)
}
}

(1) Obtain VPA table with the appropriate type.
(2) Access the pixel value.