@Namespace(value="cv") @NoOffset @Properties(inherit=opencv_core.class) public class SparseMat extends Pointer
Such a sparse array can store elements of any type that Mat can store. *Sparse* means that only non-zero elements are stored (though, as a result of operations on a sparse matrix, some of its stored elements can actually become 0. It is up to you to detect such elements and delete them using SparseMat::erase ). The non-zero elements are stored in a hash table that grows when it is filled so that the search time is O(1) in average (regardless of whether element is there or not). Elements can be accessed using the following methods: - Query operations (SparseMat::ptr and the higher-level SparseMat::ref, SparseMat::value and SparseMat::find), for example:
const int dims = 5;
int size[5] = {10, 10, 10, 10, 10};
SparseMat sparse_mat(dims, size, CV_32F);
for(int i = 0; i < 1000; i++)
{
int idx[dims];
for(int k = 0; k < dims; k++)
idx[k] = rand() % size[k];
sparse_mat.ref<float>(idx) += 1.f;
}
cout << "nnz = " << sparse_mat.nzcount() << endl;
- Sparse matrix iterators. They are similar to MatIterator but different from NAryMatIterator.
That is, the iteration loop is familiar to STL users:
// prints elements of a sparse floating-point matrix
// and the sum of elements.
SparseMatConstIterator_<float>
it = sparse_mat.begin<float>(),
it_end = sparse_mat.end<float>();
double s = 0;
int dims = sparse_mat.dims();
for(; it != it_end; ++it)
{
// print element indices and the element value
const SparseMat::Node* n = it.node();
printf("(");
for(int i = 0; i < dims; i++)
printf("%d%s", n->idx[i], i < dims-1 ? ", " : ")");
printf(": %g\n", it.value<float>());
s += *it;
}
printf("Element sum is %g\n", s);
If you run this loop, you will notice that elements are not enumerated in a logical order
(lexicographical, and so on). They come in the same order as they are stored in the hash table
(semi-randomly). You may collect pointers to the nodes and sort them to get the proper ordering.
Note, however, that pointers to the nodes may become invalid when you add more elements to the
matrix. This may happen due to possible buffer reallocation.
- Combination of the above 2 methods when you need to process 2 or more sparse matrices
simultaneously. For example, this is how you can compute unnormalized cross-correlation of the 2
floating-point sparse matrices:
double cross_corr(const SparseMat& a, const SparseMat& b)
{
const SparseMat *_a = &a, *_b = &b;
// if b contains less elements than a,
// it is faster to iterate through b
if(_a->nzcount() > _b->nzcount())
std::swap(_a, _b);
SparseMatConstIterator_<float> it = _a->begin<float>(),
it_end = _a->end<float>();
double ccorr = 0;
for(; it != it_end; ++it)
{
// take the next element from the first matrix
float avalue = *it;
const Node* anode = it.node();
// and try to find an element with the same index in the second matrix.
// since the hash value depends only on the element index,
// reuse the hash value stored in the node
float bvalue = _b->value<float>(anode->idx,&anode->hashval);
ccorr += avalue*bvalue;
}
return ccorr;
}
Modifier and Type | Class and Description |
---|---|
static class |
SparseMat.Hdr
the sparse matrix header
|
static class |
SparseMat.Node
sparse matrix node - element of a hash table
|
Pointer.CustomDeallocator, Pointer.Deallocator, Pointer.NativeDeallocator, Pointer.ReferenceCounter
Modifier and Type | Field and Description |
---|---|
static int |
HASH_BIT
enum cv::SparseMat::
|
static int |
HASH_SCALE
enum cv::SparseMat::
|
static int |
MAGIC_VAL
enum cv::SparseMat::
|
static int |
MAX_DIM
enum cv::SparseMat::
|
Constructor and Description |
---|
SparseMat()
\brief Various SparseMat constructors.
|
SparseMat(int dims,
int[] _sizes,
int _type) |
SparseMat(int dims,
IntBuffer _sizes,
int _type) |
SparseMat(int dims,
IntPointer _sizes,
int _type)
\overload
|
SparseMat(long size)
Native array allocator.
|
SparseMat(Mat m)
\overload
|
SparseMat(Pointer p)
Pointer cast constructor.
|
SparseMat(SparseMat m)
\overload
|
Modifier and Type | Method and Description |
---|---|
void |
addref()
manually increments the reference counter to the header.
|
void |
assignTo(SparseMat m) |
void |
assignTo(SparseMat m,
int type) |
SparseMatIterator |
begin()
returns the sparse matrix iterator at the matrix beginning
|
int |
channels()
returns the number of channels
|
void |
clear()
sets all the sparse matrix elements to 0, which means clearing the hash table.
|
SparseMat |
clone()
creates full copy of the matrix
|
void |
convertTo(Mat m,
int rtype) |
void |
convertTo(Mat m,
int rtype,
double alpha,
double beta)
converts sparse matrix to dense n-dim matrix with optional type conversion and scaling.
|
void |
convertTo(SparseMat m,
int rtype) |
void |
convertTo(SparseMat m,
int rtype,
double alpha)
multiplies all the matrix elements by the specified scale factor alpha and converts the results to the specified data type
|
void |
copyTo(Mat m)
converts sparse matrix to dense matrix.
|
void |
copyTo(SparseMat m)
copies all the data to the destination matrix.
|
void |
create(int dims,
int[] _sizes,
int _type) |
void |
create(int dims,
IntBuffer _sizes,
int _type) |
void |
create(int dims,
IntPointer _sizes,
int _type)
reallocates sparse matrix.
|
int |
depth()
returns the depth of sparse matrix elements
|
int |
dims()
returns the matrix dimensionality
|
long |
elemSize()
returns the size of each element in bytes (not including the overhead - the space occupied by SparseMat::Node elements)
|
long |
elemSize1()
returns elemSize()/channels()
|
SparseMatIterator |
end()
returns the sparse matrix iterator at the matrix end
|
void |
erase(int[] idx) |
void |
erase(int[] idx,
SizeTPointer hashval) |
void |
erase(IntBuffer idx) |
void |
erase(IntBuffer idx,
SizeTPointer hashval) |
void |
erase(int i0,
int i1) |
void |
erase(int i0,
int i1,
int i2) |
void |
erase(int i0,
int i1,
int i2,
SizeTPointer hashval)
erases the specified element (3D case)
|
void |
erase(int i0,
int i1,
SizeTPointer hashval)
\}
|
void |
erase(IntPointer idx) |
void |
erase(IntPointer idx,
SizeTPointer hashval)
erases the specified element (nD case)
|
int |
flags() |
SparseMat |
flags(int setter) |
long |
hash(int i0)
computes the element hash value (1D case)
|
long |
hash(int[] idx) |
long |
hash(IntBuffer idx) |
long |
hash(int i0,
int i1)
computes the element hash value (2D case)
|
long |
hash(int i0,
int i1,
int i2)
computes the element hash value (3D case)
|
long |
hash(IntPointer idx)
computes the element hash value (nD case)
|
SparseMat.Hdr |
hdr() |
SparseMat |
hdr(SparseMat.Hdr setter) |
byte[] |
newNode(int[] idx,
long hashval) |
ByteBuffer |
newNode(IntBuffer idx,
long hashval) |
BytePointer |
newNode(IntPointer idx,
long hashval) |
SparseMat.Node |
node(long nidx)
returns the value stored in the sparse martix node
|
long |
nzcount()
returns the number of non-zero elements (=the number of hash table nodes)
|
SparseMat |
position(long position) |
byte[] |
ptr(int[] idx,
boolean createMissing) |
byte[] |
ptr(int[] idx,
boolean createMissing,
SizeTPointer hashval) |
BytePointer |
ptr(int i0,
boolean createMissing) |
BytePointer |
ptr(int i0,
boolean createMissing,
SizeTPointer hashval)
returns pointer to the specified element (1D case)
|
ByteBuffer |
ptr(IntBuffer idx,
boolean createMissing) |
ByteBuffer |
ptr(IntBuffer idx,
boolean createMissing,
SizeTPointer hashval) |
BytePointer |
ptr(int i0,
int i1,
boolean createMissing) |
BytePointer |
ptr(int i0,
int i1,
boolean createMissing,
SizeTPointer hashval)
returns pointer to the specified element (2D case)
|
BytePointer |
ptr(int i0,
int i1,
int i2,
boolean createMissing) |
BytePointer |
ptr(int i0,
int i1,
int i2,
boolean createMissing,
SizeTPointer hashval)
returns pointer to the specified element (3D case)
|
BytePointer |
ptr(IntPointer idx,
boolean createMissing) |
BytePointer |
ptr(IntPointer idx,
boolean createMissing,
SizeTPointer hashval)
returns pointer to the specified element (nD case)
|
SparseMat |
put(Mat m)
equivalent to the corresponding constructor
|
SparseMat |
put(SparseMat m)
assignment operator.
|
void |
release() |
void |
removeNode(long hidx,
long nidx,
long previdx) |
void |
resizeHashTab(long newsize) |
IntPointer |
size()
returns the array of sizes, or NULL if the matrix is not allocated
|
int |
size(int i)
returns the size of i-th matrix dimension (or 0)
|
int |
type()
returns type of sparse matrix elements
|
address, asBuffer, asByteBuffer, availablePhysicalBytes, calloc, capacity, capacity, close, deallocate, deallocate, deallocateReferences, deallocator, deallocator, equals, fill, formatBytes, free, hashCode, isNull, isNull, limit, limit, malloc, maxBytes, maxPhysicalBytes, memchr, memcmp, memcpy, memmove, memset, offsetof, parseBytes, physicalBytes, position, put, realloc, referenceCount, releaseReference, retainReference, setNull, sizeof, toString, totalBytes, totalPhysicalBytes, withDeallocator, zero
public static final int MAGIC_VAL
public static final int MAX_DIM
public static final int HASH_SCALE
public static final int HASH_BIT
public SparseMat(Pointer p)
Pointer.Pointer(Pointer)
.public SparseMat(long size)
Pointer.position(long)
.public SparseMat()
public SparseMat(int dims, @Const IntPointer _sizes, int _type)
dims
- Array dimensionality._sizes
- Sparce matrix size on all dementions._type
- Sparse matrix data type.public SparseMat(int dims, @Const int[] _sizes, int _type)
public SparseMat(@Const @ByRef SparseMat m)
m
- Source matrix for copy constructor. If m is dense matrix (ocvMat) then it will be converted
to sparse representation.@ByRef @Name(value="operator =") public SparseMat put(@Const @ByRef SparseMat m)
@ByRef @Name(value="operator =") public SparseMat put(@Const @ByRef Mat m)
public void copyTo(@ByRef SparseMat m)
public void convertTo(@ByRef SparseMat m, int rtype, double alpha)
public void convertTo(@ByRef Mat m, int rtype, double alpha, double beta)
m
- [out] - output matrix; if it does not have a proper size or type before the operation,
it is reallocatedrtype
- [in] - desired output matrix type or, rather, the depth since the number of channels
are the same as the input has; if rtype is negative, the output matrix will have the
same type as the input.alpha
- [in] - optional scale factorbeta
- [in] - optional delta added to the scaled valuespublic void create(int dims, @Const IntPointer _sizes, int _type)
public void create(int dims, @Const int[] _sizes, int _type)
public void clear()
public void addref()
public void release()
@Cast(value="size_t") public long elemSize()
public int type()
public int depth()
public int channels()
@Const public IntPointer size()
public int size(int i)
public int dims()
@Cast(value="size_t") public long nzcount()
@Cast(value="size_t") public long hash(int i0, int i1)
@Cast(value="size_t") public long hash(int i0, int i1, int i2)
@Cast(value="size_t") public long hash(@Const IntPointer idx)
@Cast(value="uchar*") public BytePointer ptr(int i0, @Cast(value="bool") boolean createMissing, @Cast(value="size_t*") SizeTPointer hashval)
@Cast(value="uchar*") public BytePointer ptr(int i0, @Cast(value="bool") boolean createMissing)
@Cast(value="uchar*") public BytePointer ptr(int i0, int i1, @Cast(value="bool") boolean createMissing, @Cast(value="size_t*") SizeTPointer hashval)
@Cast(value="uchar*") public BytePointer ptr(int i0, int i1, @Cast(value="bool") boolean createMissing)
@Cast(value="uchar*") public BytePointer ptr(int i0, int i1, int i2, @Cast(value="bool") boolean createMissing, @Cast(value="size_t*") SizeTPointer hashval)
@Cast(value="uchar*") public BytePointer ptr(int i0, int i1, int i2, @Cast(value="bool") boolean createMissing)
@Cast(value="uchar*") public BytePointer ptr(@Const IntPointer idx, @Cast(value="bool") boolean createMissing, @Cast(value="size_t*") SizeTPointer hashval)
@Cast(value="uchar*") public BytePointer ptr(@Const IntPointer idx, @Cast(value="bool") boolean createMissing)
@Cast(value="uchar*") public ByteBuffer ptr(@Const IntBuffer idx, @Cast(value="bool") boolean createMissing, @Cast(value="size_t*") SizeTPointer hashval)
@Cast(value="uchar*") public ByteBuffer ptr(@Const IntBuffer idx, @Cast(value="bool") boolean createMissing)
@Cast(value="uchar*") public byte[] ptr(@Const int[] idx, @Cast(value="bool") boolean createMissing, @Cast(value="size_t*") SizeTPointer hashval)
@Cast(value="uchar*") public byte[] ptr(@Const int[] idx, @Cast(value="bool") boolean createMissing)
public void erase(int i0, int i1, @Cast(value="size_t*") SizeTPointer hashval)
erases the specified element (2D case)
public void erase(int i0, int i1)
public void erase(int i0, int i1, int i2, @Cast(value="size_t*") SizeTPointer hashval)
public void erase(int i0, int i1, int i2)
public void erase(@Const IntPointer idx, @Cast(value="size_t*") SizeTPointer hashval)
public void erase(@Const IntPointer idx)
public void erase(@Const IntBuffer idx, @Cast(value="size_t*") SizeTPointer hashval)
public void erase(@Const int[] idx, @Cast(value="size_t*") SizeTPointer hashval)
public void erase(@Const int[] idx)
@ByVal public SparseMatIterator begin()
@ByVal public SparseMatIterator end()
public SparseMat.Node node(@Cast(value="size_t") long nidx)
@Cast(value="uchar*") public BytePointer newNode(@Const IntPointer idx, @Cast(value="size_t") long hashval)
@Cast(value="uchar*") public ByteBuffer newNode(@Const IntBuffer idx, @Cast(value="size_t") long hashval)
@Cast(value="uchar*") public byte[] newNode(@Const int[] idx, @Cast(value="size_t") long hashval)
public void removeNode(@Cast(value="size_t") long hidx, @Cast(value="size_t") long nidx, @Cast(value="size_t") long previdx)
public int flags()
public SparseMat flags(int setter)
public SparseMat.Hdr hdr()
public SparseMat hdr(SparseMat.Hdr setter)
Copyright © 2020. All rights reserved.