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

Merge pull request #26022 from Kumataro:fix26016

Imgproc: use double to determine whether the corners points are within src #26022

close #26016
Related https://github.com/opencv/opencv_contrib/pull/3778

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
Kumataro
2024-08-23 18:35:13 +09:00
committed by GitHub
parent 916724e2a0
commit a3bdbf5553
5 changed files with 78 additions and 8 deletions
+28 -4
View File
@@ -475,7 +475,14 @@ public:
template<typename _Tp2> operator Rect_<_Tp2>() const;
//! checks whether the rectangle contains the point
bool contains(const Point_<_Tp>& pt) const;
/*! @warning After OpenCV 4.11.0, when calling Rect.contains() with cv::Point2f / cv::Point2d point, point should not convert/round to int.
* ```
* Rect_<int> r(0,0,500,500); Point_<float> pt(250.0f, 499.9f);
* r.contains(pt) returns false.(OpenCV 4.10.0 or before)
* r.contains(pt) returns true. (OpenCV 4.11.0 or later)
* ```
*/
template<typename _Tp2> inline bool contains(const Point_<_Tp2>& pt) const;
_Tp x; //!< x coordinate of the top-left corner
_Tp y; //!< y coordinate of the top-left corner
@@ -1861,12 +1868,29 @@ Rect_<_Tp>::operator Rect_<_Tp2>() const
return Rect_<_Tp2>(saturate_cast<_Tp2>(x), saturate_cast<_Tp2>(y), saturate_cast<_Tp2>(width), saturate_cast<_Tp2>(height));
}
template<typename _Tp> inline
bool Rect_<_Tp>::contains(const Point_<_Tp>& pt) const
template<typename _Tp> template<typename _Tp2> inline
bool Rect_<_Tp>::contains(const Point_<_Tp2>& pt) const
{
return x <= pt.x && pt.x < x + width && y <= pt.y && pt.y < y + height;
}
// See https://github.com/opencv/opencv/issues/26016
template<> template<> inline
bool Rect_<int>::contains(const Point_<double>& pt) const
{
// std::numeric_limits<int>::digits is 31.
// std::numeric_limits<double>::digits is 53.
// So conversion int->double does not lead to accuracy errors.
const Rect_<double> _rect(static_cast<double>(x), static_cast<double>(y), static_cast<double>(width), static_cast<double>(height));
return _rect.contains(pt);
}
template<> template<> inline
bool Rect_<int>::contains(const Point_<float>& _pt) const
{
// std::numeric_limits<float>::digits is 24.
// std::numeric_limits<double>::digits is 53.
// So conversion float->double does not lead to accuracy errors.
return contains(Point_<double>(static_cast<double>(_pt.x), static_cast<double>(_pt.y)));
}
template<typename _Tp> static inline
Rect_<_Tp>& operator += ( Rect_<_Tp>& a, const Point_<_Tp>& b )
+16 -1
View File
@@ -908,7 +908,22 @@ TYPED_TEST_P(Rect_Test, Overflows) {
EXPECT_EQ(R(), R(20, 0, 10, 10) & R(0, num_lowest, 10, 10));
EXPECT_EQ(R(), R(num_lowest, 0, 10, 10) & R(0, num_lowest, 10, 10));
}
REGISTER_TYPED_TEST_CASE_P(Rect_Test, Overflows);
// See https://github.com/opencv/opencv/issues/26016
// Rect_<int>.contains(Point_<float/double>) needs template specialization.
// This is test for a point on the edge and its nearest points.
template<typename T> T cv_nexttoward(T v, T v2);
template<> int cv_nexttoward<int>(int v, int v2) { CV_UNUSED(v); return v2; }
template<> float cv_nexttoward<float>(float v, float v2) { return std::nextafter(v,v2); }
template<> double cv_nexttoward<double>(double v, double v2) { return std::nexttoward(v,v2); }
TYPED_TEST_P(Rect_Test, OnTheEdge) {
Rect_<int> rect(0,0,500,500);
TypeParam h = static_cast<TypeParam>(rect.height);
ASSERT_TRUE ( rect.contains( Point_<TypeParam>(250, cv_nexttoward(h, h - 1))));
ASSERT_FALSE( rect.contains( Point_<TypeParam>(250, cv_nexttoward(h, h ))));
ASSERT_FALSE( rect.contains( Point_<TypeParam>(250, cv_nexttoward(h, h + 1))));
}
REGISTER_TYPED_TEST_CASE_P(Rect_Test, Overflows, OnTheEdge);
typedef ::testing::Types<int, float, double> RectTypes;
INSTANTIATE_TYPED_TEST_CASE_P(Negative_Test, Rect_Test, RectTypes);