Conversation
📝 WalkthroughWalkthroughThis PR adds comprehensive support for OpenCV saliency computation algorithms including MotionSaliencyBinWangApr2014, ObjectnessBING, StaticSaliencySpectralResidual, and StaticSaliencyFineGrained. The implementation spans C++ interop wrappers, C# P/Invoke bindings, managed algorithm classes, and unit tests. Changes
Sequence DiagramsequenceDiagram
participant User as User Code
participant API as C# Saliency API<br/>(e.g., MotionSaliencyBinWangApr2014)
participant PInvoke as P/Invoke Native<br/>Binding
participant Cpp as C++ Wrapper<br/>(saliency_*.h)
participant OpenCV as OpenCV<br/>C++ Library
User->>API: Create()
API->>PInvoke: saliency_MotionSaliencyBinWangApr2014_create()
PInvoke->>Cpp: Call extern function
Cpp->>OpenCV: cv::saliency::MotionSaliencyBinWangApr2014::create()
OpenCV-->>Cpp: cv::Ptr<...>
Cpp-->>PInvoke: ExceptionStatus + IntPtr
PInvoke-->>API: IntPtr (native handle)
API-->>User: Managed instance
User->>API: SetImagesize(width, height)
API->>PInvoke: saliency_MotionSaliencyBinWangApr2014_setImagesize()
PInvoke->>Cpp: Call extern function
Cpp->>OpenCV: obj->setImagesize(W, H)
OpenCV-->>Cpp: void
Cpp-->>PInvoke: ExceptionStatus
PInvoke-->>API: ExceptionStatus
API-->>User: (no return)
User->>API: Init()
API->>PInvoke: saliency_MotionSaliencyBinWangApr2014_init()
PInvoke->>Cpp: Call extern function
Cpp->>OpenCV: obj->init()
OpenCV-->>Cpp: bool result
Cpp-->>PInvoke: ExceptionStatus + int (0/1)
PInvoke-->>API: ExceptionStatus + int
API-->>User: bool (success)
User->>API: ComputeSaliency(image, saliencyMap)
API->>PInvoke: saliency_MotionSaliencyBinWangApr2014_computeSaliency()
PInvoke->>Cpp: Call extern function
Cpp->>OpenCV: obj->computeSaliency(image, saliencyMap)
OpenCV-->>Cpp: bool result
Cpp-->>PInvoke: ExceptionStatus + int (0/1)
PInvoke-->>API: ExceptionStatus + int
API->>API: Fix output array
API-->>User: bool (success/failure)
User->>API: Dispose()
API->>PInvoke: saliency_Ptr_MotionSaliencyBinWangApr2014_delete()
PInvoke->>Cpp: Call extern function
Cpp->>OpenCV: delete cv::Ptr<...>
OpenCV-->>Cpp: (cleanup)
Cpp-->>PInvoke: ExceptionStatus
PInvoke-->>API: ExceptionStatus
API-->>User: (disposed)
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ 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: 1
🧹 Nitpick comments (1)
test/OpenCvSharp.Tests/saliency/MotionSaliencyTest.cs (1)
48-51: Consider assertingInit()return value for consistency.For robustness, assert the
Init()return value in theComputeSaliencytest, similar to how it's done inSetImagesizeAndInit.Suggested fix
using var saliency = MotionSaliencyBinWangApr2014.Create(); saliency.SetImagesize(width, height); - saliency.Init(); + Assert.True(saliency.Init());🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@test/OpenCvSharp.Tests/saliency/MotionSaliencyTest.cs` around lines 48 - 51, The test currently calls MotionSaliencyBinWangApr2014.Create(), SetImagesize(width, height) and Init() without checking Init()'s return; update the ComputeSaliency test to assert the boolean result of saliency.Init() (like in SetImagesizeAndInit) so initialization failures fail the test: after calling saliency.SetImagesize(...) call var ok = saliency.Init() and assert ok (or Assert.IsTrue/Assert.True) before proceeding to ComputeSaliency to ensure consistent behavior across tests.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/OpenCvSharp/Modules/saliency/ObjectnessBING.cs`:
- Around line 74-94: The P/Invoke signatures for
saliency_ObjectnessBING_setTrainingPath and saliency_ObjectnessBING_setBBResDir
currently use ANSI marshaling and must be updated to UTF-8-safe marshaling;
locate their declarations in the NativeMethods class and change the string
parameter marshaling from [MarshalAs(UnmanagedType.LPStr)] to a UTF-8-safe
alternative (e.g., [MarshalAs(UnmanagedType.LPUTF8Str)] for .NET 5+ or the
project’s platform-specific UTF8 marshaling wrapper used elsewhere like
imgcodecs_imread), then rebuild and run tests to ensure non-ASCII file paths are
handled correctly when calling SetTrainingPath and SetBBResDir.
---
Nitpick comments:
In `@test/OpenCvSharp.Tests/saliency/MotionSaliencyTest.cs`:
- Around line 48-51: The test currently calls
MotionSaliencyBinWangApr2014.Create(), SetImagesize(width, height) and Init()
without checking Init()'s return; update the ComputeSaliency test to assert the
boolean result of saliency.Init() (like in SetImagesizeAndInit) so
initialization failures fail the test: after calling saliency.SetImagesize(...)
call var ok = saliency.Init() and assert ok (or Assert.IsTrue/Assert.True)
before proceeding to ComputeSaliency to ensure consistent behavior across tests.
🪄 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: 86144a2a-f568-4754-88ba-22e7af998341
📒 Files selected for processing (17)
src/OpenCvSharp/Internal/PInvoke/NativeMethods/saliency/NativeMethods_saliency_MotionSaliencyBinWangApr2014.cssrc/OpenCvSharp/Internal/PInvoke/NativeMethods/saliency/NativeMethods_saliency_ObjectnessBING.cssrc/OpenCvSharp/Internal/PInvoke/NativeMethods/saliency/NativeMethods_saliency_StaticSaliencyFineGrained.cssrc/OpenCvSharp/Internal/PInvoke/NativeMethods/saliency/NativeMethods_saliency_StaticSaliencySpectralResidual.cssrc/OpenCvSharp/Modules/saliency/MotionSaliencyBinWangApr2014.cssrc/OpenCvSharp/Modules/saliency/ObjectnessBING.cssrc/OpenCvSharp/Modules/saliency/StaticSaliencyFineGrained.cssrc/OpenCvSharp/Modules/saliency/StaticSaliencySpectralResidual.cssrc/OpenCvSharpExtern/include_opencv.hsrc/OpenCvSharpExtern/saliency.cppsrc/OpenCvSharpExtern/saliency_MotionSaliencyBinWangApr2014.hsrc/OpenCvSharpExtern/saliency_ObjectnessBING.hsrc/OpenCvSharpExtern/saliency_StaticSaliencyFineGrained.hsrc/OpenCvSharpExtern/saliency_StaticSaliencySpectralResidual.htest/OpenCvSharp.Tests/saliency/MotionSaliencyTest.cstest/OpenCvSharp.Tests/saliency/ObjectnessBINGTest.cstest/OpenCvSharp.Tests/saliency/StaticSaliencyTest.cs
| public void SetTrainingPath(string trainingPath) | ||
| { | ||
| ThrowIfDisposed(); | ||
| if (trainingPath is null) | ||
| throw new ArgumentNullException(nameof(trainingPath)); | ||
| NativeMethods.HandleException( | ||
| NativeMethods.saliency_ObjectnessBING_setTrainingPath(RawPtr, trainingPath)); | ||
| GC.KeepAlive(this); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Sets the directory path for writing optional output results. | ||
| /// </summary> | ||
| public void SetBBResDir(string resultsDir) | ||
| { | ||
| ThrowIfDisposed(); | ||
| if (resultsDir is null) | ||
| throw new ArgumentNullException(nameof(resultsDir)); | ||
| NativeMethods.HandleException( | ||
| NativeMethods.saliency_ObjectnessBING_setBBResDir(RawPtr, resultsDir)); | ||
| GC.KeepAlive(this); |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Verify how ObjectnessBING path setters marshal strings in P/Invoke layer.
set -euo pipefail
echo "== Locate P/Invoke declaration file =="
fd -i 'NativeMethods_saliency_ObjectnessBING.cs' src
echo
echo "== Inspect DllImport/LibraryImport attributes and string param declarations =="
for f in $(fd -i 'NativeMethods_saliency_ObjectnessBING.cs' src); do
echo "--- $f ---"
sed -n '1,260p' "$f"
done
echo
echo "== Focused search for marshaling hints =="
rg -n -C3 --type=cs 'saliency_ObjectnessBING_setTrainingPath|saliency_ObjectnessBING_setBBResDir|StringMarshalling|MarshalAs|CharSet|Utf8' srcRepository: shimat/opencvsharp
Length of output: 50375
Update P/Invoke string marshaling for path parameters to UTF-8-safe alternatives.
The saliency_ObjectnessBING_setTrainingPath and saliency_ObjectnessBING_setBBResDir P/Invoke declarations use [MarshalAs(UnmanagedType.LPStr)], which is ANSI/ASCII marshaling and will fail on non-ASCII file paths (e.g., accented characters, CJK, emoji in directory/file names). Other path-handling P/Invoke methods in the codebase (e.g., imgcodecs_imread) use platform-specific or UTF-8 marshaling. Change these declarations to either UnmanagedType.LPUTF8Str (for .NET 5+) or adopt the platform-specific marshaling pattern used elsewhere in the codebase.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@src/OpenCvSharp/Modules/saliency/ObjectnessBING.cs` around lines 74 - 94, The
P/Invoke signatures for saliency_ObjectnessBING_setTrainingPath and
saliency_ObjectnessBING_setBBResDir currently use ANSI marshaling and must be
updated to UTF-8-safe marshaling; locate their declarations in the NativeMethods
class and change the string parameter marshaling from
[MarshalAs(UnmanagedType.LPStr)] to a UTF-8-safe alternative (e.g.,
[MarshalAs(UnmanagedType.LPUTF8Str)] for .NET 5+ or the project’s
platform-specific UTF8 marshaling wrapper used elsewhere like imgcodecs_imread),
then rebuild and run tests to ensure non-ASCII file paths are handled correctly
when calling SetTrainingPath and SetBBResDir.
Fix #1826
This pull request adds C# bindings for the OpenCV Saliency module, enabling .NET users to access various saliency detection algorithms. The changes introduce new wrapper classes and their corresponding native method declarations for four different saliency algorithms, providing object-oriented APIs for creating, configuring, and using these algorithms in managed code.
The most important changes are:
New Saliency Algorithm Wrappers:
MotionSaliencyBinWangApr2014class, which wraps the fast self-tuning background subtraction algorithm for motion saliency detection, including methods for initialization, parameter setting, and saliency computation. (MotionSaliencyBinWangApr2014.cs, src/OpenCvSharp/Modules/saliency/MotionSaliencyBinWangApr2014.csR1-R125)ObjectnessBINGclass, which provides access to the BING objectness saliency algorithm, including model file management, proposal generation, and parameter configuration. (ObjectnessBING.cs, src/OpenCvSharp/Modules/saliency/ObjectnessBING.csR1-R162)StaticSaliencyFineGrainedclass, exposing the fine-grained static saliency detection algorithm with methods for computing saliency and binary maps. (StaticSaliencyFineGrained.cs, src/OpenCvSharp/Modules/saliency/StaticSaliencyFineGrained.csR1-R78)StaticSaliencySpectralResidualclass, providing the spectral residual static saliency algorithm with support for image size configuration and binary map computation. (StaticSaliencySpectralResidual.cs, src/OpenCvSharp/Internal/PInvoke/NativeMethods/saliency/NativeMethods_saliency_StaticSaliencySpectralResidual.csR1-R45)P/Invoke Declarations:
NativeMethods_saliency_MotionSaliencyBinWangApr2014.cs, [1];NativeMethods_saliency_ObjectnessBING.cs, [2];NativeMethods_saliency_StaticSaliencyFineGrained.cs, [3];NativeMethods_saliency_StaticSaliencySpectralResidual.cs, [4]Summary by CodeRabbit
Release Notes
New Features
MotionSaliencyBinWangApr2014for motion-based saliency detection,ObjectnessBINGfor objectness detection with bounding boxes,StaticSaliencySpectralResidualfor spectral residual-based saliency, andStaticSaliencyFineGrainedfor fine-grained static saliency analysis. Each algorithm supports computing saliency maps and configurable parameters.Tests