Struct MatRowAccessor<T>
- Namespace
- OpenCvSharp
- Assembly
- OpenCvSharp.dll
Provides zero-allocation, zero-P/Invoke-per-element row access to a 2D Mat. Obtain via AsRows<T>(). The data pointer, step, and dimensions are captured once at construction time, so all subsequent row indexing is pure pointer arithmetic.
public readonly ref struct MatRowAccessor<T> where T : unmanaged
Type Parameters
TUnmanaged element type matching the matrix element type (e.g. Vec3b for CV_8UC3).
- Inherited Members
Remarks
Typical usage for per-pixel iteration:
var rows = mat.AsRows<Vec3b>();
for (int r = 0; r < rows.Count; r++)
{
Span<Vec3b> row = rows[r];
for (int c = 0; c < row.Length; c++)
{
Vec3b pixel = row[c];
}
}
This is a ref struct and therefore cannot be stored in fields, boxed, or used as a
generic type argument.
Parallel usage: MatRowAccessor<T> is safe to use concurrently from multiple threads
as long as each thread writes to a distinct set of rows. Because MatRowAccessor<T> is a
ref struct it cannot be captured by a lambda closure; instead, call AsRows<T>()
inside each iteration to obtain a per-thread local accessor:
Parallel.For(0, mat.Rows, y =>
{
Span<float> row = mat.AsRows<float>()[y];
for (int x = 0; x < row.Length; x++)
row[x] = ComputeValue(y, x);
});
Properties
- Count
Number of rows in the matrix.
- this[int]
Returns a Span<T> over the specified row. No P/Invoke is performed; this is pure pointer arithmetic. The row index is bounds checked. Use DangerousGetRowUnchecked(int) only when the caller has already proved that the index is valid and profiling shows the check is significant.
Methods
- DangerousGetRowUnchecked(int)
Returns a row without validating
row.