Skip to content

Migrate ArUco API to OpenCV 4.7+ detector-class design#1873

Merged
shimat merged 4 commits into
mainfrom
feature/aruco-new-api
May 24, 2026
Merged

Migrate ArUco API to OpenCV 4.7+ detector-class design#1873
shimat merged 4 commits into
mainfrom
feature/aruco-new-api

Conversation

@shimat
Copy link
Copy Markdown
Owner

@shimat shimat commented May 23, 2026

Summary

  • Remove deprecated free-function wrappers (detectMarkers, estimatePoseSingleMarkers, detectCharucoDiamond, detectCharucoBoard, interpolateCornersCharuco) that no longer exist as non-deprecated API since OpenCV 4.7
  • Add ArucoDetector class wrapping cv::aruco::ArucoDetector
  • Add CharucoBoard class wrapping cv::aruco::CharucoBoard
  • Add CharucoDetector class wrapping cv::aruco::CharucoDetector
  • Add RefineParameters struct wrapping cv::aruco::RefineParameters
  • Update DetectorParameters with two new fields: MinGroupDistance and RelativeCornerRefinmentWinSize
  • Fix null-pointer dereference in aruco_drawDetectedCornersCharuco when ids is null

Breaking changes

This PR intentionally breaks the existing C# API to align with OpenCV's own breaking change introduced in 4.7.

Removed Replacement
CvAruco.DetectMarkers(...) new ArucoDetector(dict).DetectMarkers(...)
CvAruco.EstimatePoseSingleMarkers(...) Removed — use Board.MatchImagePoints + Cv2.SolvePnP
CvAruco.DetectCharucoDiamond(...) new CharucoDetector(board).DetectDiamonds(...)
CvAruco.DetectCharucoBoard(...) new CharucoDetector(board).DetectBoard(...)
CvAruco.InterpolateCornersCharuco(...) new CharucoDetector(board).DetectBoard(...)

Test plan

  • ArucoDetector: create (default / custom params), DetectMarkers on real image, dispose
  • CharucoBoard: create, properties, LegacyPattern, GenerateImage, CheckCharucoCornersCollinear, dispose
  • CharucoDetector: create, DetectBoard on generated image, empty image, dispose
  • RefineParameters: default and custom constructors
  • DetectorParameters: new fields verified
  • Draw helpers: DrawDetectedCornersCharuco with and without ids

Closes #1796

Summary by CodeRabbit

  • New Features

    • ArucoDetector for marker detection with customizable parameters
    • CharucoBoard for creating/managing ChArUco boards and generating board images
    • CharucoDetector for ChArUco board and diamond detection
    • RefineParameters struct to control detection refinement
  • Improvements

    • Extended DetectorParameters with new tuning options for improved detection accuracy
  • Tests

    • Expanded ArUco/ChArUco test coverage and updated detection tests to use the new detector classes

Review Change Stack

shimat and others added 3 commits May 24, 2026 02:33
Replace deprecated free functions (detectMarkers, estimatePoseSingleMarkers,
detectCharucoDiamond, interpolateCornersCharuco, detectCharucoBoard) with the
new ArucoDetector and CharucoDetector class wrappers. Add CharucoBoard class.
Update DetectorParameters struct with minGroupDistance and
relativeCornerRefinmentWinSize fields. Add RefineParameters struct.
Update tests to use the new API.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
…tor, RefineParameters

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@shimat shimat self-assigned this May 23, 2026
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 23, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: fa7a60f9-2bb6-4bf9-9b05-f6db3a06a3cd

📥 Commits

Reviewing files that changed from the base of the PR and between 71a9a14 and c94963d.

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

📝 Walkthrough

Walkthrough

Migrates ArUco bindings from static OpenCV functions to object-oriented wrappers (ArucoDetector, CharucoBoard, CharucoDetector); adds RefineParameters, extends DetectorParameters, updates C API exports and P/Invoke routing, and expands tests to exercise the new APIs.

Changes

ArUco API Modernization

Layer / File(s) Summary
Detector and refine parameter types
src/OpenCvSharp/Modules/aruco/RefineParameters.cs, src/OpenCvSharp/Modules/aruco/DetectorParameters.cs
Introduces RefineParameters struct and adds MinGroupDistance and RelativeCornerRefinmentWinSize to DetectorParameters.
C++ interop boundary and detector APIs
src/OpenCvSharpExtern/aruco.h
Replaces deprecated standalone detect/pose/Charuco helpers with wrapper-style exports for ArucoDetector, CharucoBoard, and CharucoDetector; maps new parameter fields into OpenCV types.
P/Invoke declarations and native method routing
src/OpenCvSharp/Internal/PInvoke/NativeMethods/NativeMethods_aruco.cs
Reorganized extern declarations into #region blocks for ArucoDetector, CharucoBoard, and CharucoDetector; drawing externs reformatted; old static detection externs removed.
ArucoDetector managed wrapper class
src/OpenCvSharp/Modules/aruco/ArucoDetector.cs
New ArucoDetector class that creates and owns a native detector handle, exposes constructors (default/custom) and DetectMarkers with input validation and result marshaling.
CharucoBoard and CharucoDetector managed wrappers
src/OpenCvSharp/Modules/aruco/CharucoBoard.cs, src/OpenCvSharp/Modules/aruco/CharucoDetector.cs
Adds CharucoBoard (creation, properties, image generation, collinearity check) and CharucoDetector (board/diamond detection, optional camera params, detector/refine params) with safe-handle backing.
Tests and API cleanup
src/OpenCvSharp/Modules/aruco/CvAruco.cs, test/OpenCvSharp.Tests/aruco/ArucoTest.cs, test/OpenCvSharp.Tests/system/ExceptionTest.cs
Minor docs/whitespace cleanup; expanded tests for RefineParameters, ArucoDetector, CharucoBoard, and CharucoDetector; migrated detection tests to new detector APIs and updated system test to use ArucoDetector.

