Trait opencv::prelude::SparseMatTraitConst
source · [−]pub trait SparseMatTraitConst {
Show 24 methods
fn as_raw_SparseMat(&self) -> *const c_void;
fn flags(&self) -> i32 { ... }
fn try_clone(&self) -> Result<SparseMat> { ... }
fn copy_to(&self, m: &mut SparseMat) -> Result<()> { ... }
fn copy_to_mat(&self, m: &mut Mat) -> Result<()> { ... }
fn convert_to(&self, m: &mut SparseMat, rtype: i32, alpha: f64) -> Result<()> { ... }
fn convert_to_1(
&self,
m: &mut Mat,
rtype: i32,
alpha: f64,
beta: f64
) -> Result<()> { ... }
fn assign_to(&self, m: &mut SparseMat, typ: i32) -> Result<()> { ... }
fn elem_size(&self) -> size_t { ... }
fn elem_size1(&self) -> size_t { ... }
fn typ(&self) -> i32 { ... }
fn depth(&self) -> i32 { ... }
fn channels(&self) -> i32 { ... }
fn size(&self) -> Result<*const i32> { ... }
fn size_1(&self, i: i32) -> Result<i32> { ... }
fn dims(&self) -> Result<i32> { ... }
fn nzcount(&self) -> Result<size_t> { ... }
fn hash(&self, i0: i32) -> Result<size_t> { ... }
fn hash_1(&self, i0: i32, i1: i32) -> Result<size_t> { ... }
fn hash_2(&self, i0: i32, i1: i32, i2: i32) -> Result<size_t> { ... }
fn hash_3(&self, idx: &i32) -> Result<size_t> { ... }
fn begin(&self) -> Result<SparseMatConstIterator> { ... }
fn end(&self) -> Result<SparseMatConstIterator> { ... }
fn node(&self, nidx: size_t) -> Result<SparseMat_Node> { ... }
}
Expand description
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:
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;
}
Required Methods
fn as_raw_SparseMat(&self) -> *const c_void
Provided Methods
fn flags(&self) -> i32
sourcefn copy_to(&self, m: &mut SparseMat) -> Result<()>
fn copy_to(&self, m: &mut SparseMat) -> Result<()>
copies all the data to the destination matrix. All the previous content of m is erased
sourcefn copy_to_mat(&self, m: &mut Mat) -> Result<()>
fn copy_to_mat(&self, m: &mut Mat) -> Result<()>
converts sparse matrix to dense matrix.
sourcefn convert_to(&self, m: &mut SparseMat, rtype: i32, alpha: f64) -> Result<()>
fn convert_to(&self, m: &mut SparseMat, rtype: i32, alpha: f64) -> Result<()>
multiplies all the matrix elements by the specified scale factor alpha and converts the results to the specified data type
C++ default parameters
- alpha: 1
sourcefn convert_to_1(&self, m: &mut Mat, rtype: i32, alpha: f64, beta: f64) -> Result<()>
fn convert_to_1(&self, m: &mut Mat, rtype: i32, alpha: f64, beta: f64) -> Result<()>
converts sparse matrix to dense n-dim matrix with optional type conversion and scaling.
Parameters
- m:[out] - output matrix; if it does not have a proper size or type before the operation, it is reallocated
- rtype: - 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: - optional scale factor
- beta: - optional delta added to the scaled values
C++ default parameters
- alpha: 1
- beta: 0
sourcefn elem_size(&self) -> size_t
fn elem_size(&self) -> size_t
returns the size of each element in bytes (not including the overhead - the space occupied by SparseMat::Node elements)
sourcefn elem_size1(&self) -> size_t
fn elem_size1(&self) -> size_t
returns elemSize()/channels()
sourcefn size(&self) -> Result<*const i32>
fn size(&self) -> Result<*const i32>
returns the array of sizes, or NULL if the matrix is not allocated
sourcefn nzcount(&self) -> Result<size_t>
fn nzcount(&self) -> Result<size_t>
returns the number of non-zero elements (=the number of hash table nodes)
sourcefn hash_2(&self, i0: i32, i1: i32, i2: i32) -> Result<size_t>
fn hash_2(&self, i0: i32, i1: i32, i2: i32) -> Result<size_t>
computes the element hash value (3D case)
sourcefn begin(&self) -> Result<SparseMatConstIterator>
fn begin(&self) -> Result<SparseMatConstIterator>
returns the read-only sparse matrix iterator at the matrix beginning
sourcefn end(&self) -> Result<SparseMatConstIterator>
fn end(&self) -> Result<SparseMatConstIterator>
returns the read-only sparse matrix iterator at the matrix end