[][src]Trait opencv::core::SparseMatTrait

pub trait SparseMatTrait {
    pub fn as_raw_SparseMat(&self) -> *const c_void;
pub fn as_raw_mut_SparseMat(&mut self) -> *mut c_void; pub fn flags(&self) -> i32 { ... }
pub fn set_flags(&mut self, val: i32) { ... }
pub fn hdr(&mut self) -> SparseMat_Hdr { ... }
pub fn set_hdr(&mut self, val: &mut SparseMat_Hdr) { ... }
pub fn try_clone(&self) -> Result<SparseMat> { ... }
pub fn copy_to(&self, m: &mut SparseMat) -> Result<()> { ... }
pub fn copy_to_mat(&self, m: &mut Mat) -> Result<()> { ... }
pub fn convert_to(
        &self,
        m: &mut SparseMat,
        rtype: i32,
        alpha: f64
    ) -> Result<()> { ... }
pub fn convert_to_1(
        &self,
        m: &mut Mat,
        rtype: i32,
        alpha: f64,
        beta: f64
    ) -> Result<()> { ... }
pub fn assign_to(&self, m: &mut SparseMat, typ: i32) -> Result<()> { ... }
pub fn create(&mut self, dims: i32, _sizes: &i32, _type: i32) -> Result<()> { ... }
pub fn clear(&mut self) -> Result<()> { ... }
pub fn addref(&mut self) -> Result<()> { ... }
pub fn release(&mut self) -> Result<()> { ... }
pub fn elem_size(&self) -> Result<size_t> { ... }
pub fn elem_size1(&self) -> Result<size_t> { ... }
pub fn typ(&self) -> Result<i32> { ... }
pub fn depth(&self) -> Result<i32> { ... }
pub fn channels(&self) -> Result<i32> { ... }
pub fn size(&self) -> Result<&i32> { ... }
pub fn size_1(&self, i: i32) -> Result<i32> { ... }
pub fn dims(&self) -> Result<i32> { ... }
pub fn nzcount(&self) -> Result<size_t> { ... }
pub fn hash(&self, i0: i32) -> Result<size_t> { ... }
pub fn hash_1(&self, i0: i32, i1: i32) -> Result<size_t> { ... }
pub fn hash_2(&self, i0: i32, i1: i32, i2: i32) -> Result<size_t> { ... }
pub fn hash_3(&self, idx: &i32) -> Result<size_t> { ... }
pub fn ptr(
        &mut self,
        i0: i32,
        create_missing: bool,
        hashval: &mut size_t
    ) -> Result<&mut u8> { ... }
pub fn ptr_1(
        &mut self,
        i0: i32,
        i1: i32,
        create_missing: bool,
        hashval: &mut size_t
    ) -> Result<&mut u8> { ... }
pub fn ptr_2(
        &mut self,
        i0: i32,
        i1: i32,
        i2: i32,
        create_missing: bool,
        hashval: &mut size_t
    ) -> Result<&mut u8> { ... }
pub fn ptr_3(
        &mut self,
        idx: &i32,
        create_missing: bool,
        hashval: &mut size_t
    ) -> Result<&mut u8> { ... }
pub fn erase(
        &mut self,
        i0: i32,
        i1: i32,
        hashval: &mut size_t
    ) -> Result<()> { ... }
pub fn erase_1(
        &mut self,
        i0: i32,
        i1: i32,
        i2: i32,
        hashval: &mut size_t
    ) -> Result<()> { ... }
pub fn erase_2(&mut self, idx: &i32, hashval: &mut size_t) -> Result<()> { ... }
pub fn begin_mut(&mut self) -> Result<SparseMatIterator> { ... }
pub fn begin(&self) -> Result<SparseMatConstIterator> { ... }
pub fn end_mut(&mut self) -> Result<SparseMatIterator> { ... }
pub fn end(&self) -> Result<SparseMatConstIterator> { ... }
pub fn node(&mut self, nidx: size_t) -> Result<SparseMat_Node> { ... }
pub fn node_1(&self, nidx: size_t) -> Result<SparseMat_Node> { ... }
pub fn new_node(&mut self, idx: &i32, hashval: size_t) -> Result<&mut u8> { ... }
pub fn remove_node(
        &mut self,
        hidx: size_t,
        nidx: size_t,
        previdx: size_t
    ) -> Result<()> { ... }
pub fn resize_hash_tab(&mut self, newsize: size_t) -> Result<()> { ... } }

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

Loading content...

Provided methods

pub fn flags(&self) -> i32[src]

pub fn set_flags(&mut self, val: i32)[src]

pub fn hdr(&mut self) -> SparseMat_Hdr[src]

pub fn set_hdr(&mut self, val: &mut SparseMat_Hdr)[src]

pub fn try_clone(&self) -> Result<SparseMat>[src]

creates full copy of the matrix

pub fn copy_to(&self, m: &mut SparseMat) -> Result<()>[src]

copies all the data to the destination matrix. All the previous content of m is erased

pub fn copy_to_mat(&self, m: &mut Mat) -> Result<()>[src]

converts sparse matrix to dense matrix.

pub fn convert_to(
    &self,
    m: &mut SparseMat,
    rtype: i32,
    alpha: f64
) -> Result<()>
[src]

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

pub fn convert_to_1(
    &self,
    m: &mut Mat,
    rtype: i32,
    alpha: f64,
    beta: f64
) -> Result<()>
[src]

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

