OpenCV 5.0.0-pre
Open Source Computer Vision
Loading...
Searching...
No Matches
cv::NAryMatIterator Class Reference

n-ary multi-dimensional array iterator. More...

#include <opencv2/core/mat.hpp>

Collaboration diagram for cv::NAryMatIterator:

Public Member Functions

 NAryMatIterator ()
 the default constructor
 
 NAryMatIterator (const Mat **arrays, Mat *planes, int narrays=-1)
 the full constructor taking arbitrary number of n-dim matrices
 
 NAryMatIterator (const Mat **arrays, uchar **ptrs, int narrays=-1)
 the full constructor taking arbitrary number of n-dim matrices
 
void init (const Mat **arrays, Mat *planes, uchar **ptrs, int narrays=-1)
 the separate iterator initialization method
 
NAryMatIteratoroperator++ ()
 proceeds to the next plane of every iterated matrix
 
NAryMatIterator operator++ (int)
 proceeds to the next plane of every iterated matrix (postfix increment operator)
 

Public Attributes

const Mat ** arrays
 the iterated arrays
 
int narrays
 the number of arrays
 
size_t nplanes
 the number of hyper-planes that the iterator steps through
 
Matplanes
 the current planes
 
uchar ** ptrs
 data pointers
 
size_t size
 the size of each segment (in elements)
 

Protected Attributes

size_t idx
 
int iterdepth
 

Detailed Description

n-ary multi-dimensional array iterator.

Use the class to implement unary, binary, and, generally, n-ary element-wise operations on multi-dimensional arrays. Some of the arguments of an n-ary function may be continuous arrays, some may be not. It is possible to use conventional MatIterator 's for each array but incrementing all of the iterators after each small operations may be a big overhead. In this case consider using NAryMatIterator to iterate through several matrices simultaneously as long as they have the same geometry (dimensionality and all the dimension sizes are the same). On each iteration it.planes[0], it.planes[1],... will be the slices of the corresponding matrices.

The example below illustrates how you can compute a normalized and threshold 3D color histogram:

void computeNormalizedColorHist(const Mat& image, Mat& hist, int N, double minProb)
{
const int histSize[] = {N, N, N};
// make sure that the histogram has a proper size and type
hist.create(3, histSize, CV_32F);
// and clear it
hist = Scalar(0);
// the loop below assumes that the image
// is a 8-bit 3-channel. check it.
CV_Assert(image.type() == CV_8UC3);
it_end = image.end<Vec3b>();
for( ; it != it_end; ++it )
{
const Vec3b& pix = *it;
hist.at<float>(pix[0]*N/256, pix[1]*N/256, pix[2]*N/256) += 1.f;
}
minProb *= image.rows*image.cols;
// initialize iterator (the style is different from STL).
// after initialization the iterator will contain
// the number of slices or planes the iterator will go through.
// it simultaneously increments iterators for several matrices
// supplied as a null terminated list of pointers
const Mat* arrays[] = {&hist, 0};
Mat planes[1];
double s = 0;
// iterate through the matrix. on each iteration
// itNAry.planes[i] (of type Mat) will be set to the current plane
// of the i-th n-dim matrix passed to the iterator constructor.
for(int p = 0; p < itNAry.nplanes; p++, ++itNAry)
{
threshold(itNAry.planes[0], itNAry.planes[0], minProb, 0, THRESH_TOZERO);
s += sum(itNAry.planes[0])[0];
}
s = 1./s;
itNAry = NAryMatIterator(arrays, planes, 1);
for(int p = 0; p < itNAry.nplanes; p++, ++itNAry)
itNAry.planes[0] *= s;
}
Matrix read-only iterator.
Definition mat.hpp:3444
n-dimensional dense array class
Definition mat.hpp:951
MatIterator_< _Tp > end()
Returns the matrix iterator and sets it to the after-last matrix element.
void create(int rows, int cols, int type)
Allocates new array data if needed.
MatIterator_< _Tp > begin()
Returns the matrix iterator and sets it to the first matrix element.
_Tp & at(int i0=0)
Returns a reference to the specified array element.
int cols
Definition mat.hpp:2425
int rows
the number of rows and columns or (-1, -1) when the matrix has more than 2 dimensions
Definition mat.hpp:2425
int type() const
Returns the type of a matrix element.
const Mat ** arrays
the iterated arrays
Definition mat.hpp:3773
Mat * planes
the current planes
Definition mat.hpp:3775
NAryMatIterator()
the default constructor
Template class for short numerical vectors, a partial case of Matx.
Definition matx.hpp:379
Scalar sum(InputArray src)
Calculates the sum of array elements.
Scalar_< double > Scalar
Definition types.hpp:712
#define CV_32F
Definition interface.h:59
#define CV_8UC3
Definition interface.h:79
#define CV_Assert(expr)
Checks a condition at runtime and throws exception if it fails.
Definition exception.hpp:198
double threshold(InputArray src, OutputArray dst, double thresh, double maxval, int type)
Applies a fixed-level threshold to each array element.
@ THRESH_TOZERO
Definition imgproc.hpp:328

Constructor & Destructor Documentation

◆ NAryMatIterator() [1/3]

cv::NAryMatIterator::NAryMatIterator ( )

the default constructor

◆ NAryMatIterator() [2/3]

cv::NAryMatIterator::NAryMatIterator ( const Mat ** arrays,
uchar ** ptrs,
int narrays = -1 )

the full constructor taking arbitrary number of n-dim matrices

◆ NAryMatIterator() [3/3]

cv::NAryMatIterator::NAryMatIterator ( const Mat ** arrays,
Mat * planes,
int narrays = -1 )

the full constructor taking arbitrary number of n-dim matrices

Member Function Documentation

◆ init()

void cv::NAryMatIterator::init ( const Mat ** arrays,
Mat * planes,
uchar ** ptrs,
int narrays = -1 )

the separate iterator initialization method

◆ operator++() [1/2]

NAryMatIterator & cv::NAryMatIterator::operator++ ( )

proceeds to the next plane of every iterated matrix

◆ operator++() [2/2]

NAryMatIterator cv::NAryMatIterator::operator++ ( int )

proceeds to the next plane of every iterated matrix (postfix increment operator)

Member Data Documentation

◆ arrays

const Mat** cv::NAryMatIterator::arrays

the iterated arrays

◆ idx

size_t cv::NAryMatIterator::idx
protected

◆ iterdepth

int cv::NAryMatIterator::iterdepth
protected

◆ narrays

int cv::NAryMatIterator::narrays

the number of arrays

◆ nplanes

size_t cv::NAryMatIterator::nplanes

the number of hyper-planes that the iterator steps through

◆ planes

Mat* cv::NAryMatIterator::planes

the current planes

◆ ptrs

uchar** cv::NAryMatIterator::ptrs

data pointers

◆ size

size_t cv::NAryMatIterator::size

the size of each segment (in elements)


The documentation for this class was generated from the following file: