Skip to content

Commit 493c37b

Browse files
Added examples from Chapter 4
1 parent 10a5b36 commit 493c37b

File tree

5 files changed

+177
-0
lines changed

5 files changed

+177
-0
lines changed

CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ add_executable( example_02-08 example_02-08.cpp )
1818
add_executable( example_02-09 example_02-09.cpp )
1919
add_executable( example_02-10 example_02-10.cpp )
2020
add_executable( example_02-11 example_02-11.cpp )
21+
add_executable( example_04-01 example_04-01.cpp )
22+
add_executable( example_04-02 example_04-02.cpp )
23+
add_executable( example_04-03 example_04-03.cpp )
24+
add_executable( example_04-04 example_04-04.cpp )
2125
add_executable( example_16-01 example_16-01.cpp )
2226

2327
target_link_libraries( example_02-01 ${OpenCV_LIBS} )
@@ -31,6 +35,10 @@ target_link_libraries( example_02-08 ${OpenCV_LIBS} )
3135
target_link_libraries( example_02-09 ${OpenCV_LIBS} )
3236
target_link_libraries( example_02-10 ${OpenCV_LIBS} )
3337
target_link_libraries( example_02-11 ${OpenCV_LIBS} )
38+
target_link_libraries( example_04-01 ${OpenCV_LIBS} )
39+
target_link_libraries( example_04-02 ${OpenCV_LIBS} )
40+
target_link_libraries( example_04-03 ${OpenCV_LIBS} )
41+
target_link_libraries( example_04-04 ${OpenCV_LIBS} )
3442
target_link_libraries( example_16-01 ${OpenCV_LIBS} )
3543

3644

example_04-01.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <opencv2/opencv.hpp>
2+
#include <iostream>
3+
4+
using namespace std;
5+
6+
// Summation of a multidimensional array, done plane by plane
7+
//
8+
int main( int argc, char** argv ) {
9+
10+
const int n_mat_size = 5;
11+
const int n_mat_sz[] = { n_mat_size, n_mat_size, n_mat_size };
12+
cv::Mat n_mat( 3, n_mat_sz, CV_32FC1 );
13+
14+
cv::RNG rng;
15+
rng.fill( n_mat, cv::RNG::UNIFORM, 0.f, 1.f );
16+
17+
const cv::Mat* arrays[] = { &n_mat, 0 };
18+
cv::Mat my_planes[1];
19+
cv::NAryMatIterator it( arrays, my_planes );
20+
21+
// On each iteration, it.planes[i] will be the current plane of the
22+
// i-th array from 'arrays'.
23+
//
24+
float s = 0.f; // Total sum over all planes
25+
int n = 0; // Total number of planes
26+
for (int p = 0; p < it.nplanes; p++, ++it) {
27+
s += cv::sum(it.planes[0])[0];
28+
n++;
29+
}
30+
31+
cout <<"Total across entire volume: " <<s <<endl;
32+
33+
}

example_04-02.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <opencv2/opencv.hpp>
2+
#include <iostream>
3+
4+
using namespace std;
5+
6+
// Summation of a multidimensional array, done plane by plane
7+
//
8+
int main( int argc, char** argv ) {
9+
10+
const int n_mat_size = 5;
11+
const int n_mat_sz[] = { n_mat_size, n_mat_size, n_mat_size };
12+
13+
cv::Mat n_mat0( 3, n_mat_sz, CV_32FC1 );
14+
cv::Mat n_mat1( 3, n_mat_sz, CV_32FC1 );
15+
16+
cv::RNG rng;
17+
rng.fill( n_mat0, cv::RNG::UNIFORM, 0.f, 1.f );
18+
rng.fill( n_mat1, cv::RNG::UNIFORM, 0.f, 1.f );
19+
20+
const cv::Mat* arrays[] = { &n_mat0, &n_mat1, 0 };
21+
22+
cv::Mat my_planes[2];
23+
cv::NAryMatIterator it( arrays, my_planes );
24+
25+
float s = 0.f; // Total sum over all planes in both arrays
26+
int n = 0; // Total number of planes
27+
for( int p = 0; p < it.nplanes; p++, ++it ) {
28+
s += cv::sum(it.planes[0])[0];
29+
s += cv::sum(it.planes[1])[0];
30+
n++;
31+
}
32+
33+
cout <<"Total across both volumes: " <<s <<endl;
34+
}

