forked from oreillymedia/Learning-OpenCV-3_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_04-04.cpp
78 lines (59 loc) · 1.67 KB
/
example_04-04.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//Example 4-4. A better way to print a matrix
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
// A better way to print a sparse matrix
//
template <class T> void print_matrix( const cv::SparseMat_<T>* sm ) {
cv::SparseMatConstIterator_<T> it = sm->begin();
cv::SparseMatConstIterator_<T> it_end = sm->end();
for(; it != it_end; ++it) {
const typename cv::SparseMat_<T>::Node* node = it.node();
cout <<"( " <<node->idx[0] <<", " <<node->idx[1]
<<" ) = " <<*it <<endl;
}
}
void calling_function1( void ) {
int ndim = 2;
int size[] = {4,4};
cv::SparseMat_<float> sm( ndim, size );
// Create a sparse matrix with a few nonzero elements
//
for( int i=0; i<4; i++ ) { // Fill the array
int idx[2];
idx[0] = size[0] * rand();
idx[1] = size[1] * rand();
sm.ref( idx ) += 1.0f;
}
print_matrix<float>( &sm );
}
void calling_function2( void ) {
int ndim = 2;
int size[] = {4,4};
cv::SparseMat sm( ndim, size, CV_32F );
// Create a sparse matrix with a few nonzero elements
//
for( int i=0; i<4; i++ ) { // Fill the array
int idx[2];
idx[0] = size[0] * rand();
idx[1] = size[1] * rand();
sm.ref<float>( idx ) += 1.0f;
}
print_matrix<float>( (cv::SparseMat_<float>*) &sm );
}
void help(char** argv) {
cout << "\nExample 4-4, a better way to print out a sparse matrix"
<< "\n Demonstrates printing of two different sparse matrices"
<< "\nCall:"
<< argv[0]
<< endl;
}
int main( int argc, char** argv ) {
help(argv);
cout <<"Case 1:" <<endl;
calling_function1();
cout <<endl;
cout <<"Case 2:" <<endl;
calling_function2();
cout <<endl;
}