c++ - OpenCV sparse matrix: how to get the indices of non-zero elements in a row/column -


is there way indices of non-zeros of specific row of 2d sparsemat, without looping on row?

the following code, using loop, print results y-th row.

for(int x = 0; x < sparse_mat.size(1); x++) {      if(sparse_mat.ref<int>(y,x) != 0)             std::cout<<x<<std::endl;   } 

thank in advance suggestions!

i don't have opencv, reading documentation found example:

// prints elements of sparse floating-point matrix // , sum of elements. sparsematconstiterator_<float>     = sparse_mat.begin<float>(),     it_end = sparse_mat.end<float>(); double s = 0; int dims = sparse_mat.dims(); for(; != it_end; ++it) {     // print element indices , element value     const sparsemat::node* n = it.node();     printf("(");     for(int = 0; < dims; i++)         printf("%d%s", n->idx[i], < dims-1 ? ", " : ")");     printf(": %g\n", it.value<float>());     s += *it; } printf("element sum %g\n", s); 

along following:

if run loop, notice elements not enumerated in logical order (lexicographical, , on). come in same order stored in hash table (semi-randomly). may collect pointers nodes , sort them proper ordering. note, however, pointers nodes may become invalid when add more elements matrix. may happen due possible buffer reallocation.

this implies (kinda explicitly) data stored in hash table , not vector of indices , values, or eigen. should enable sift through nodes instead of specific row; like

for(; != it_end; ++it) {     const sparsemat::node* n = it.node();     if(n->idx[row_index])         std::cout << n->idx[col_index] << std::endl; } 

replace row_index , col_index accordingly, don't know relevant order.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -