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
pull/10318/merge
Kumataro 3 months ago committed by GitHub
parent 916724e2a0
commit a3bdbf5553
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 32
      modules/core/include/opencv2/core/types.hpp
  2. 17
      modules/core/test/test_misc.cpp
  3. 4
      modules/features2d/src/keypoint.cpp
  4. 27
      modules/imgproc/test/test_cornersubpix.cpp
  5. 6
      modules/stitching/test/test_matchers.cpp

@ -475,7 +475,14 @@ public:
template<typename _Tp2> operator Rect_<_Tp2>() const; template<typename _Tp2> operator Rect_<_Tp2>() const;
//! checks whether the rectangle contains the point //! 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 x; //!< x coordinate of the top-left corner
_Tp y; //!< y 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)); return Rect_<_Tp2>(saturate_cast<_Tp2>(x), saturate_cast<_Tp2>(y), saturate_cast<_Tp2>(width), saturate_cast<_Tp2>(height));
} }
template<typename _Tp> inline template<typename _Tp> template<typename _Tp2> inline
bool Rect_<_Tp>::contains(const Point_<_Tp>& pt) const bool Rect_<_Tp>::contains(const Point_<_Tp2>& pt) const
{ {
return x <= pt.x && pt.x < x + width && y <= pt.y && pt.y < y + height; 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 template<typename _Tp> static inline
Rect_<_Tp>& operator += ( Rect_<_Tp>& a, const Point_<_Tp>& b ) Rect_<_Tp>& operator += ( Rect_<_Tp>& a, const Point_<_Tp>& b )

@ -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(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)); 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; typedef ::testing::Types<int, float, double> RectTypes;
INSTANTIATE_TYPED_TEST_CASE_P(Negative_Test, Rect_Test, RectTypes); INSTANTIATE_TYPED_TEST_CASE_P(Negative_Test, Rect_Test, RectTypes);

@ -96,7 +96,9 @@ struct RoiPredicate
bool operator()( const KeyPoint& keyPt ) const bool operator()( const KeyPoint& keyPt ) const
{ {
return !r.contains( keyPt.pt ); // workaround for https://github.com/opencv/opencv/issues/26016
// To keep its behaviour, keyPt.pt casts to Point_<int>.
return !r.contains( Point_<int>(keyPt.pt) );
} }
Rect r; Rect r;

@ -65,4 +65,31 @@ TEST(Imgproc_CornerSubPix, out_of_image_corners)
ASSERT_TRUE(Rect(0, 0, image.cols, image.rows).contains(corners.front())); ASSERT_TRUE(Rect(0, 0, image.cols, image.rows).contains(corners.front()));
} }
// See https://github.com/opencv/opencv/issues/26016
TEST(Imgproc_CornerSubPix, corners_on_the_edge)
{
cv::Mat image(500, 500, CV_8UC1);
cv::Size win(1, 1);
cv::Size zeroZone(-1, -1);
cv::TermCriteria criteria;
std::vector<cv::Point2f> cornersOK1 = { cv::Point2f(250, std::nextafter(499.5f, 499.5f - 1.0f)) };
EXPECT_NO_THROW( cv::cornerSubPix(image, cornersOK1, win, zeroZone, criteria) ) << cornersOK1;
std::vector<cv::Point2f> cornersOK2 = { cv::Point2f(250, 499.5f) };
EXPECT_NO_THROW( cv::cornerSubPix(image, cornersOK2, win, zeroZone, criteria) ) << cornersOK2;
std::vector<cv::Point2f> cornersOK3 = { cv::Point2f(250, std::nextafter(499.5f, 499.5f + 1.0f)) };
EXPECT_NO_THROW( cv::cornerSubPix(image, cornersOK3, win, zeroZone, criteria) ) << cornersOK3;
std::vector<cv::Point2f> cornersOK4 = { cv::Point2f(250, std::nextafter(500.0f, 500.0f - 1.0f)) };
EXPECT_NO_THROW( cv::cornerSubPix(image, cornersOK4, win, zeroZone, criteria) ) << cornersOK4;
std::vector<cv::Point2f> cornersNG1 = { cv::Point2f(250, 500.0f) };
EXPECT_ANY_THROW( cv::cornerSubPix(image, cornersNG1, win, zeroZone, criteria) ) << cornersNG1;
std::vector<cv::Point2f> cornersNG2 = { cv::Point2f(250, std::nextafter(500.0f, 500.0f + 1.0f)) };
EXPECT_ANY_THROW( cv::cornerSubPix(image, cornersNG2, win, zeroZone, criteria) ) << cornersNG2;
}
}} // namespace }} // namespace

@ -67,9 +67,11 @@ TEST(SurfFeaturesFinder, CanFindInROIs)
int tl_rect_count = 0, br_rect_count = 0, bad_count = 0; int tl_rect_count = 0, br_rect_count = 0, bad_count = 0;
for (const auto &keypoint : roi_features.keypoints) for (const auto &keypoint : roi_features.keypoints)
{ {
if (rois[0].contains(keypoint.pt)) // Workaround for https://github.com/opencv/opencv/issues/26016
// To keep its behaviour, keypoint.pt casts to Point_<int>.
if (rois[0].contains(Point_<int>(keypoint.pt)))
tl_rect_count++; tl_rect_count++;
else if (rois[1].contains(keypoint.pt)) else if (rois[1].contains(Point_<int>(keypoint.pt)))
br_rect_count++; br_rect_count++;
else else
bad_count++; bad_count++;

Loading…
Cancel
Save