Skip to content

Add missing OpenCV 5 geometry APIs; fix stale readNet doc - #2000

Merged
shimat merged 4 commits into
mainfrom
feature/opencv5-geometry-module-gaps
Jul 4, 2026
Merged

Add missing OpenCV 5 geometry APIs; fix stale readNet doc#2000
shimat merged 4 commits into
mainfrom
feature/opencv5-geometry-module-gaps

Conversation

@shimat

@shimat shimat commented Jul 4, 2026

Copy link
Copy Markdown
Owner

Summary

  • Wraps OpenCV 5 geometry-module functions that were never exposed in OpenCvSharp: ApproxPolyN, MinEnclosingConvexPolygon, GetClosestEllipsePoints (from geometry/2d.hpp), and BuildMST, VoxelGridSampling, RandomSampling, FarthestPointSampling, NormalEstimate (from geometry/segment.hpp and geometry/mst.hpp). Adds the supporting MSTEdge struct and MSTAlgorithm enum.
  • Relocates ~64 functions (convexHull, minAreaRect, fitEllipse family, moments, getAffineTransform, etc.) that OpenCV 5 physically moved from imgproc into the new geometry module, from imgproc.h/Cv2_imgproc.cs into geometry.h/Cv2_geometry.cs, renaming the native CVAPI prefix imgproc_ -> geometry_ to match upstream's module layout. The public Cv2 API surface (method names/signatures) is unchanged.
  • Fixes Net.ReadNet()'s XML doc, which still described the Caffe/Darknet/Torch model formats; those parsers (and readNetFromCaffe/readNetFromDarknet/readNetFromTorch) were removed in OpenCV 5, and readNet() 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.hpp also declares two larger classes that are still unwrapped: cv::SACSegmentation (RANSAC-based point-cloud plane/sphere segmentation - create() factory plus getters/setters for SacModelType, SacMethod, DistanceThreshold, RadiusLimits, MaxIterations, Confidence, NumberOfModelsExpected, Parallel, RandomGeneratorState, CustomModelConstraints) and cv::RegionGrowing3D (declared further down the same header). These need the CvPtrObject-based algorithm-class pattern (see VolumeSettings/Volume in Modules/ptcloud for 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

  • Native build: cmake --build src/build --config Release --target OpenCvSharpExtern succeeds with no errors.
  • Managed build: dotnet build src/OpenCvSharp/OpenCvSharp.csproj -c Release -f net8.0 succeeds, 0 warnings/errors.
  • Added unit tests for all 8 newly-wrapped functions in test/OpenCvSharp.Tests/calib3d/GeometryFunctionsTest.cs.
  • Full test suite: 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

  • New Features
    • Added new geometry/shape APIs (polygon approximation, convex queries, ellipse/line fitting, bounding primitives, point-in-polygon testing, rotated-rectangle intersections, and affine/perspective transforms).
    • Added point sampling and normal/curvature estimation utilities.
    • Added Minimum Spanning Tree support via BuildMST, including MSTAlgorithm and MSTEdge.
  • Breaking Changes
    • Moved geometry helpers from the image-processing API surface to the geometry API surface.
  • Documentation
    • Updated DNN model-loading documentation to reflect TensorFlow/ONNX/OpenVINO formats.
  • Tests
    • Added coverage for the new geometry and sampling APIs.

- 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>
@coderabbitai

coderabbitai Bot commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: de8baa12-22a1-41b4-9e19-c528bf68a7a5

📥 Commits

Reviewing files that changed from the base of the PR and between 57e390b and c450c7e.

📒 Files selected for processing (1)
  • src/OpenCvSharpExtern/geometry.h

📝 Walkthrough

Walkthrough

This 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 ReadNet documentation.

Changes

Geometry module migration

Layer / File(s) Summary
MST types and native include setup
src/OpenCvSharp/Modules/geometry/Enum/MSTAlgorithm.cs, src/OpenCvSharp/Modules/geometry/MSTEdge.cs, src/OpenCvSharpExtern/my_types.h, src/OpenCvSharpExtern/include_opencv.h
Adds MSTAlgorithm, MSTEdge, interop conversion for MSTEdge, and an explicit opencv2/geometry/mst.hpp include.
Native geometry.h wrapper functions
src/OpenCvSharpExtern/geometry.h
Adds CVAPI wrappers for geometry approximation, MST building, sampling, transforms, contour metrics, convex hull/defects, ellipse/line fitting, and intersection operations.
C# NativeMethods geometry P/Invoke declarations
src/OpenCvSharp/Internal/PInvoke/NativeMethods/geometry/NativeMethods_geometry.cs
Adds LibraryImport declarations matching the new native geometry wrappers.
Cv2 geometry public API wrappers
src/OpenCvSharp/Cv2/Cv2_geometry.cs, src/OpenCvSharp/Modules/imgproc/Moments.cs
Adds public Cv2 geometry wrappers and redirects moments computation to geometry_moments.
Remove legacy imgproc geometry APIs
src/OpenCvSharp/Cv2/Cv2_imgproc.cs, src/OpenCvSharp/Internal/PInvoke/NativeMethods/imgproc/NativeMethods_imgproc.cs, src/OpenCvSharpExtern/imgproc.h
Removes the old imgproc geometry wrappers, P/Invoke declarations, and native implementations.
Tests and documentation updates
test/OpenCvSharp.Tests/calib3d/GeometryFunctionsTest.cs, src/OpenCvSharp/Modules/dnn/Cv2.Dnn.cs, src/OpenCvSharp/Modules/dnn/Net.cs
Adds tests for the new geometry APIs and updates ReadNet documentation text.

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
Loading
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly captures the main changes: new OpenCV 5 geometry APIs and updated ReadNet documentation.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/opencv5-geometry-module-gaps

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between d3bb1f3 and b54bfe7.

📒 Files selected for processing (14)
  • src/OpenCvSharp/Cv2/Cv2_geometry.cs
  • src/OpenCvSharp/Cv2/Cv2_imgproc.cs
  • src/OpenCvSharp/Internal/PInvoke/NativeMethods/geometry/NativeMethods_geometry.cs
  • src/OpenCvSharp/Internal/PInvoke/NativeMethods/imgproc/NativeMethods_imgproc.cs
  • src/OpenCvSharp/Modules/dnn/Cv2.Dnn.cs
  • src/OpenCvSharp/Modules/dnn/Net.cs
  • src/OpenCvSharp/Modules/geometry/Enum/MSTAlgorithm.cs
  • src/OpenCvSharp/Modules/geometry/MSTEdge.cs
  • src/OpenCvSharp/Modules/imgproc/Moments.cs
  • src/OpenCvSharpExtern/geometry.h
  • src/OpenCvSharpExtern/imgproc.h
  • src/OpenCvSharpExtern/include_opencv.h
  • src/OpenCvSharpExtern/my_types.h
  • test/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

Comment thread src/OpenCvSharp/Cv2/Cv2_geometry.cs
Comment thread src/OpenCvSharp/Cv2/Cv2_geometry.cs
shimat and others added 3 commits July 5, 2026 08:14
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>
@shimat
shimat merged commit 00049a3 into main Jul 4, 2026
10 of 11 checks passed
@shimat
shimat deleted the feature/opencv5-geometry-module-gaps branch July 4, 2026 23:48
@shimat shimat self-assigned this Jul 5, 2026
@shimat shimat added the enhancement New feature or improvement to OpenCvSharp label Jul 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or improvement to OpenCvSharp

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant