patterncppMinor
Custom iterator implementation returning OpenCV Mat
Viewed 0 times
iteratoropencvcustomreturningimplementationmat
Problem
I have written an adapter class that allows iteration over the rows of a
This is my first attempt at writing custom iterators, so I am interested in feedback on correctness and general points to improve. It seems like my use of
Here are the class definitions:
RowRange.hpp
```
#ifndef CV_ADAPTERS_ROWRANGE_HPP
#define CV_ADAPTERS_ROWRANGE_HPP
#include
#include
namespace cv
{
template
class RowRangeConstIterator : public std::iterator
{
public:
RowRangeConstIterator()
: data()
, row()
, position()
{}
RowRangeConstIterator(const cv::Mat_& m, int index)
: data(m)
, row()
, position(index)
{
CV_DbgAssert(position >= 0 && position & operator*() const
{
setRow();
return row;
}
const cv::Mat_* operator->() const
{
setRow();
return &row;
}
// Logical comparison
bool operator==(const RowRangeConstIterator& that) const
{
return this->position == that.position;
}
bool operator!=(const RowRangeConstIterator& that) const
{
return !(*this == that);
}
bool operatorposition (const RowRangeConstIterator& that) const
{
return this->position > that.position;
}
bool operator that);
}
bool operator>=(const RowRangeConstIterator& that) const
{
return !(*this
Mat object from OpenCV. For those interested, here is the Mat documentation, but the salient points are as follows:Matis a reference-counted header to a shared data buffer.
Mat::row()returns anotherMat, which provides a view of a single row of the largerMat.
Mat_is a templated version ofMatoffering the same interface but better type safety.Mat_andMatcan be converted to one another.
This is my first attempt at writing custom iterators, so I am interested in feedback on correctness and general points to improve. It seems like my use of
mutable is suspect, but I don't know if I can avoid it.Here are the class definitions:
RowRange.hpp
```
#ifndef CV_ADAPTERS_ROWRANGE_HPP
#define CV_ADAPTERS_ROWRANGE_HPP
#include
#include
namespace cv
{
template
class RowRangeConstIterator : public std::iterator
{
public:
RowRangeConstIterator()
: data()
, row()
, position()
{}
RowRangeConstIterator(const cv::Mat_& m, int index)
: data(m)
, row()
, position(index)
{
CV_DbgAssert(position >= 0 && position & operator*() const
{
setRow();
return row;
}
const cv::Mat_* operator->() const
{
setRow();
return &row;
}
// Logical comparison
bool operator==(const RowRangeConstIterator& that) const
{
return this->position == that.position;
}
bool operator!=(const RowRangeConstIterator& that) const
{
return !(*this == that);
}
bool operatorposition (const RowRangeConstIterator& that) const
{
return this->position > that.position;
}
bool operator that);
}
bool operator>=(const RowRangeConstIterator& that) const
{
return !(*this
Solution
Disclaimer: I am not a CV expert.
Overall, LGTM.
-
Relational operators (
-
It is recommended to define
Overall, LGTM.
-
Relational operators (
==, !=, etc) should not be members but friends. If such operators do not treat their operands symmetrically, expect problems with implicit conversion.-
It is recommended to define
operator> in terms of operator
-
I don't see why row should be a member.
-
I understand that position is declared int just because cv::Mat_::rows is int (which is a shame). It'd be nice to have a type of position inferred.
-
Last, but not least. This iterator can serve a much wider spectrum of classes than just cv::Mat. Its only dependency on the underlying class is in rows and row` methods.Context
StackExchange Code Review Q#57351, answer score: 2
Revisions (0)
No revisions yet.