|
| SparseMat () |
| Various SparseMat constructors.
|
|
| SparseMat (int dims, const int *_sizes, int _type) |
|
| SparseMat (const SparseMat &m) |
|
| SparseMat (const Mat &m) |
|
| ~SparseMat () |
| the destructor
|
|
SparseMat & | operator= (const SparseMat &m) |
| assignment operator. This is O(1) operation, i.e. no data is copied
|
|
SparseMat & | operator= (const Mat &m) |
| equivalent to the corresponding constructor
|
|
CV_NODISCARD_STD SparseMat | clone () const |
| creates full copy of the matrix
|
|
void | copyTo (SparseMat &m) const |
| copies all the data to the destination matrix. All the previous content of m is erased
|
|
void | copyTo (Mat &m) const |
| converts sparse matrix to dense matrix.
|
|
void | convertTo (SparseMat &m, int rtype, double alpha=1) const |
| multiplies all the matrix elements by the specified scale factor alpha and converts the results to the specified data type
|
|
void | convertTo (Mat &m, int rtype, double alpha=1, double beta=0) const |
| converts sparse matrix to dense n-dim matrix with optional type conversion and scaling.
|
|
void | assignTo (SparseMat &m, int type=-1) const |
|
void | create (int dims, const int *_sizes, int _type) |
| reallocates sparse matrix.
|
|
void | clear () |
| sets all the sparse matrix elements to 0, which means clearing the hash table.
|
|
void | addref () |
| manually increments the reference counter to the header.
|
|
void | release () |
|
size_t | elemSize () const |
| converts sparse matrix to the old-style representation; all the elements are copied.
|
|
size_t | elemSize1 () const |
| returns elemSize()/channels()
|
|
int | type () const |
| returns type of sparse matrix elements
|
|
int | depth () const |
| returns the depth of sparse matrix elements
|
|
int | channels () const |
| returns the number of channels
|
|
const int * | size () const |
| returns the array of sizes, or NULL if the matrix is not allocated
|
|
int | size (int i) const |
| returns the size of i-th matrix dimension (or 0)
|
|
int | dims () const |
| returns the matrix dimensionality
|
|
size_t | nzcount () const |
| returns the number of non-zero elements (=the number of hash table nodes)
|
|
size_t | hash (int i0) const |
| computes the element hash value (1D case)
|
|
size_t | hash (int i0, int i1) const |
| computes the element hash value (2D case)
|
|
size_t | hash (int i0, int i1, int i2) const |
| computes the element hash value (3D case)
|
|
size_t | hash (const int *idx) const |
| computes the element hash value (nD case)
|
|
void | erase (int i0, int i1, size_t *hashval=0) |
| erases the specified element (2D case)
|
|
void | erase (int i0, int i1, int i2, size_t *hashval=0) |
| erases the specified element (3D case)
|
|
void | erase (const int *idx, size_t *hashval=0) |
| erases the specified element (nD case)
|
|
SparseMatIterator | end () |
| returns the sparse matrix iterator at the matrix end
|
|
SparseMatConstIterator | end () const |
| returns the read-only sparse matrix iterator at the matrix end
|
|
template<typename _Tp > |
SparseMatIterator_< _Tp > | end () |
| returns the typed sparse matrix iterator at the matrix end
|
|
template<typename _Tp > |
SparseMatConstIterator_< _Tp > | end () const |
| returns the typed read-only sparse matrix iterator at the matrix end
|
|
template<typename _Tp > |
_Tp & | value (Node *n) |
| returns the value stored in the sparse martix node
|
|
template<typename _Tp > |
const _Tp & | value (const Node *n) const |
| returns the value stored in the sparse martix node
|
|
Node * | node (size_t nidx) |
|
const Node * | node (size_t nidx) const |
|
uchar * | newNode (const int *idx, size_t hashval) |
|
void | removeNode (size_t hidx, size_t nidx, size_t previdx) |
|
void | resizeHashTab (size_t newsize) |
|
|
uchar * | ptr (int i0, bool createMissing, size_t *hashval=0) |
| returns pointer to the specified element (1D case)
|
|
uchar * | ptr (int i0, int i1, bool createMissing, size_t *hashval=0) |
| returns pointer to the specified element (2D case)
|
|
uchar * | ptr (int i0, int i1, int i2, bool createMissing, size_t *hashval=0) |
| returns pointer to the specified element (3D case)
|
|
uchar * | ptr (const int *idx, bool createMissing, size_t *hashval=0) |
| returns pointer to the specified element (nD case)
|
|
|
template<typename _Tp > |
_Tp & | ref (int i0, size_t *hashval=0) |
| returns reference to the specified element (1D case)
|
|
template<typename _Tp > |
_Tp & | ref (int i0, int i1, size_t *hashval=0) |
| returns reference to the specified element (2D case)
|
|
template<typename _Tp > |
_Tp & | ref (int i0, int i1, int i2, size_t *hashval=0) |
| returns reference to the specified element (3D case)
|
|
template<typename _Tp > |
_Tp & | ref (const int *idx, size_t *hashval=0) |
| returns reference to the specified element (nD case)
|
|
|
template<typename _Tp > |
_Tp | value (int i0, size_t *hashval=0) const |
| returns value of the specified element (1D case)
|
|
template<typename _Tp > |
_Tp | value (int i0, int i1, size_t *hashval=0) const |
| returns value of the specified element (2D case)
|
|
template<typename _Tp > |
_Tp | value (int i0, int i1, int i2, size_t *hashval=0) const |
| returns value of the specified element (3D case)
|
|
template<typename _Tp > |
_Tp | value (const int *idx, size_t *hashval=0) const |
| returns value of the specified element (nD case)
|
|
|
template<typename _Tp > |
const _Tp * | find (int i0, size_t *hashval=0) const |
| returns pointer to the specified element (1D case)
|
|
template<typename _Tp > |
const _Tp * | find (int i0, int i1, size_t *hashval=0) const |
| returns pointer to the specified element (2D case)
|
|
template<typename _Tp > |
const _Tp * | find (int i0, int i1, int i2, size_t *hashval=0) const |
| returns pointer to the specified element (3D case)
|
|
template<typename _Tp > |
const _Tp * | find (const int *idx, size_t *hashval=0) const |
| returns pointer to the specified element (nD case)
|
|
|
SparseMatIterator | begin () |
| returns the sparse matrix iterator at the matrix beginning
|
|
template<typename _Tp > |
SparseMatIterator_< _Tp > | begin () |
| returns the sparse matrix iterator at the matrix beginning
|
|
SparseMatConstIterator | begin () const |
| returns the read-only sparse matrix iterator at the matrix beginning
|
|
template<typename _Tp > |
SparseMatConstIterator_< _Tp > | begin () const |
| returns the read-only sparse matrix iterator at the matrix beginning
|
|
The class SparseMat represents multi-dimensional sparse numerical arrays.
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:
int size[5] = {10, 10, 10, 10, 10};
for(int i = 0; i < 1000; i++)
{
sparse_mat.ref<
float>(
idx) += 1.f;
}
cout << "nnz = " << sparse_mat.nzcount() << endl;
The class SparseMat represents multi-dimensional sparse numerical arrays.
Definition mat.hpp:2735
const int * size() const
returns the array of sizes, or NULL if the matrix is not allocated
int dims() const
returns the matrix dimensionality
const int * idx
Definition core_c.h:668
#define CV_32F
Definition interface.h:78
CV_EXPORTS OutputArray int double double InputArray OutputArray int int bool double k
Definition imgproc.hpp:2133
- Sparse matrix iterators. They are similar to MatIterator but different from NAryMatIterator. That is, the iteration loop is familiar to STL users:
it = sparse_mat.begin<float>(),
it_end = sparse_mat.end<float>();
double s = 0;
int dims = sparse_mat.dims();
for(; it != it_end; ++it)
{
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);
Template Read-Only Sparse Matrix Iterator Class.
Definition mat.hpp:3338
const _Tp & value() const
template method returning the current matrix element
const SparseMat::Node * node() const
returns the current node of the sparse matrix. it.node->idx is the current element index
sparse matrix node - element of a hash table
Definition mat.hpp:2760
int idx[MAX_DIM]
index of the matrix element
Definition mat.hpp:2766
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:
{
it_end = _a->
end<
float>();
double ccorr = 0;
for(; it != it_end; ++it)
{
float avalue = *it;
float bvalue = _b->value<
float>(anode->
idx,&anode->
hashval);
ccorr += avalue*bvalue;
}
return ccorr;
}
SparseMatIterator end()
returns the sparse matrix iterator at the matrix end
size_t nzcount() const
returns the number of non-zero elements (=the number of hash table nodes)
SparseMatIterator begin()
returns the sparse matrix iterator at the matrix beginning
size_t hashval
hash value
Definition mat.hpp:2762