From 0a4e1501279cd3bc03603a9b48c2fa2f1ba84b2a Mon Sep 17 00:00:00 2001 From: Shawn <97796289+ShawnNotFound@users.noreply.github.com> Date: Tue, 17 Nov 2026 09:14:36 +0000 Subject: [PATCH] Merge pull request #30597 from ShawnNotFound:fix-20979-python-empty-umat stitching: wrap default constructors of ImageFeatures, MatchesInfo and CameraParams - #30597 Fixes #20979 `cv2.detail_ImageFeatures()`, `cv2.detail_MatchesInfo()` and `cv2.detail_CameraParams()` have no wrapped constructor, so the Python bindings create the object without running the C++ constructor and the fields are left zero-filled: - reading `ImageFeatures().descriptors` copies a zeroed `UMat` and segfaults - `MatchesInfo()` returns `src_img_idx`/`dst_img_idx` 0 instead of -1 - `CameraParams()` returns `focal` 0.0 and `R` None instead of 1 and the identity This marks the existing default constructors of `MatchesInfo` and `CameraParams` as `CV_WRAP` and adds one for `ImageFeatures` (`img_idx(0)`), so the generator emits a constructor for all three. Stitching is only wrapped for Python, so other bindings are not affected. ### Testing Added `stitching_detail_default_construct_test` to `modules/stitching/misc/python/test/test_stitching.py`. Without the fix the `ImageFeatures` test segfaults and the other two fail on the default values. With the fix, `test_stitching.py` (10 tests) and `opencv_test_stitching` (22 tests) pass locally (Linux x86-64, GCC 13, Python 3.13). ### 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. - [ ] The feature is well documented and sample code can be built with the project CMake --- .../opencv2/stitching/detail/camera.hpp | 2 +- .../opencv2/stitching/detail/matchers.hpp | 4 +- .../misc/python/test/test_stitching.py | 42 +++++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/modules/stitching/include/opencv2/stitching/detail/camera.hpp b/modules/stitching/include/opencv2/stitching/detail/camera.hpp index 14ecf60f30..79d20f3ae8 100644 --- a/modules/stitching/include/opencv2/stitching/detail/camera.hpp +++ b/modules/stitching/include/opencv2/stitching/detail/camera.hpp @@ -57,7 +57,7 @@ namespace detail { */ struct CV_EXPORTS_W_SIMPLE CameraParams { - CameraParams(); + CV_WRAP CameraParams(); CameraParams(const CameraParams& other); CameraParams& operator =(const CameraParams& other); CV_WRAP Mat K() const; diff --git a/modules/stitching/include/opencv2/stitching/detail/matchers.hpp b/modules/stitching/include/opencv2/stitching/detail/matchers.hpp index e25668308e..9f9e21ad59 100644 --- a/modules/stitching/include/opencv2/stitching/detail/matchers.hpp +++ b/modules/stitching/include/opencv2/stitching/detail/matchers.hpp @@ -57,6 +57,8 @@ namespace detail { /** @brief Structure containing image keypoints and descriptors. */ struct CV_EXPORTS_W_SIMPLE ImageFeatures { + CV_WRAP ImageFeatures() : img_idx(0) {} + CV_PROP_RW int img_idx; CV_PROP_RW Size img_size; CV_PROP_RW std::vector keypoints; @@ -98,7 +100,7 @@ homography or affine transformation based on selected matcher. */ struct CV_EXPORTS_W_SIMPLE MatchesInfo { - MatchesInfo(); + CV_WRAP MatchesInfo(); MatchesInfo(const MatchesInfo &other); MatchesInfo& operator =(const MatchesInfo &other); diff --git a/modules/stitching/misc/python/test/test_stitching.py b/modules/stitching/misc/python/test/test_stitching.py index 5f143d0013..123503c6e6 100644 --- a/modules/stitching/misc/python/test/test_stitching.py +++ b/modules/stitching/misc/python/test/test_stitching.py @@ -177,5 +177,47 @@ class stitching_seam_finder_graph_cuts(NewOpenCVTests): self.assertIsNotNone(masks_warped) +class stitching_detail_default_construct_test(NewOpenCVTests): + + def test_image_features(self): + features = cv.detail_ImageFeatures() + self.assertEqual(features.img_idx, 0) + self.assertEqual(features.keypoints, ()) + self.assertIsInstance(features.descriptors, cv.UMat) + + def test_camera_params(self): + camera = cv.detail_CameraParams() + self.assertEqual(camera.focal, 1) + self.assertEqual(camera.aspect, 1) + np.testing.assert_array_equal(camera.R, np.eye(3)) + + def test_matches_info(self): + matches_info = cv.detail_MatchesInfo() + self.assertEqual(matches_info.src_img_idx, -1) + self.assertEqual(matches_info.dst_img_idx, -1) + + def test_matcher_apply(self): + orb = cv.ORB_create() + features = [] + for idx, name in enumerate(('stitching/a1.png', 'stitching/a2.png')): + img = self.get_sample(name) + keypoints, descriptors = orb.detectAndCompute(img, None) + feat = cv.detail_ImageFeatures() + feat.img_idx = idx + feat.img_size = (img.shape[1], img.shape[0]) + feat.keypoints = keypoints + feat.descriptors = cv.UMat(descriptors) + features.append(feat) + + matcher = cv.detail_BestOf2NearestMatcher() + matches_info = matcher.apply(features[0], features[1]) + + self.assertGreater(len(matches_info.matches), 0) + self.assertGreater(matches_info.num_inliers, 0) + self.assertLessEqual(matches_info.num_inliers, len(matches_info.matches)) + self.assertEqual(matches_info.H.shape, (3, 3)) + self.assertGreater(matches_info.confidence, 0) + + if __name__ == '__main__': NewOpenCVTests.bootstrap()