Add missing OpenCV 5 geometry APIs; fix stale readNet doc - #2000
Conversation
- Add approxPolyN, minEnclosingConvexPolygon, getClosestEllipsePoints (2d.hpp), and buildMST, voxelGridSampling, randomSampling, farthestPointSampling, normalEstimate (segment.hpp/mst.hpp), which OpenCV 5 added to the geometry module but OpenCvSharp never wrapped. - Relocate the ~64 functions (convexHull, minAreaRect, fitEllipse family, moments, getAffineTransform, etc.) that OpenCV 5 physically moved from imgproc into the geometry module, from imgproc.h/ Cv2_imgproc.cs into geometry.h/Cv2_geometry.cs, renaming the CVAPI prefix imgproc_ -> geometry_ to match. Public Cv2 API is unchanged. - Fix Net.ReadNet()'s XML doc, which still described the Caffe/ Darknet/Torch formats that OpenCV 5 removed from readNet's format auto-detection. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughThis PR adds new geometry APIs and MST support across native wrappers, P/Invoke, and C# surfaces, redirects moments to the new geometry binding, removes the old imgproc geometry surface, and updates tests plus ChangesGeometry module migration
Estimated code review effort: 4 (Complex) | ~60 minutes Sequence Diagram(s)sequenceDiagram
participant Cv2_geometry
participant NativeMethods_geometry
participant geometry_h
participant OpenCV
Cv2_geometry->>NativeMethods_geometry: call geometry_* P/Invoke
NativeMethods_geometry->>geometry_h: invoke CVAPI wrapper
geometry_h->>OpenCV: forward geometry operation
OpenCV-->>geometry_h: return result
geometry_h-->>NativeMethods_geometry: fill out parameters
NativeMethods_geometry-->>Cv2_geometry: return managed result
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/OpenCvSharp/Cv2/Cv2_geometry.cs`:
- Around line 2650-2654: The BoxPoints wrapper in Cv2_geometry is missing the
usual lifetime guard for the OutputArray argument. Update BoxPoints to keep the
points.Source alive after the NativeMethods.geometry_boxPoints_OutputArray call,
matching the pattern used by the other InputArray/OutputArray wrappers in this
file. Use the existing BoxPoints method and points.Source reference to locate
the fix.
- Around line 2727-2734: `Cv2_geometry.MinEnclosingTriangle` keeps
`points.Source` alive after the native call but does not keep `triangle.Source`
alive, unlike similar methods in this class. Update
`MinEnclosingTriangle(InputArray points, OutputArray triangle)` to add a
`GC.KeepAlive(triangle.Source)` after `NativeMethods.HandleException(...)`,
alongside the existing `GC.KeepAlive(points.Source)`, so the output wrapper
stays rooted for the duration of the interop call.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 9733f167-45c9-4012-95c3-a9c34321beae
📒 Files selected for processing (14)
src/OpenCvSharp/Cv2/Cv2_geometry.cssrc/OpenCvSharp/Cv2/Cv2_imgproc.cssrc/OpenCvSharp/Internal/PInvoke/NativeMethods/geometry/NativeMethods_geometry.cssrc/OpenCvSharp/Internal/PInvoke/NativeMethods/imgproc/NativeMethods_imgproc.cssrc/OpenCvSharp/Modules/dnn/Cv2.Dnn.cssrc/OpenCvSharp/Modules/dnn/Net.cssrc/OpenCvSharp/Modules/geometry/Enum/MSTAlgorithm.cssrc/OpenCvSharp/Modules/geometry/MSTEdge.cssrc/OpenCvSharp/Modules/imgproc/Moments.cssrc/OpenCvSharpExtern/geometry.hsrc/OpenCvSharpExtern/imgproc.hsrc/OpenCvSharpExtern/include_opencv.hsrc/OpenCvSharpExtern/my_types.htest/OpenCvSharp.Tests/calib3d/GeometryFunctionsTest.cs
💤 Files with no reviewable changes (3)
- src/OpenCvSharpExtern/imgproc.h
- src/OpenCvSharp/Internal/PInvoke/NativeMethods/imgproc/NativeMethods_imgproc.cs
- src/OpenCvSharp/Cv2/Cv2_imgproc.cs
CodeRabbit review comment on PR #2000: this overload was missing the usual GC.KeepAlive guard used by the other InputArray/OutputArray wrappers in this file. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
…Array) CodeRabbit review comment on PR #2000: this overload kept points.Source alive but not triangle.Source, unlike the other InputArray/OutputArray wrappers in this file. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Indent the cvTry([&] { ... }) lambda bodies one level deeper than the
enclosing return statement, for the functions added/relocated by this
PR. The rest of the codebase is inconsistent about this (some blocks
are already indented, most are not); fixing that broader inconsistency
is tracked separately and out of scope here.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Summary
ApproxPolyN,MinEnclosingConvexPolygon,GetClosestEllipsePoints(fromgeometry/2d.hpp), andBuildMST,VoxelGridSampling,RandomSampling,FarthestPointSampling,NormalEstimate(fromgeometry/segment.hppandgeometry/mst.hpp). Adds the supportingMSTEdgestruct andMSTAlgorithmenum.convexHull,minAreaRect,fitEllipsefamily,moments,getAffineTransform, etc.) that OpenCV 5 physically moved fromimgprocinto the newgeometrymodule, fromimgproc.h/Cv2_imgproc.csintogeometry.h/Cv2_geometry.cs, renaming the native CVAPI prefiximgproc_->geometry_to match upstream's module layout. The publicCv2API surface (method names/signatures) is unchanged.Net.ReadNet()'s XML doc, which still described the Caffe/Darknet/Torch model formats; those parsers (andreadNetFromCaffe/readNetFromDarknet/readNetFromTorch) were removed in OpenCV 5, andreadNet()now only auto-detects TensorFlow/ONNX/OpenVINO formats.This came out of an audit against the OpenCV 5 wiki page to find gaps in OpenCvSharp's 5.x migration.
Follow-up (not in this PR)
geometry/segment.hppalso declares two larger classes that are still unwrapped:cv::SACSegmentation(RANSAC-based point-cloud plane/sphere segmentation -create()factory plus getters/setters forSacModelType,SacMethod,DistanceThreshold,RadiusLimits,MaxIterations,Confidence,NumberOfModelsExpected,Parallel,RandomGeneratorState,CustomModelConstraints) andcv::RegionGrowing3D(declared further down the same header). These need theCvPtrObject-based algorithm-class pattern (seeVolumeSettings/VolumeinModules/ptcloudfor a similar recent example), which is a bigger unit of work than the free-function wrapping done here, so it's being picked up in a separate session/PR. A tracked follow-up chip already exists for this.Test plan
cmake --build src/build --config Release --target OpenCvSharpExternsucceeds with no errors.dotnet build src/OpenCvSharp/OpenCvSharp.csproj -c Release -f net8.0succeeds, 0 warnings/errors.test/OpenCvSharp.Tests/calib3d/GeometryFunctionsTest.cs.dotnet test test/OpenCvSharp.Tests/OpenCvSharp.Tests.csproj -c Release- 1252 passed, 0 failed, 27 skipped (pre-existing, environment-dependent).🤖 Generated with Claude Code
Summary by CodeRabbit
BuildMST, includingMSTAlgorithmandMSTEdge.