Sequence Diagram

sequenceDiagram
  participant Managed as ArucoDetector
  participant PInvoke as NativeMethods
  participant CApi as OpenCvSharpExtern
  participant OpenCV as cv::aruco

  Managed->>PInvoke: aruco_ArucoDetector_create(dictionary, params, refine)
  PInvoke->>CApi: aruco_ArucoDetector_create(...)
  CApi->>OpenCV: new cv::aruco::ArucoDetector(...)
  OpenCV-->>CApi: detector ptr
  CApi-->>PInvoke: ptr
  PInvoke-->>Managed: native handle

  Managed->>PInvoke: aruco_ArucoDetector_detectMarkers(handle,image,...)
  PInvoke->>CApi: aruco_ArucoDetector_detectMarkers(...)
  CApi->>OpenCV: detector->detectMarkers(...)
  OpenCV-->>CApi: corners, ids, rejected
  CApi-->>PInvoke: marshaled vectors
  PInvoke-->>Managed: results (marshaled to arrays)
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

  • shimat/opencvsharp#1833: Touches NativeMethods_aruco.cs and adjusts ArUco P/Invoke declarations (removes [Pure]), related to P/Invoke changes in this PR.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the primary change: migrating the ArUco API from free-function design to detector-class design matching OpenCV 4.7+.
Linked Issues check ✅ Passed The PR successfully addresses issue #1796 by removing deprecated free-function wrappers (detectMarkers, estimatePoseSingleMarkers, detectCharucoDiamond, detectCharucoBoard, interpolateCornersCharuco) and adding class-based replacements (ArucoDetector, CharucoBoard, CharucoDetector, RefineParameters) that align with OpenCV 4.7+ API.
Out of Scope Changes check ✅ Passed The PR includes only changes directly related to the ArUco API migration: native P/Invoke declarations, wrapper classes, test updates, and C++ extern definitions. All modifications support the core objective of updating to OpenCV 4.7+ design.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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/aruco-new-api

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 and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

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/OpenCvSharpExtern/aruco.h`:
- Around line 308-323: The build fails because
aruco_ArucoDetector_refineDetectedMarkers declares recoveredIdxs as
std::vector<int>* but entity() only accepts cv::_InputArray*/cv::_OutputArray*;
change the recoveredIdxs parameter to cv::_OutputArray* (in the
aruco_ArucoDetector_refineDetectedMarkers signature) and update the call to use
entity(recoveredIdxs) (add a null check if other callers may pass nullptr) so
the call becomes detector->refineDetectedMarkers(..., entity(cameraMatrix),
entity(distCoeffs), entity(recoveredIdxs)).
- Around line 262-272: In aruco_drawDetectedCornersCharuco the ternary mixes
cv::_InputArray and cv::_InputOutputArray causing a type error; change the
idArray declaration to consistently use cv::_InputArray like in
aruco_drawDetectedDiamonds, e.g. construct idArray as cv::_InputArray idArray =
(ids != nullptr) ? cv::_InputArray(*ids) : cv::_InputArray(cv::noArray()); so
the ternary returns the same type before calling
cv::aruco::drawDetectedCornersCharuco.
🪄 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: 72feac1b-57d8-478f-a5c8-403902a61ae1

📥 Commits

Reviewing files that changed from the base of the PR and between 16ca2bc and 71a9a14.

📒 Files selected for processing (10)
  • src/OpenCvSharp/Internal/PInvoke/NativeMethods/NativeMethods_aruco.cs
  • src/OpenCvSharp/Modules/aruco/ArucoDetector.cs
  • src/OpenCvSharp/Modules/aruco/CharucoBoard.cs
  • src/OpenCvSharp/Modules/aruco/CharucoDetector.cs
  • src/OpenCvSharp/Modules/aruco/CvAruco.cs
  • src/OpenCvSharp/Modules/aruco/DetectorParameters.cs
  • src/OpenCvSharp/Modules/aruco/RefineParameters.cs
  • src/OpenCvSharpExtern/aruco.h
  • test/OpenCvSharp.Tests/aruco/ArucoTest.cs
  • test/OpenCvSharp.Tests/system/ExceptionTest.cs

Comment thread src/OpenCvSharpExtern/aruco.h
Comment thread src/OpenCvSharpExtern/aruco.h
- aruco_drawDetectedCornersCharuco: avoid ternary between _InputArray and
  _InputOutputArray (noArray()); use if-assignment instead
- aruco_ArucoDetector_refineDetectedMarkers: entity() does not accept
  std::vector<int>*; use _OutputArray + if-assignment for recoveredIdxs

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@shimat shimat merged commit ec06732 into main May 24, 2026
10 checks passed
@shimat shimat deleted the feature/aruco-new-api branch May 24, 2026 09:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

error: ‘detectMarkers’ is not a member of ‘cv::aruco’ when compiling OpenCvSharpExtern

1 participant