Introduction
Here, we present various ways to computes the mean of a 2d container denoted S2d. It illustrates the different uses to iterate through bidimensional containers 2d Containers like slip::block2d, slip::Array2d, slip::Matrix, slip::GrayscaleImage and multicomponent bidimensional containers Multi-component 2d Containers like: slip::ColorImage, slip::MultispectralImage... Here are the axis conventions used in the library:
The classical loop
const std::size_t rows = S2d.rows();
const std::size_t cols = S2d.cols();
for(std::size_t i = 0; i < rows; ++i)
{
for(std::size_t j = 0; j < cols; ++j)
{
mean += S2d[i][j];
}
}
mean = mean / double(S2d.size());
1d iterators
These iterators follow the RandomAccessIterator concept of the standard library.
Direct global 1d iterator
double m = slip::mean<double>(S2d.begin(),S2d.end());
Reverse global 1d iterator
double m = slip::mean<double>(S2d.rbegin(),S2d.rend());
Row iterators
Direct row iterator
double m = slip::mean<double>(S2d.row_begin(i),S2d.row_end(i));
Reverse row iterator
double m = slip::mean<double>(S2d.row_rbegin(i),S2d.row_rend(i));
Direct row range iterator
double m = slip::mean<double>(S2d.row_begin(i,range_rows),S2d.row_end(i,range_rows));
Reverse row range iterator
double m = slip::mean<double>(S2d.row_rbegin(i,range_rows),S2d.row_rend(i,range_rows));
Column iterators
Direct col iterator
double m = slip::mean<double>(S2d.col_begin(i),S2d.col_end(i));
Reverse column iterator
double m = slip::mean<double>(S2d.col_rbegin(i),S2d.col_rend(i));
Direct column range iterator
double m = slip::mean<double>(S2d.col_begin(i,range_cols),S2d.col_end(i,range_cols));
Reverse column range iterator
double m = slip::mean<double>(S2d.col_rbegin(i,range_cols),S2d.col_rend(i,range_cols));
2d iterators
Box iterators
These iterators follow the BidirectionalIterator concept of the standard library. Specific bidimensional iterator that store the i (x1) and j (x2) positions of the iterator within the container. These coordinates are accessible by the method i() (x1())and j() (x2()). These iterators are less fast than RandomAccessIterator because they verify that the iteration stay within the defined box. See the internal classes slip::iterator2d_box and slip::const_iterator2d_box for more details.
double m = slip::mean<double>(S2d.upper_left(),S2d.bottom_right());
double m = slip::mean<double>(S2d.upper_left(box),S2d.bottom_right(box));
double m = slip::mean<double>(S2d.rupper_left(box),S2d.rbottom_right(box));
Range iterators
These iterators follow the BidirectionalIterator concept of the standard library. Specific bidimensional iterator that store the i (x1) and j (x2) positions of the iterator within the container. These coordinates are accessible by the method i() (x1())and j() (x2()). These iterators are less fast than RandomAccessIterator because they verify that the iteration stay within the defined ranges. See the internal classes slip::iterator2d_range and slip::const_iterator2d_range for more details.
double m = slip::mean<double>(S2d.upper_left(range_rows,range_cols),
S2d.bottom_right(range_rows,range_cols));
double m = slip::mean<double>(S2d.rupper_left(range_rows,range_cols),
S2d.rbottom_right(range_rows,range_cols));
Multi-component iterators
To iterate through a specific component of a multi-component container. It suffies to add first the number of the component to all the previous iterator methods. See slip::GenericMultiComponent2d interface for a more detailed description. As an example, we give here the iteration through a row of a specific component:
const std::size_t component = 1;
const std::size_t row = 1;
double m = slip::mean<double>(S2d.row_begin(component,row),S2d.row_end(component,row));
const std::size_t component = 1;
const std::size_t row = 1;
double m = slip::mean<double>(S2d.row_rbegin(component,row),S2d.row_rend(component,row));