From 92de4241d3878967f66811170896f6c4c72cddcb Mon Sep 17 00:00:00 2001 From: shimat Date: Wed, 19 Aug 2015 19:30:37 +0900 Subject: [PATCH] Added Feature2D.DetectAndCompute --- src/OpenCvSharp/OpenCvSharp.csproj | 1 + .../features2d/NativeMethods_features2d.cs | 35 +----- .../NativeMethods_features2d_Feature2D.cs | 45 +++++++ .../modules/features2d/Feature2D.cs | 119 ++++++++++++------ .../features2d_FeatureDetector.h | 7 ++ 5 files changed, 138 insertions(+), 69 deletions(-) create mode 100644 src/OpenCvSharp/PInvoke/features2d/NativeMethods_features2d_Feature2D.cs diff --git a/src/OpenCvSharp/OpenCvSharp.csproj b/src/OpenCvSharp/OpenCvSharp.csproj index af7f103bf..d681a8975 100644 --- a/src/OpenCvSharp/OpenCvSharp.csproj +++ b/src/OpenCvSharp/OpenCvSharp.csproj @@ -119,6 +119,7 @@ + diff --git a/src/OpenCvSharp/PInvoke/features2d/NativeMethods_features2d.cs b/src/OpenCvSharp/PInvoke/features2d/NativeMethods_features2d.cs index 7776491f2..91c7fefd0 100644 --- a/src/OpenCvSharp/PInvoke/features2d/NativeMethods_features2d.cs +++ b/src/OpenCvSharp/PInvoke/features2d/NativeMethods_features2d.cs @@ -9,38 +9,6 @@ static partial class NativeMethods { // ReSharper disable InconsistentNaming - // FeatureDetector - [DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)] - public static extern IntPtr features2d_Feature2D_info(IntPtr obj); - - [DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)] - public static extern void features2d_Feature2D_detect_Mat1(IntPtr detector, IntPtr image, IntPtr keypoints, IntPtr mask); - [DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)] - public static extern void features2d_Feature2D_detect_Mat2(IntPtr detector, IntPtr[] images, int imageLength, IntPtr keypoints, IntPtr[] mask); - [DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)] - public static extern void features2d_Feature2D_detect_InputArray(IntPtr detector, IntPtr image, IntPtr keypoints, IntPtr mask); - - [DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)] - public static extern void features2d_Feature2D_compute1(IntPtr obj, IntPtr image, IntPtr keypoints, IntPtr descriptors); - [DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)] - public static extern void features2d_Feature2D_compute2( - IntPtr detector, IntPtr[] images, int imageLength, - IntPtr keypoints, IntPtr[] descriptors, int descriptorsLength); - - [DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)] - public static extern IntPtr features2d_Ptr_Feature2D_get(IntPtr ptr); - [DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)] - public static extern void features2d_Ptr_Feature2D_delete(IntPtr ptr); - - [DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)] - public static extern int features2d_Feature2D_descriptorSize(IntPtr obj); - [DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)] - public static extern int features2d_Feature2D_descriptorType(IntPtr obj); - [DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)] - public static extern int features2d_Feature2D_defaultNorm(IntPtr obj); - [DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)] - public static extern int features2d_Feature2D_empty(IntPtr obj); - // DenseFeatureDetector /* [DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)] @@ -57,8 +25,7 @@ public static extern IntPtr features2d_DenseFeatureDetector_new( [DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)] public static extern void features2d_Ptr_DenseFeatureDetector_delete(IntPtr ptr); */ - - + [DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)] public static extern void features2d_drawKeypoints(IntPtr image, KeyPoint[] keypoints, int keypointsLength, IntPtr outImage, Scalar color, int flags); diff --git a/src/OpenCvSharp/PInvoke/features2d/NativeMethods_features2d_Feature2D.cs b/src/OpenCvSharp/PInvoke/features2d/NativeMethods_features2d_Feature2D.cs new file mode 100644 index 000000000..db1444e2f --- /dev/null +++ b/src/OpenCvSharp/PInvoke/features2d/NativeMethods_features2d_Feature2D.cs @@ -0,0 +1,45 @@ +using System; +using System.Runtime.InteropServices; + +#pragma warning disable 1591 + +namespace OpenCvSharp +{ + static partial class NativeMethods + { + // ReSharper disable InconsistentNaming + + [DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)] + public static extern IntPtr features2d_Ptr_Feature2D_get(IntPtr ptr); + [DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)] + public static extern void features2d_Ptr_Feature2D_delete(IntPtr ptr); + + [DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)] + public static extern void features2d_Feature2D_detect_Mat1(IntPtr detector, IntPtr image, IntPtr keypoints, IntPtr mask); + [DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)] + public static extern void features2d_Feature2D_detect_Mat2(IntPtr detector, IntPtr[] images, int imageLength, IntPtr keypoints, IntPtr[] mask); + [DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)] + public static extern void features2d_Feature2D_detect_InputArray(IntPtr detector, IntPtr image, IntPtr keypoints, IntPtr mask); + + [DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)] + public static extern void features2d_Feature2D_compute1(IntPtr obj, IntPtr image, IntPtr keypoints, IntPtr descriptors); + [DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)] + public static extern void features2d_Feature2D_compute2( + IntPtr detector, IntPtr[] images, int imageLength, + IntPtr keypoints, IntPtr[] descriptors, int descriptorsLength); + + [DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)] + public static extern void features2d_Feature2D_detectAndCompute( + IntPtr detector, IntPtr image, IntPtr mask, + IntPtr keypoints, IntPtr descriptors, int useProvidedKeypoints); + + [DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)] + public static extern int features2d_Feature2D_descriptorSize(IntPtr obj); + [DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)] + public static extern int features2d_Feature2D_descriptorType(IntPtr obj); + [DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)] + public static extern int features2d_Feature2D_defaultNorm(IntPtr obj); + [DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)] + public static extern int features2d_Feature2D_empty(IntPtr obj); + } +} \ No newline at end of file diff --git a/src/OpenCvSharp/modules/features2d/Feature2D.cs b/src/OpenCvSharp/modules/features2d/Feature2D.cs index 5165a021f..cccd02edf 100644 --- a/src/OpenCvSharp/modules/features2d/Feature2D.cs +++ b/src/OpenCvSharp/modules/features2d/Feature2D.cs @@ -85,6 +85,62 @@ protected override void Dispose(bool disposing) } } } + + /// + /// + /// + /// + public virtual int DescriptorSize + { + get + { + if (disposed) + throw new ObjectDisposedException(GetType().Name); + return NativeMethods.features2d_Feature2D_descriptorSize(ptr); + } + } + + /// + /// + /// + /// + public virtual int DescriptorType + { + get + { + if (disposed) + throw new ObjectDisposedException(GetType().Name); + return NativeMethods.features2d_Feature2D_descriptorType(ptr); + } + } + + /// + /// + /// + /// + public virtual int DefaultNorm + { + get + { + if (disposed) + throw new ObjectDisposedException(GetType().Name); + return NativeMethods.features2d_Feature2D_defaultNorm(ptr); + } + } + + /// + /// Return true if detector object is empty + /// + /// + public virtual bool Empty + { + get + { + if (disposed) + throw new ObjectDisposedException(GetType().Name); + return NativeMethods.features2d_Feature2D_empty(ptr) != 0; + } + } /// /// Detect keypoints in an image. @@ -256,47 +312,40 @@ public virtual void Compute(IEnumerable images, ref KeyPoint[][] keypoints, } /// - /// - /// - /// - public virtual int DescriptorSize() - { - if (disposed) - throw new ObjectDisposedException(GetType().Name); - return NativeMethods.features2d_Feature2D_descriptorSize(ptr); - } - - /// - /// + /// Detects keypoints and computes the descriptors /// - /// - public virtual int DescriptorType() + /// + /// + /// + /// + /// + public virtual void DetectAndCompute( + InputArray image, + InputArray mask, + out KeyPoint[] keypoints, + OutputArray descriptors, + bool useProvidedKeypoints = false) { if (disposed) throw new ObjectDisposedException(GetType().Name); - return NativeMethods.features2d_Feature2D_descriptorType(ptr); - } + if (image == null) + throw new ArgumentNullException("image"); + if (descriptors == null) + throw new ArgumentNullException("descriptors"); + image.ThrowIfDisposed(); + if (mask != null) + mask.ThrowIfDisposed(); - /// - /// - /// - /// - public virtual int DefaultNorm() - { - if (disposed) - throw new ObjectDisposedException(GetType().Name); - return NativeMethods.features2d_Feature2D_defaultNorm(ptr); - } + using (var keypointsVec = new VectorOfKeyPoint()) + { + NativeMethods.features2d_Feature2D_detectAndCompute( + ptr, image.CvPtr, Cv2.ToPtr(mask), keypointsVec.CvPtr, descriptors.CvPtr, useProvidedKeypoints ? 1 : 0); + keypoints = keypointsVec.ToArray(); + } - /// - /// Return true if detector object is empty - /// - /// - public virtual bool Empty() - { - if (disposed) - throw new ObjectDisposedException(GetType().Name); - return NativeMethods.features2d_Feature2D_empty(ptr) != 0; + GC.KeepAlive(image); + GC.KeepAlive(mask); + descriptors.Fix(); } } } diff --git a/src/OpenCvSharpExtern/features2d_FeatureDetector.h b/src/OpenCvSharpExtern/features2d_FeatureDetector.h index fcfb88c77..1bbb4c671 100644 --- a/src/OpenCvSharpExtern/features2d_FeatureDetector.h +++ b/src/OpenCvSharpExtern/features2d_FeatureDetector.h @@ -68,6 +68,13 @@ CVAPI(void) features2d_Feature2D_compute2( detector->compute(imageVec, *keypoints, descriptorsVec); } +CVAPI(void) features2d_Feature2D_detectAndCompute( + cv::Feature2D *detector, cv::_InputArray *image, cv::_InputArray *mask, + std::vector *keypoints, cv::_OutputArray *descriptors, int useProvidedKeypoints) +{ + detector->detectAndCompute(entity(image), entity(mask), *keypoints, *descriptors, useProvidedKeypoints != 0); +} + CVAPI(int) features2d_Feature2D_descriptorSize(cv::Ptr *obj) { return obj->get()->descriptorSize();