1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-30 07:43:03 +04:00

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>
This commit is contained in:
HattrickGenerator
2021-05-20 21:21:34 +02:00
committed by GitHub
parent c4df8989e9
commit 115e471515
3 changed files with 159 additions and 0 deletions
+78
View File
@@ -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