pub struct SparseMat { /* private fields */ }
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;
}
Implementations§
Trait Implementations§
Source§impl Boxed for SparseMat
impl Boxed for SparseMat
Source§unsafe fn from_raw(ptr: <SparseMat as OpenCVFromExtern>::ExternReceive) -> Self
unsafe fn from_raw(ptr: <SparseMat as OpenCVFromExtern>::ExternReceive) -> Self
Source§fn into_raw(self) -> <SparseMat as OpenCVTypeExternContainer>::ExternSendMut
fn into_raw(self) -> <SparseMat as OpenCVTypeExternContainer>::ExternSendMut
Source§fn as_raw(&self) -> <SparseMat as OpenCVTypeExternContainer>::ExternSend
fn as_raw(&self) -> <SparseMat as OpenCVTypeExternContainer>::ExternSend
Source§fn as_raw_mut(
&mut self,
) -> <SparseMat as OpenCVTypeExternContainer>::ExternSendMut
fn as_raw_mut( &mut self, ) -> <SparseMat as OpenCVTypeExternContainer>::ExternSendMut
Source§impl SparseMatTrait for SparseMat
impl SparseMatTrait for SparseMat
fn as_raw_mut_SparseMat(&mut self) -> *mut c_void
fn set_flags(&mut self, val: i32)
fn hdr(&mut self) -> SparseMat_Hdr
fn set_hdr(&mut self, val: &impl SparseMat_HdrTraitConst)
Source§fn set(&mut self, m: &impl SparseMatTraitConst) -> Result<()>
fn set(&mut self, m: &impl SparseMatTraitConst) -> Result<()>
Source§fn set_mat(&mut self, m: &impl MatTraitConst) -> Result<()>
fn set_mat(&mut self, m: &impl MatTraitConst) -> Result<()>
Source§fn create(&mut self, _sizes: &[i32], _type: i32) -> Result<()>
fn create(&mut self, _sizes: &[i32], _type: i32) -> Result<()>
Source§fn clear(&mut self) -> Result<()>
fn clear(&mut self) -> Result<()>
Source§unsafe fn addref(&mut self) -> Result<()>
unsafe fn addref(&mut self) -> Result<()>
unsafe fn release(&mut self) -> Result<()>
Source§fn ptr(
&mut self,
i0: i32,
create_missing: bool,
hashval: &mut size_t,
) -> Result<*mut u8>
fn ptr( &mut self, i0: i32, create_missing: bool, hashval: &mut size_t, ) -> Result<*mut u8>
Source§fn ptr_def(&mut self, i0: i32, create_missing: bool) -> Result<*mut u8>
fn ptr_def(&mut self, i0: i32, create_missing: bool) -> Result<*mut u8>
Source§fn ptr_1(
&mut self,
i0: i32,
i1: i32,
create_missing: bool,
hashval: &mut size_t,
) -> Result<*mut u8>
fn ptr_1( &mut self, i0: i32, i1: i32, create_missing: bool, hashval: &mut size_t, ) -> Result<*mut u8>
Source§fn ptr_def_1(
&mut self,
i0: i32,
i1: i32,
create_missing: bool,
) -> Result<*mut u8>
fn ptr_def_1( &mut self, i0: i32, i1: i32, create_missing: bool, ) -> Result<*mut u8>
Source§fn ptr_2(
&mut self,
i0: i32,
i1: i32,
i2: i32,
create_missing: bool,
hashval: &mut size_t,
) -> Result<*mut u8>
fn ptr_2( &mut self, i0: i32, i1: i32, i2: i32, create_missing: bool, hashval: &mut size_t, ) -> Result<*mut u8>
Source§fn ptr_def_2(
&mut self,
i0: i32,
i1: i32,
i2: i32,
create_missing: bool,
) -> Result<*mut u8>
fn ptr_def_2( &mut self, i0: i32, i1: i32, i2: i32, create_missing: bool, ) -> Result<*mut u8>
Source§fn ptr_3(
&mut self,
idx: &i32,
create_missing: bool,
hashval: &mut size_t,
) -> Result<*mut u8>
fn ptr_3( &mut self, idx: &i32, create_missing: bool, hashval: &mut size_t, ) -> Result<*mut u8>
Source§fn ptr_def_3(&mut self, idx: &i32, create_missing: bool) -> Result<*mut u8>
fn ptr_def_3(&mut self, idx: &i32, create_missing: bool) -> Result<*mut u8>
Source§fn erase(&mut self, i0: i32, i1: i32, hashval: &mut size_t) -> Result<()>
fn erase(&mut self, i0: i32, i1: i32, hashval: &mut size_t) -> Result<()>
Source§fn erase_def(&mut self, i0: i32, i1: i32) -> Result<()>
fn erase_def(&mut self, i0: i32, i1: i32) -> Result<()>
Source§fn erase_1(
&mut self,
i0: i32,
i1: i32,
i2: i32,
hashval: &mut size_t,
) -> Result<()>
fn erase_1( &mut self, i0: i32, i1: i32, i2: i32, hashval: &mut size_t, ) -> Result<()>
Source§fn erase_def_1(&mut self, i0: i32, i1: i32, i2: i32) -> Result<()>
fn erase_def_1(&mut self, i0: i32, i1: i32, i2: i32) -> Result<()>
Source§fn erase_2(&mut self, idx: &i32, hashval: &mut size_t) -> Result<()>
fn erase_2(&mut self, idx: &i32, hashval: &mut size_t) -> Result<()>
Source§fn erase_def_2(&mut self, idx: &i32) -> Result<()>
fn erase_def_2(&mut self, idx: &i32) -> Result<()>
Source§fn begin_mut(&mut self) -> Result<SparseMatIterator>
fn begin_mut(&mut self) -> Result<SparseMatIterator>
Source§fn end_mut(&mut self) -> Result<SparseMatIterator>
fn end_mut(&mut self) -> Result<SparseMatIterator>
Source§fn node_1(&mut self, nidx: size_t) -> Result<SparseMat_Node>
fn node_1(&mut self, nidx: size_t) -> Result<SparseMat_Node>
fn new_node(&mut self, idx: &i32, hashval: size_t) -> Result<*mut u8>
fn remove_node( &mut self, hidx: size_t, nidx: size_t, previdx: size_t, ) -> Result<()>
fn resize_hash_tab(&mut self, newsize: size_t) -> Result<()>
Source§impl SparseMatTraitConst for SparseMat
impl SparseMatTraitConst for SparseMat
fn as_raw_SparseMat(&self) -> *const c_void
fn flags(&self) -> i32
Source§fn copy_to(&self, m: &mut impl SparseMatTrait) -> Result<()>
fn copy_to(&self, m: &mut impl SparseMatTrait) -> Result<()>
Source§fn copy_to_mat(&self, m: &mut impl MatTrait) -> Result<()>
fn copy_to_mat(&self, m: &mut impl MatTrait) -> Result<()>
Source§fn convert_to(
&self,
m: &mut impl SparseMatTrait,
rtype: i32,
alpha: f64,
) -> Result<()>
fn convert_to( &self, m: &mut impl SparseMatTrait, rtype: i32, alpha: f64, ) -> Result<()>
Source§fn convert_to_def(&self, m: &mut impl SparseMatTrait, rtype: i32) -> Result<()>
fn convert_to_def(&self, m: &mut impl SparseMatTrait, rtype: i32) -> Result<()>
Source§fn convert_to_1(
&self,
m: &mut impl MatTrait,
rtype: i32,
alpha: f64,
beta: f64,
) -> Result<()>
fn convert_to_1( &self, m: &mut impl MatTrait, rtype: i32, alpha: f64, beta: f64, ) -> Result<()>
Source§fn convert_to_def_1(&self, m: &mut impl MatTrait, rtype: i32) -> Result<()>
fn convert_to_def_1(&self, m: &mut impl MatTrait, rtype: i32) -> Result<()>
Source§fn assign_to(&self, m: &mut impl SparseMatTrait, typ: i32) -> Result<()>
fn assign_to(&self, m: &mut impl SparseMatTrait, typ: i32) -> Result<()>
Source§fn assign_to_def(&self, m: &mut impl SparseMatTrait) -> Result<()>
fn assign_to_def(&self, m: &mut impl SparseMatTrait) -> Result<()>
Source§fn elem_size(&self) -> size_t
fn elem_size(&self) -> size_t
Source§fn elem_size1(&self) -> size_t
fn elem_size1(&self) -> size_t
Source§fn size(&self) -> Result<*const i32>
fn size(&self) -> Result<*const i32>
Source§fn nzcount(&self) -> Result<size_t>
fn nzcount(&self) -> Result<size_t>
Source§fn hash_1(&self, i0: i32, i1: i32) -> Result<size_t>
fn hash_1(&self, i0: i32, i1: i32) -> Result<size_t>
Source§fn 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>
Source§fn begin(&self) -> Result<SparseMatConstIterator>
fn begin(&self) -> Result<SparseMatConstIterator>
Source§fn end(&self) -> Result<SparseMatConstIterator>
fn end(&self) -> Result<SparseMatConstIterator>
fn node(&self, nidx: size_t) -> Result<SparseMat_Node>
impl Send for SparseMat
Auto Trait Implementations§
impl Freeze for SparseMat
impl RefUnwindSafe for SparseMat
impl !Sync for SparseMat
impl Unpin for SparseMat
impl UnwindSafe for SparseMat
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Mat> ModifyInplace for Matwhere
Mat: Boxed,
impl<Mat> ModifyInplace for Matwhere
Mat: Boxed,
Source§unsafe fn modify_inplace<Res>(
&mut self,
f: impl FnOnce(&Mat, &mut Mat) -> Res,
) -> Res
unsafe fn modify_inplace<Res>( &mut self, f: impl FnOnce(&Mat, &mut Mat) -> Res, ) -> Res
Mat
or another similar object. By passing
a mutable reference to the Mat
to this function your closure will get called with the read reference and a write references
to the same Mat
. This is unsafe in a general case as it leads to having non-exclusive mutable access to the internal data,
but it can be useful for some performance sensitive operations. One example of an OpenCV function that allows such in-place
modification is imgproc::threshold
. Read more