Conversation
📝 WalkthroughWalkthroughThis PR implements shape transformers and histogram cost extractors for OpenCvSharp, adding P/Invoke bindings, C++ interop wrappers, and managed C# classes that expose functionality for computing shape distances and transformations using thin-plate splines and affine transformers, plus multiple histogram-based cost extractor variants. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
🚥 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.
🧹 Nitpick comments (1)
test/OpenCvSharp.Tests/shape/HistogramCostExtractorTest.cs (1)
66-67: Optional: avoid repeated inline array allocations in test data.These literals trigger CA1861; moving them to static readonly fields keeps the test cleaner and removes analyzer noise.
♻️ Suggested refactor
public class HistogramCostExtractorTest : TestBase { + private static readonly float[] NormHist1 = { 0.5f, 0.3f, 0.2f }; + private static readonly float[] NormHist2 = { 0.4f, 0.4f, 0.2f }; + @@ public void Norm_BuildCostMatrix() { using var ext = NormHistogramCostExtractor.Create(); - using var hist1 = Mat.FromArray(new float[] { 0.5f, 0.3f, 0.2f }); - using var hist2 = Mat.FromArray(new float[] { 0.4f, 0.4f, 0.2f }); + using var hist1 = Mat.FromArray(NormHist1); + using var hist2 = Mat.FromArray(NormHist2); using var costMat = new Mat();🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@test/OpenCvSharp.Tests/shape/HistogramCostExtractorTest.cs` around lines 66 - 67, Move the inline float[] literals used to create Mat objects into static readonly fields to avoid repeated allocations and CA1861 warnings; e.g., add static readonly float[] fields (e.g., SampleHist1, SampleHist2) to the HistogramCostExtractorTest class and use Mat.FromArray(SampleHist1) and Mat.FromArray(SampleHist2) instead of new float[] { ... } in the test setup (replace usages that create hist1 and hist2).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@test/OpenCvSharp.Tests/shape/HistogramCostExtractorTest.cs`:
- Around line 66-67: Move the inline float[] literals used to create Mat objects
into static readonly fields to avoid repeated allocations and CA1861 warnings;
e.g., add static readonly float[] fields (e.g., SampleHist1, SampleHist2) to the
HistogramCostExtractorTest class and use Mat.FromArray(SampleHist1) and
Mat.FromArray(SampleHist2) instead of new float[] { ... } in the test setup
(replace usages that create hist1 and hist2).
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: e1eb3dc7-5463-484c-8d1e-b0fd04d43a36
📒 Files selected for processing (21)
src/OpenCvSharp/Internal/PInvoke/NativeMethods/shape/NativeMethods_shape_HistogramCostExtractor.cssrc/OpenCvSharp/Internal/PInvoke/NativeMethods/shape/NativeMethods_shape_ShapeDistanceExtractor.cssrc/OpenCvSharp/Internal/PInvoke/NativeMethods/shape/NativeMethods_shape_ShapeTransformer.cssrc/OpenCvSharp/Modules/shape/AffineTransformer.cssrc/OpenCvSharp/Modules/shape/ChiHistogramCostExtractor.cssrc/OpenCvSharp/Modules/shape/CvShape.cssrc/OpenCvSharp/Modules/shape/EMDHistogramCostExtractor.cssrc/OpenCvSharp/Modules/shape/EMDL1HistogramCostExtractor.cssrc/OpenCvSharp/Modules/shape/HistogramCostExtractor.cssrc/OpenCvSharp/Modules/shape/NormHistogramCostExtractor.cssrc/OpenCvSharp/Modules/shape/ShapeContextDistanceExtractor.cssrc/OpenCvSharp/Modules/shape/ShapeTransformer.cssrc/OpenCvSharp/Modules/shape/ThinPlateSplineShapeTransformer.cssrc/OpenCvSharpExtern/shape.cppsrc/OpenCvSharpExtern/shape_HistogramCostExtractor.hsrc/OpenCvSharpExtern/shape_ShapeDistanceExtractor.hsrc/OpenCvSharpExtern/shape_ShapeTransformer.htest/OpenCvSharp.Tests/shape/HausdorffDistanceExtractorTest.cstest/OpenCvSharp.Tests/shape/HistogramCostExtractorTest.cstest/OpenCvSharp.Tests/shape/ShapeContextDistanceExtractorTest.cstest/OpenCvSharp.Tests/shape/ThinPlateSplineShapeTransformerTest.cs
Fix #1758
This pull request adds comprehensive support for the OpenCV "shape" module to the OpenCvSharp library. It introduces new classes for shape transformers and histogram cost extractors, provides factory methods for easy creation, and implements the necessary P/Invoke bindings to the native OpenCV functions. The changes enable advanced shape matching and transformation features in managed code.
New shape transformers and histogram cost extractors:
Added new managed classes for shape transformers (
AffineTransformer,ThinPlateSplineShapeTransformer) and for histogram cost extractors (NormHistogramCostExtractor,EMDHistogramCostExtractor,ChiHistogramCostExtractor,EMDL1HistogramCostExtractor), each wrapping the corresponding native OpenCV functionality. (AffineTransformer.cs[1]ChiHistogramCostExtractor.cs[2]EMDHistogramCostExtractor.cs[3]EMDL1HistogramCostExtractor.cs[4]HistogramCostExtractor.cs[5]Implemented the abstract base class
HistogramCostExtractorwith core properties and methods such asBuildCostMatrix,NDummies, andDefaultCost. (HistogramCostExtractor.cssrc/OpenCvSharp/Modules/shape/HistogramCostExtractor.csR1-R87)P/Invoke and native bindings:
NativeMethods_shape_HistogramCostExtractor.cs[1]NativeMethods_shape_ShapeTransformer.cs[2]NativeMethods_shape_ShapeDistanceExtractor.cs[3]API surface and usability improvements:
CvShapestatic class with factory methods for creating all supported transformers and cost extractors, as well as a managed wrapper for the EMDL1 free function. (CvShape.cssrc/OpenCvSharp/Modules/shape/CvShape.csR1-R103)Integration and extensibility:
AffineTransformer.cs[1]NativeMethods_shape_ShapeTransformer.cs[2]These changes significantly expand the capabilities of OpenCvSharp for shape analysis and matching, making advanced OpenCV shape algorithms available to .NET developers.
Summary by CodeRabbit
Release Notes
ThinPlateSplineShapeTransformer,AffineTransformer) for estimating and applying shape transformations.NormHistogramCostExtractor,EMDHistogramCostExtractor,ChiHistogramCostExtractor,EMDL1HistogramCostExtractor) for advanced shape matching and distance computation.ShapeContextDistanceExtractorwithSetCostExtractor()andSetTransformAlgorithm()configuration methods.EMDL1()function for computing Earth Mover's Distance-based shape distances.