Table of Contents

Class SparseMat<T>

Namespace
OpenCvSharp
Assembly
OpenCvSharp.dll

Strongly-typed sparse matrix. T determines the element type, so element access needs no per-call type argument and no runtime type checks: reading an absent element returns default without creating it; assigning through the indexer creates it.

public sealed class SparseMat<T> : SparseMat, IDisposable where T : unmanaged

Type Parameters

T

Element type (e.g. int, Vec3b).

Inheritance
SparseMat<T>
Implements
Inherited Members

Constructors

SparseMat(params int[])

Constructs an n-dimensional sparse matrix of the given shape.

public SparseMat(params int[] dimensions)

Parameters

dimensions int[]

Size along each dimension.

Properties

this[int, int]

Gets or sets the element at the given 2-D index. Reading an absent element returns default (without creating it); assigning creates the element.

public T this[int i0, int i1] { get; set; }

Parameters

i0 int
i1 int

Property Value

T

this[int, int, int]

Gets or sets the element at the given 3-D index. Reading an absent element returns default (without creating it); assigning creates the element.

public T this[int i0, int i1, int i2] { get; set; }

Parameters

i0 int
i1 int
i2 int

Property Value

T

this[ReadOnlySpan<int>]

Gets or sets the element at the given n-D index. Reading an absent element returns default (without creating it); assigning creates the element.

public T this[ReadOnlySpan<int> index] { get; set; }

Parameters

index ReadOnlySpan<int>

Property Value

T

Methods

Clone()

Creates a full (deep) copy of the matrix.

public SparseMat<T> Clone()

Returns

SparseMat<T>

Contains(ReadOnlySpan<int>)

Returns whether an element is stored (non-zero) at the given index.

public bool Contains(ReadOnlySpan<int> index)

Parameters

index ReadOnlySpan<int>

Returns

bool

EnumerateNonZero()

Enumerates every stored (non-zero) element together with its index, mirroring cv::SparseMatConstIterator. This visits only the stored elements, not the full dense index space.

public IEnumerable<(int[] Index, T Value)> EnumerateNonZero()

Returns

IEnumerable<(int[] Index, T Value)>

A sequence of (index, value) pairs. Each Index is a fresh array of length Dims(). The enumeration is a snapshot taken when the method is called.

FromMat(Mat)

Creates a typed sparse matrix from a dense Mat. The Mat type must match T.

public static SparseMat<T> FromMat(Mat mat)

Parameters

mat Mat

Returns

SparseMat<T>

GetValueRef(ReadOnlySpan<int>)

Returns a reference to the element at the given index, creating it (zero-initialized) if it does not exist. Useful for in-place updates such as accumulation (sm.GetValueRef(i, j)++), which performs a single hash lookup.

public ref T GetValueRef(ReadOnlySpan<int> index)

Parameters

index ReadOnlySpan<int>

Returns

T

Remarks

The reference points into native storage and is only valid until the next structural change to the matrix (an insertion that triggers a rehash, Clear(), disposal, etc.). Do not retain it across such operations.

Remove(ReadOnlySpan<int>)

Removes the stored element at the given index. Returns false if it was not stored.

public bool Remove(ReadOnlySpan<int> index)

Parameters

index ReadOnlySpan<int>

Returns

bool

TryGetValue(ReadOnlySpan<int>, out T)

Gets the element at the given index if it is stored, without creating it.

public bool TryGetValue(ReadOnlySpan<int> index, out T value)

Parameters

index ReadOnlySpan<int>
value T

Returns

bool