[−][src]Struct opencv::core::SparseMat
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; }
Methods
impl SparseMat
[src]
pub fn as_raw_SparseMat(&self) -> *mut c_void
[src]
pub unsafe fn from_raw_ptr(ptr: *mut c_void) -> Self
[src]
impl SparseMat
[src]
pub fn default() -> Result<SparseMat>
[src]
Various SparseMat constructors.
pub fn new(dims: i32, _sizes: &i32, _type: i32) -> Result<SparseMat>
[src]
Various SparseMat constructors.
Overloaded parameters
Parameters
- dims: Array dimensionality.
- _sizes: Sparce matrix size on all dementions.
- _type: Sparse matrix data type.
pub fn copy(m: &SparseMat) -> Result<SparseMat>
[src]
Various SparseMat constructors.
Overloaded parameters
Parameters
- m: Source matrix for copy constructor. If m is dense matrix (ocvMat) then it will be converted to sparse representation.
pub fn from_mat(m: &Mat) -> Result<SparseMat>
[src]
Various SparseMat constructors.
Overloaded parameters
Parameters
- m: Source matrix for copy constructor. If m is dense matrix (ocvMat) then it will be converted to sparse representation.
Trait Implementations
impl Drop for SparseMat
[src]
impl Send for SparseMat
[src]
impl SparseMatTrait for SparseMat
[src]
fn as_raw_SparseMat(&self) -> *mut c_void
[src]
fn flags(&self) -> i32
[src]
fn set_flags(&mut self, val: i32)
[src]
fn hdr(&mut self) -> SparseMat_Hdr
[src]
fn set_hdr(&mut self, val: &mut SparseMat_Hdr)
[src]
fn clone(&self) -> Result<SparseMat>
[src]
fn copy_to(&self, m: &mut SparseMat) -> Result<()>
[src]
fn copy_to_mat(&self, m: &mut Mat) -> Result<()>
[src]
fn convert_to(&self, m: &mut SparseMat, rtype: i32, alpha: f64) -> Result<()>
[src]
fn convert_to_1(
&self,
m: &mut Mat,
rtype: i32,
alpha: f64,
beta: f64
) -> Result<()>
[src]
&self,
m: &mut Mat,
rtype: i32,
alpha: f64,
beta: f64
) -> Result<()>
fn assign_to(&self, m: &mut SparseMat, typ: i32) -> Result<()>
[src]
fn create(&mut self, dims: i32, _sizes: &i32, _type: i32) -> Result<()>
[src]
fn clear(&mut self) -> Result<()>
[src]
fn addref(&mut self) -> Result<()>
[src]
fn release(&mut self) -> Result<()>
[src]
fn elem_size(&self) -> Result<size_t>
[src]
fn elem_size1(&self) -> Result<size_t>
[src]
fn typ(&self) -> Result<i32>
[src]
fn depth(&self) -> Result<i32>
[src]
fn channels(&self) -> Result<i32>
[src]
fn size(&self) -> Result<&i32>
[src]
fn size_1(&self, i: i32) -> Result<i32>
[src]
fn dims(&self) -> Result<i32>
[src]
fn nzcount(&self) -> Result<size_t>
[src]
fn hash(&self, i0: i32) -> Result<size_t>
[src]
fn hash_1(&self, i0: i32, i1: i32) -> Result<size_t>
[src]
fn hash_2(&self, i0: i32, i1: i32, i2: i32) -> Result<size_t>
[src]
fn hash_3(&self, idx: &i32) -> Result<size_t>
[src]
fn ptr(
&mut self,
i0: i32,
create_missing: bool,
hashval: &mut size_t
) -> Result<&mut u8>
[src]
&mut self,
i0: 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>
[src]
&mut self,
i0: i32,
i1: 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>
[src]
&mut self,
i0: i32,
i1: i32,
i2: 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>
[src]
&mut self,
idx: &i32,
create_missing: bool,
hashval: &mut size_t
) -> Result<&mut u8>
fn erase(&mut self, i0: i32, i1: i32, hashval: &mut size_t) -> Result<()>
[src]
fn erase_1(
&mut self,
i0: i32,
i1: i32,
i2: i32,
hashval: &mut size_t
) -> Result<()>
[src]
&mut self,
i0: i32,
i1: i32,
i2: i32,
hashval: &mut size_t
) -> Result<()>
fn erase_2(&mut self, idx: &i32, hashval: &mut size_t) -> Result<()>
[src]
fn begin_mut(&mut self) -> Result<SparseMatIterator>
[src]
fn begin(&self) -> Result<SparseMatConstIterator>
[src]
fn end_mut(&mut self) -> Result<SparseMatIterator>
[src]
fn end(&self) -> Result<SparseMatConstIterator>
[src]
fn node(&mut self, nidx: size_t) -> Result<SparseMat_Node>
[src]
fn node_1(&self, nidx: size_t) -> Result<SparseMat_Node>
[src]
fn new_node(&mut self, idx: &i32, hashval: size_t) -> Result<&mut u8>
[src]
fn remove_node(
&mut self,
hidx: size_t,
nidx: size_t,
previdx: size_t
) -> Result<()>
[src]
&mut self,
hidx: size_t,
nidx: size_t,
previdx: size_t
) -> Result<()>
fn resize_hash_tab(&mut self, newsize: size_t) -> Result<()>
[src]
Auto Trait Implementations
impl RefUnwindSafe for SparseMat
impl !Sync for SparseMat
impl Unpin for SparseMat
impl UnwindSafe for SparseMat
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,