/
SparseMat.cs
139 lines (130 loc) · 4.53 KB
/
SparseMat.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System;
using System.Collections.Generic;
using OpenCV.Net.Native;
namespace OpenCV.Net
{
/// <summary>
/// Represents a multi-dimensional sparse multi-channel array.
/// </summary>
public class SparseMat : Arr
{
internal SparseMat(IntPtr handle, bool ownsHandle)
: base(ownsHandle)
{
SetHandle(handle);
}
/// <summary>
/// Initializes a new instance of the <see cref="SparseMat"/> class with the
/// specified dimension sizes, element bit <paramref name="depth"/> and
/// <paramref name="channels"/> per element.
/// </summary>
/// <param name="dimSizes">The size of each of the multi-dimensional array dimensions.</param>
/// <param name="depth">The bit depth of matrix elements.</param>
/// <param name="channels">The number of channels per element.</param>
public SparseMat(int[] dimSizes, Depth depth, int channels)
: this(NativeMethods.cvCreateSparseMat(dimSizes.Length, dimSizes, MatHelper.GetMatType(depth, channels)), true)
{
}
/// <summary>
/// Gets the bit depth of matrix elements.
/// </summary>
public Depth Depth
{
get
{
unsafe
{
return MatHelper.GetMatDepth(((_CvSparseMat*)handle.ToPointer())->type);
}
}
}
/// <summary>
/// Gets the number of channels per matrix element.
/// </summary>
public int Channels
{
get
{
unsafe
{
return MatHelper.GetMatChannels(((_CvSparseMat*)handle.ToPointer())->type);
}
}
}
/// <summary>
/// Gets the size of each matrix element in bytes.
/// </summary>
public int ElementSize
{
get
{
unsafe
{
return MatHelper.GetElemSize(((_CvSparseMat*)handle.ToPointer())->type);
}
}
}
/// <summary>
/// Creates a new <see cref="SparseMat"/> that is a copy of the current instance.
/// </summary>
/// <returns>
/// A new <see cref="SparseMat"/> that is a copy of this instance.
/// </returns>
public SparseMat Clone()
{
return new SparseMat(NativeMethods.cvCloneSparseMat(this), true);
}
IntPtr GetNextSparseNode(ref _CvSparseMatIterator mat_iterator)
{
unsafe
{
if (mat_iterator.node->next != null)
return (IntPtr)(mat_iterator.node = mat_iterator.node->next);
else
{
int idx;
for (idx = ++mat_iterator.curidx; idx < mat_iterator.mat->hashsize; idx++)
{
var node = (_CvSparseNode*)mat_iterator.mat->hashtable[idx];
if (node != null)
{
mat_iterator.curidx = idx;
return (IntPtr)(mat_iterator.node = node);
}
}
return IntPtr.Zero;
}
}
}
/// <summary>
/// Returns an <see cref="System.Collections.IEnumerable"/> that supports iteration over the element nodes
/// of the sparse array.
/// </summary>
/// <returns>
/// An <see cref="System.Collections.IEnumerable"/> that supports iteration over the element nodes
/// of the sparse array.
/// </returns>
public IEnumerable<SparseNode> GetSparseNodes()
{
_CvSparseMatIterator iterator;
for (var node = NativeMethods.cvInitSparseMatIterator(this, out iterator);
node != IntPtr.Zero;
node = GetNextSparseNode(ref iterator))
{
yield return new SparseNode(node, this);
}
}
/// <summary>
/// Executes the code required to free the native <see cref="SparseMat"/> handle.
/// </summary>
/// <returns>
/// <b>true</b> if the handle is released successfully; otherwise, in the event of a
/// catastrophic failure, <b>false</b>.
/// </returns>
protected override bool ReleaseHandle()
{
NativeMethods.cvReleaseSparseMat(ref handle);
return true;
}
}
}