pub fn assign_to(&self, m: &mut SparseMat, typ: i32) -> Result<()>[src]

pub fn create(&mut self, dims: i32, _sizes: &i32, _type: i32) -> Result<()>[src]

reallocates sparse matrix.

If the matrix already had the proper size and type, it is simply cleared with clear(), otherwise, the old matrix is released (using release()) and the new one is allocated.

pub fn clear(&mut self) -> Result<()>[src]

sets all the sparse matrix elements to 0, which means clearing the hash table.

pub fn addref(&mut self) -> Result<()>[src]

manually increments the reference counter to the header.

pub fn release(&mut self) -> Result<()>[src]

pub fn elem_size(&self) -> Result<size_t>[src]

returns the size of each element in bytes (not including the overhead - the space occupied by SparseMat::Node elements)

pub fn elem_size1(&self) -> Result<size_t>[src]

returns elemSize()/channels()

pub fn typ(&self) -> Result<i32>[src]

returns type of sparse matrix elements

pub fn depth(&self) -> Result<i32>[src]

returns the depth of sparse matrix elements

pub fn channels(&self) -> Result<i32>[src]

returns the number of channels

pub fn size(&self) -> Result<&i32>[src]

returns the array of sizes, or NULL if the matrix is not allocated

pub fn size_1(&self, i: i32) -> Result<i32>[src]

returns the size of i-th matrix dimension (or 0)

pub fn dims(&self) -> Result<i32>[src]

returns the matrix dimensionality

pub fn nzcount(&self) -> Result<size_t>[src]

returns the number of non-zero elements (=the number of hash table nodes)

pub fn hash(&self, i0: i32) -> Result<size_t>[src]

computes the element hash value (1D case)

pub fn hash_1(&self, i0: i32, i1: i32) -> Result<size_t>[src]

computes the element hash value (2D case)

pub fn hash_2(&self, i0: i32, i1: i32, i2: i32) -> Result<size_t>[src]

computes the element hash value (3D case)

pub fn hash_3(&self, idx: &i32) -> Result<size_t>[src]

computes the element hash value (nD case)

pub fn ptr(
    &mut self,
    i0: i32,
    create_missing: bool,
    hashval: &mut size_t
) -> Result<&mut u8>
[src]

specialized variants for 1D, 2D, 3D cases and the generic_type one for n-D case. return pointer to the matrix element. - if the element is there (it's non-zero), the pointer to it is returned - if it's not there and createMissing=false, NULL pointer is returned - if it's not there and createMissing=true, then the new element is created and initialized with 0. Pointer to it is returned - if the optional hashval pointer is not NULL, the element hash value is not computed, but *hashval is taken instead.

returns pointer to the specified element (1D case)

C++ default parameters

  • hashval: 0

pub fn ptr_1(
    &mut self,
    i0: i32,
    i1: i32,
    create_missing: bool,
    hashval: &mut size_t
) -> Result<&mut u8>
[src]

returns pointer to the specified element (2D case)

C++ default parameters

  • hashval: 0

pub fn ptr_2(
    &mut self,
    i0: i32,
    i1: i32,
    i2: i32,
    create_missing: bool,
    hashval: &mut size_t
) -> Result<&mut u8>
[src]

returns pointer to the specified element (3D case)

C++ default parameters

  • hashval: 0

pub fn ptr_3(
    &mut self,
    idx: &i32,
    create_missing: bool,
    hashval: &mut size_t
) -> Result<&mut u8>
[src]

returns pointer to the specified element (nD case)

C++ default parameters

  • hashval: 0

pub fn erase(&mut self, i0: i32, i1: i32, hashval: &mut size_t) -> Result<()>[src]

erases the specified element (2D case)

C++ default parameters

  • hashval: 0

pub fn erase_1(
    &mut self,
    i0: i32,
    i1: i32,
    i2: i32,
    hashval: &mut size_t
) -> Result<()>
[src]

erases the specified element (3D case)

C++ default parameters

  • hashval: 0

pub fn erase_2(&mut self, idx: &i32, hashval: &mut size_t) -> Result<()>[src]

erases the specified element (nD case)

C++ default parameters

  • hashval: 0

pub fn begin_mut(&mut self) -> Result<SparseMatIterator>[src]

return the sparse matrix iterator pointing to the first sparse matrix element

returns the sparse matrix iterator at the matrix beginning

pub fn begin(&self) -> Result<SparseMatConstIterator>[src]

returns the read-only sparse matrix iterator at the matrix beginning

pub fn end_mut(&mut self) -> Result<SparseMatIterator>[src]

return the sparse matrix iterator pointing to the element following the last sparse matrix element

returns the sparse matrix iterator at the matrix end

pub fn end(&self) -> Result<SparseMatConstIterator>[src]

returns the read-only sparse matrix iterator at the matrix end

pub fn node(&mut self, nidx: size_t) -> Result<SparseMat_Node>[src]

/////////// some internal-use methods ///////////////

pub fn node_1(&self, nidx: size_t) -> Result<SparseMat_Node>[src]

pub fn new_node(&mut self, idx: &i32, hashval: size_t) -> Result<&mut u8>[src]

pub fn remove_node(
    &mut self,
    hidx: size_t,
    nidx: size_t,
    previdx: size_t
) -> Result<()>
[src]

pub fn resize_hash_tab(&mut self, newsize: size_t) -> Result<()>[src]

Loading content...

Implementors

impl SparseMatTrait for SparseMat[src]

Loading content...