example_04-03.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <opencv2/opencv.hpp>
2+
#include <iostream>
3+
4+
// Printing all of the nonzero elements of a sparse array
5+
//
6+
using namespace std;
7+
8+
// Summation of a multidimensional array, done plane by plane
9+
//
10+
int main( int argc, char** argv ) {
11+
12+
// Create a 10x10 sparse matrix with a few nonzero elements
13+
//
14+
int size[] = {10,10};
15+
cv::SparseMat sm( 2, size, CV_32F );
16+
for( int i=0; i<10; i++ ) { // Fill the array
17+
int idx[2];
18+
idx[0] = size[0] * rand();
19+
idx[1] = size[1] * rand();
20+
sm.ref<float>( idx ) += 1.0f;
21+
}
22+
23+
// Print out the nonzero elements
24+
//
25+
cv::SparseMatConstIterator_<float> it = sm.begin<float>();
26+
cv::SparseMatConstIterator_<float> it_end = sm.end<float>();
27+
28+
for(; it != it_end; ++it) {
29+
const cv::SparseMat::Node* node = it.node();
30+
printf(" (%3d,%3d) %f\n", node->idx[0], node->idx[1], *it );
31+
}
32+
33+
}

example_04-04.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include <opencv2/opencv.hpp>
2+
#include <iostream>
3+
4+
using namespace std;
5+
6+
// A better way to print a sparse matrix
7+
//
8+
template <class T> void print_matrix( const cv::SparseMat_<T>* sm ) {
9+
cv::SparseMatConstIterator_<T> it = sm->begin();
10+
cv::SparseMatConstIterator_<T> it_end = sm->end();
11+
12+
for(; it != it_end; ++it) {
13+
const typename cv::SparseMat_<T>::Node* node = it.node();
14+
cout <<"( " <<node->idx[0] <<", " <<node->idx[1]
15+
<<" ) = " <<*it <<endl;
16+
}
17+
}
18+
19+
void calling_function1( void ) {
20+
21+
int ndim = 2;
22+
int size[] = {4,4};
23+
24+
cv::SparseMat_<float> sm( ndim, size );
25+
26+
// Create a sparse matrix with a few nonzero elements
27+
//
28+
for( int i=0; i<4; i++ ) { // Fill the array
29+
int idx[2];
30+
idx[0] = size[0] * rand();
31+
idx[1] = size[1] * rand();
32+
sm.ref( idx ) += 1.0f;
33+
}
34+
35+
print_matrix<float>( &sm );
36+
}
37+
38+
void calling_function2( void ) {
39+
40+
int ndim = 2;
41+
int size[] = {4,4};
42+
43+
cv::SparseMat sm( ndim, size, CV_32F );
44+
45+
// Create a sparse matrix with a few nonzero elements
46+
//
47+
for( int i=0; i<4; i++ ) { // Fill the array
48+
int idx[2];
49+
idx[0] = size[0] * rand();
50+
idx[1] = size[1] * rand();
51+
sm.ref<float>( idx ) += 1.0f;
52+
}
53+
54+
print_matrix<float>( (cv::SparseMat_<float>*) &sm );
55+
56+
}
57+
58+
int main( int argc, char** argv ) {
59+
60+
cout <<"Case 1:" <<endl;
61+
calling_function1();
62+
cout <<endl;
63+
64+
cout <<"Case 2:" <<endl;
65+
calling_function2();
66+
cout <<endl;
67+
68+
}
69+

0 commit comments

Comments
 (0)