Merge pull request #19967 from HattrickGenerator:master

* Adding functions rbegin() and rend() functions to matrix class.
This is important to be more standard compliant with C++ and an ever increasing number of people using standard algorithms for better code readability- and maintainability.

The functions are copy pated from their counterparts (even though they should probably call the counterparts but this gave me some troube).
They return iterators using std::reverse_iterators

Follow up of an open feature request:
https://github.com/opencv/opencv/issues/4641

* Fix rbegin() and rend() and provide tests for them

* Removing unnecessary whitespaces

* Adding rbegin and rend to Mat_ class with the right parameters so we don't need to repeat the template argument.
An instantiating cv::Mat_<int> for example can call it's rbegin() function and doesn't need rbegin<int>() with this convience addition.

Follows what is done for forward iterators

* static cast the vector size (return size_t) to an int (that is required for opencv mat constructor)

Co-authored-by: Stefan <stefan.gerl@tum.de>
pull/20137/head
HattrickGenerator 4 years ago committed by GitHub
parent c4df8989e9
commit 115e471515
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 17
      modules/core/include/opencv2/core/mat.hpp
  2. 64
      modules/core/include/opencv2/core/mat.inl.hpp
  3. 78
      modules/core/test/test_mat.cpp

@ -2011,6 +2011,11 @@ public:
template<typename _Tp> MatIterator_<_Tp> begin();
template<typename _Tp> MatConstIterator_<_Tp> begin() const;
/** @brief Same as begin() but for inverse traversal
*/
template<typename _Tp> std::reverse_iterator<MatIterator_<_Tp>> rbegin();
template<typename _Tp> std::reverse_iterator<MatConstIterator_<_Tp>> rbegin() const;
/** @brief Returns the matrix iterator and sets it to the after-last matrix element.
The methods return the matrix read-only or read-write iterators, set to the point following the last
@ -2019,6 +2024,12 @@ public:
template<typename _Tp> MatIterator_<_Tp> end();
template<typename _Tp> MatConstIterator_<_Tp> end() const;
/** @brief Same as end() but for inverse traversal
*/
template<typename _Tp> std::reverse_iterator< MatIterator_<_Tp>> rend();
template<typename _Tp> std::reverse_iterator< MatConstIterator_<_Tp>> rend() const;
/** @brief Runs the given functor over all matrix elements in parallel.
The operation passed as argument has to be a function pointer, a function object or a lambda(C++11).
@ -2250,6 +2261,12 @@ public:
const_iterator begin() const;
const_iterator end() const;
//reverse iterators
std::reverse_iterator<iterator> rbegin();
std::reverse_iterator<iterator> rend();
std::reverse_iterator<const_iterator> rbegin() const;
std::reverse_iterator<const_iterator> rend() const;
//! template methods for for operation over all matrix elements.
// the operations take care of skipping gaps in the end of rows (if any)
template<typename Functor> void forEach(const Functor& operation);

@ -1015,6 +1015,17 @@ MatConstIterator_<_Tp> Mat::begin() const
return MatConstIterator_<_Tp>((const Mat_<_Tp>*)this);
}
template<typename _Tp> inline
std::reverse_iterator<MatConstIterator_<_Tp>> Mat::rbegin() const
{
if (empty())
return std::reverse_iterator<MatConstIterator_<_Tp>>();
CV_DbgAssert( elemSize() == sizeof(_Tp) );
MatConstIterator_<_Tp> it((const Mat_<_Tp>*)this);
it += total();
return std::reverse_iterator<MatConstIterator_<_Tp>> (it);
}
template<typename _Tp> inline
MatConstIterator_<_Tp> Mat::end() const
{
@ -1026,6 +1037,15 @@ MatConstIterator_<_Tp> Mat::end() const
return it;
}
template<typename _Tp> inline
std::reverse_iterator<MatConstIterator_<_Tp>> Mat::rend() const
{
if (empty())
return std::reverse_iterator<MatConstIterator_<_Tp>>();
CV_DbgAssert( elemSize() == sizeof(_Tp) );
return std::reverse_iterator<MatConstIterator_<_Tp>>((const Mat_<_Tp>*)this);
}
template<typename _Tp> inline
MatIterator_<_Tp> Mat::begin()
{
@ -1035,6 +1055,17 @@ MatIterator_<_Tp> Mat::begin()
return MatIterator_<_Tp>((Mat_<_Tp>*)this);
}
template<typename _Tp> inline
std::reverse_iterator<MatIterator_<_Tp>> Mat::rbegin()
{
if (empty())
return std::reverse_iterator<MatIterator_<_Tp>>();
CV_DbgAssert( elemSize() == sizeof(_Tp) );
MatIterator_<_Tp> it((Mat_<_Tp>*)this);
it += total();
return std::reverse_iterator<MatIterator_<_Tp>>(it);
}
template<typename _Tp> inline
MatIterator_<_Tp> Mat::end()
{
@ -1046,6 +1077,15 @@ MatIterator_<_Tp> Mat::end()
return it;
}
template<typename _Tp> inline
std::reverse_iterator<MatIterator_<_Tp>> Mat::rend()
{
if (empty())
return std::reverse_iterator<MatIterator_<_Tp>>();
CV_DbgAssert( elemSize() == sizeof(_Tp) );
return std::reverse_iterator<MatIterator_<_Tp>>(MatIterator_<_Tp>((Mat_<_Tp>*)this));
}
template<typename _Tp, typename Functor> inline
void Mat::forEach(const Functor& operation) {
this->forEach_impl<_Tp>(operation);
@ -1713,24 +1753,48 @@ MatConstIterator_<_Tp> Mat_<_Tp>::begin() const
return Mat::begin<_Tp>();
}
template<typename _Tp> inline
std::reverse_iterator<MatConstIterator_<_Tp>> Mat_<_Tp>::rbegin() const
{
return Mat::rbegin<_Tp>();
}
template<typename _Tp> inline
MatConstIterator_<_Tp> Mat_<_Tp>::end() const
{
return Mat::end<_Tp>();
}
template<typename _Tp> inline
std::reverse_iterator<MatConstIterator_<_Tp>> Mat_<_Tp>::rend() const
{
return Mat::rend<_Tp>();
}
template<typename _Tp> inline
MatIterator_<_Tp> Mat_<_Tp>::begin()
{
return Mat::begin<_Tp>();
}
template<typename _Tp> inline
std::reverse_iterator<MatIterator_<_Tp>> Mat_<_Tp>::rbegin()
{
return Mat::rbegin<_Tp>();
}
template<typename _Tp> inline
MatIterator_<_Tp> Mat_<_Tp>::end()
{
return Mat::end<_Tp>();
}
template<typename _Tp> inline
std::reverse_iterator<MatIterator_<_Tp>> Mat_<_Tp>::rend()
{
return Mat::rend<_Tp>();
}
template<typename _Tp> template<typename Functor> inline
void Mat_<_Tp>::forEach(const Functor& operation) {
Mat::forEach<_Tp, Functor>(operation);

@ -2371,4 +2371,82 @@ TEST(Mat, ptrVecni_20044)
EXPECT_EQ(int(6), *(ci));
}
TEST(Mat, reverse_iterator_19967)
{
// empty iterator (#16855)
cv::Mat m_empty;
EXPECT_NO_THROW(m_empty.rbegin<uchar>());
EXPECT_NO_THROW(m_empty.rend<uchar>());
EXPECT_TRUE(m_empty.rbegin<uchar>() == m_empty.rend<uchar>());
// 1D test
std::vector<uchar> data{0, 1, 2, 3};
const std::vector<int> sizes_1d{4};
//Base class
cv::Mat m_1d(sizes_1d, CV_8U, data.data());
auto mismatch_it_pair_1d = std::mismatch(data.rbegin(), data.rend(), m_1d.rbegin<uchar>());
EXPECT_EQ(mismatch_it_pair_1d.first, data.rend()); // expect no mismatch
EXPECT_EQ(mismatch_it_pair_1d.second, m_1d.rend<uchar>());
//Templated derived class
cv::Mat_<uchar> m_1d_t(static_cast<int>(sizes_1d.size()), sizes_1d.data(), data.data());
auto mismatch_it_pair_1d_t = std::mismatch(data.rbegin(), data.rend(), m_1d_t.rbegin());
EXPECT_EQ(mismatch_it_pair_1d_t.first, data.rend()); // expect no mismatch
EXPECT_EQ(mismatch_it_pair_1d_t.second, m_1d_t.rend());
// 2D test
const std::vector<int> sizes_2d{2, 2};
//Base class
cv::Mat m_2d(sizes_2d, CV_8U, data.data());
auto mismatch_it_pair_2d = std::mismatch(data.rbegin(), data.rend(), m_2d.rbegin<uchar>());
EXPECT_EQ(mismatch_it_pair_2d.first, data.rend());
EXPECT_EQ(mismatch_it_pair_2d.second, m_2d.rend<uchar>());
//Templated derived class
cv::Mat_<uchar> m_2d_t(static_cast<int>(sizes_2d.size()),sizes_2d.data(), data.data());
auto mismatch_it_pair_2d_t = std::mismatch(data.rbegin(), data.rend(), m_2d_t.rbegin());
EXPECT_EQ(mismatch_it_pair_2d_t.first, data.rend());
EXPECT_EQ(mismatch_it_pair_2d_t.second, m_2d_t.rend());
// 3D test
std::vector<uchar> data_3d{0, 1, 2, 3, 4, 5, 6, 7};
const std::vector<int> sizes_3d{2, 2, 2};
//Base class
cv::Mat m_3d(sizes_3d, CV_8U, data_3d.data());
auto mismatch_it_pair_3d = std::mismatch(data_3d.rbegin(), data_3d.rend(), m_3d.rbegin<uchar>());
EXPECT_EQ(mismatch_it_pair_3d.first, data_3d.rend());
EXPECT_EQ(mismatch_it_pair_3d.second, m_3d.rend<uchar>());
//Templated derived class
cv::Mat_<uchar> m_3d_t(static_cast<int>(sizes_3d.size()),sizes_3d.data(), data_3d.data());
auto mismatch_it_pair_3d_t = std::mismatch(data_3d.rbegin(), data_3d.rend(), m_3d_t.rbegin());
EXPECT_EQ(mismatch_it_pair_3d_t.first, data_3d.rend());
EXPECT_EQ(mismatch_it_pair_3d_t.second, m_3d_t.rend());
// const test base class
const cv::Mat m_1d_const(sizes_1d, CV_8U, data.data());
auto mismatch_it_pair_1d_const = std::mismatch(data.rbegin(), data.rend(), m_1d_const.rbegin<uchar>());
EXPECT_EQ(mismatch_it_pair_1d_const.first, data.rend()); // expect no mismatch
EXPECT_EQ(mismatch_it_pair_1d_const.second, m_1d_const.rend<uchar>());
EXPECT_FALSE((std::is_assignable<decltype(m_1d_const.rend<uchar>()), uchar>::value)) << "Constness of const iterator violated.";
EXPECT_FALSE((std::is_assignable<decltype(m_1d_const.rbegin<uchar>()), uchar>::value)) << "Constness of const iterator violated.";
// const test templated dervied class
const cv::Mat_<uchar> m_1d_const_t(static_cast<int>(sizes_1d.size()), sizes_1d.data(), data.data());
auto mismatch_it_pair_1d_const_t = std::mismatch(data.rbegin(), data.rend(), m_1d_const_t.rbegin());
EXPECT_EQ(mismatch_it_pair_1d_const_t.first, data.rend()); // expect no mismatch
EXPECT_EQ(mismatch_it_pair_1d_const_t.second, m_1d_const_t.rend());
EXPECT_FALSE((std::is_assignable<decltype(m_1d_const_t.rend()), uchar>::value)) << "Constness of const iterator violated.";
EXPECT_FALSE((std::is_assignable<decltype(m_1d_const_t.rbegin()), uchar>::value)) << "Constness of const iterator violated.";
}
}} // namespace

Loading…
Cancel
Save