Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System.Runtime.InteropServices;

#pragma warning disable 1591
#pragma warning disable CA1401 // P/Invokes should not be visible
#pragma warning disable CA2101 // Specify marshaling for P/Invoke string arguments
#pragma warning disable IDE1006 // Naming style

namespace OpenCvSharp.Internal;

static partial class NativeMethods
{
// ReSharper disable InconsistentNaming

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus features_ANNIndex_create(int dim, int distType, out IntPtr returnValue);

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus features_Ptr_ANNIndex_get(IntPtr ptr, out IntPtr returnValue);

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus features_Ptr_ANNIndex_delete(IntPtr ptr);

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus features_ANNIndex_addItems(IntPtr obj, IntPtr features);

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus features_ANNIndex_build(IntPtr obj, int trees);

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus features_ANNIndex_knnSearch(
IntPtr obj, IntPtr query, IntPtr indices, IntPtr dists, int knn, int search_k);

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, BestFitMapping = false, ThrowOnUnmappableChar = true,
EntryPoint = "features_ANNIndex_save")]
public static extern ExceptionStatus features_ANNIndex_save_NotWindows(
IntPtr obj, [MarshalAs(StringUnmanagedTypeNotWindows)] string filename, int prefault);

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, BestFitMapping = false, ThrowOnUnmappableChar = true,
EntryPoint = "features_ANNIndex_save")]
public static extern ExceptionStatus features_ANNIndex_save_Windows(
IntPtr obj, [MarshalAs(StringUnmanagedTypeWindows)] string filename, int prefault);

public static ExceptionStatus features_ANNIndex_save(IntPtr obj, string filename, int prefault)
=> IsWindows()
? features_ANNIndex_save_Windows(obj, filename, prefault)
: features_ANNIndex_save_NotWindows(obj, filename, prefault);

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, BestFitMapping = false, ThrowOnUnmappableChar = true,
EntryPoint = "features_ANNIndex_load")]
public static extern ExceptionStatus features_ANNIndex_load_NotWindows(
IntPtr obj, [MarshalAs(StringUnmanagedTypeNotWindows)] string filename, int prefault);

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, BestFitMapping = false, ThrowOnUnmappableChar = true,
EntryPoint = "features_ANNIndex_load")]
public static extern ExceptionStatus features_ANNIndex_load_Windows(
IntPtr obj, [MarshalAs(StringUnmanagedTypeWindows)] string filename, int prefault);

public static ExceptionStatus features_ANNIndex_load(IntPtr obj, string filename, int prefault)
=> IsWindows()
? features_ANNIndex_load_Windows(obj, filename, prefault)
: features_ANNIndex_load_NotWindows(obj, filename, prefault);

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus features_ANNIndex_getTreeNumber(IntPtr obj, out int returnValue);

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus features_ANNIndex_getItemNumber(IntPtr obj, out int returnValue);

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, BestFitMapping = false, ThrowOnUnmappableChar = true,
EntryPoint = "features_ANNIndex_setOnDiskBuild")]
public static extern ExceptionStatus features_ANNIndex_setOnDiskBuild_NotWindows(
IntPtr obj, [MarshalAs(StringUnmanagedTypeNotWindows)] string filename, out int returnValue);

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, BestFitMapping = false, ThrowOnUnmappableChar = true,
EntryPoint = "features_ANNIndex_setOnDiskBuild")]
public static extern ExceptionStatus features_ANNIndex_setOnDiskBuild_Windows(
IntPtr obj, [MarshalAs(StringUnmanagedTypeWindows)] string filename, out int returnValue);

public static ExceptionStatus features_ANNIndex_setOnDiskBuild(IntPtr obj, string filename, out int returnValue)
=> IsWindows()
? features_ANNIndex_setOnDiskBuild_Windows(obj, filename, out returnValue)
: features_ANNIndex_setOnDiskBuild_NotWindows(obj, filename, out returnValue);

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus features_ANNIndex_setSeed(IntPtr obj, int seed);
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,40 @@ public static extern ExceptionStatus features_FlannBasedMatcher_add(

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus features_Ptr_FlannBasedMatcher_delete(IntPtr ptr);

#region LightGlueMatcher

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, BestFitMapping = false, ThrowOnUnmappableChar = true,
EntryPoint = "features_LightGlueMatcher_create")]
public static extern ExceptionStatus features_LightGlueMatcher_create_NotWindows(
[MarshalAs(StringUnmanagedTypeNotWindows)] string modelPath, float scoreThreshold, int backend, int target, out IntPtr returnValue);

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, BestFitMapping = false, ThrowOnUnmappableChar = true,
EntryPoint = "features_LightGlueMatcher_create")]
public static extern ExceptionStatus features_LightGlueMatcher_create_Windows(
[MarshalAs(StringUnmanagedTypeWindows)] string modelPath, float scoreThreshold, int backend, int target, out IntPtr returnValue);

public static ExceptionStatus features_LightGlueMatcher_create(string modelPath, float scoreThreshold, int backend, int target, out IntPtr returnValue)
=> IsWindows()
? features_LightGlueMatcher_create_Windows(modelPath, scoreThreshold, backend, target, out returnValue)
: features_LightGlueMatcher_create_NotWindows(modelPath, scoreThreshold, backend, target, out returnValue);

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus features_LightGlueMatcher_create_buffer(
byte[] modelData, IntPtr modelDataLength, float scoreThreshold, int backend, int target, out IntPtr returnValue);

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus features_LightGlueMatcher_setPairInfo(
IntPtr obj, IntPtr queryKpts, IntPtr trainKpts, Size queryImageSize, Size trainImageSize);

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus features_LightGlueMatcher_clearPairInfo(IntPtr obj);

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus features_Ptr_LightGlueMatcher_get(IntPtr ptr, out IntPtr returnValue);

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus features_Ptr_LightGlueMatcher_delete(IntPtr ptr);

#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,87 @@ public static extern ExceptionStatus features_SimpleBlobDetector_create(

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus features_Ptr_Feature2D_get(IntPtr obj, out IntPtr returnValue);

#region AffineFeature

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus features_AffineFeature_create(
IntPtr backend, int maxTilt, int minTilt, float tiltStep, float rotateStepBase, out IntPtr returnValue);

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus features_AffineFeature_setViewParams(
IntPtr obj, float[] tilts, int tiltsLength, float[] rolls, int rollsLength);

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus features_AffineFeature_getViewParams(IntPtr obj, IntPtr tilts, IntPtr rolls);

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus features_Ptr_AffineFeature_delete(IntPtr ptr);

#endregion

#region DISK

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, BestFitMapping = false, ThrowOnUnmappableChar = true,
EntryPoint = "features_DISK_create")]
public static extern ExceptionStatus features_DISK_create_NotWindows(
[MarshalAs(StringUnmanagedTypeNotWindows)] string modelPath, int maxKeypoints, float scoreThreshold, Size imageSize, int backendId, int targetId, out IntPtr returnValue);

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, BestFitMapping = false, ThrowOnUnmappableChar = true,
EntryPoint = "features_DISK_create")]
public static extern ExceptionStatus features_DISK_create_Windows(
[MarshalAs(StringUnmanagedTypeWindows)] string modelPath, int maxKeypoints, float scoreThreshold, Size imageSize, int backendId, int targetId, out IntPtr returnValue);

public static ExceptionStatus features_DISK_create(string modelPath, int maxKeypoints, float scoreThreshold, Size imageSize, int backendId, int targetId, out IntPtr returnValue)
=> IsWindows()
? features_DISK_create_Windows(modelPath, maxKeypoints, scoreThreshold, imageSize, backendId, targetId, out returnValue)
: features_DISK_create_NotWindows(modelPath, maxKeypoints, scoreThreshold, imageSize, backendId, targetId, out returnValue);

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus features_DISK_create_buffer(
byte[] bufferModel, IntPtr bufferModelLength, int maxKeypoints, float scoreThreshold, Size imageSize, int backendId, int targetId, out IntPtr returnValue);

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus features_DISK_setMaxKeypoints(IntPtr obj, int maxKeypoints);
[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus features_DISK_getMaxKeypoints(IntPtr obj, out int returnValue);
[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus features_DISK_setScoreThreshold(IntPtr obj, float threshold);
[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus features_DISK_getScoreThreshold(IntPtr obj, out float returnValue);
[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus features_DISK_setImageSize(IntPtr obj, Size size);
[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus features_DISK_getImageSize(IntPtr obj, out Size returnValue);

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus features_Ptr_DISK_delete(IntPtr ptr);

#endregion

#region ALIKED

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, BestFitMapping = false, ThrowOnUnmappableChar = true,
EntryPoint = "features_ALIKED_create")]
public static extern ExceptionStatus features_ALIKED_create_NotWindows(
[MarshalAs(StringUnmanagedTypeNotWindows)] string modelPath, Size inputSize, int normalizeDescriptors, int engine, int backend, int target, out IntPtr returnValue);

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, BestFitMapping = false, ThrowOnUnmappableChar = true,
EntryPoint = "features_ALIKED_create")]
public static extern ExceptionStatus features_ALIKED_create_Windows(
[MarshalAs(StringUnmanagedTypeWindows)] string modelPath, Size inputSize, int normalizeDescriptors, int engine, int backend, int target, out IntPtr returnValue);

public static ExceptionStatus features_ALIKED_create(string modelPath, Size inputSize, int normalizeDescriptors, int engine, int backend, int target, out IntPtr returnValue)
=> IsWindows()
? features_ALIKED_create_Windows(modelPath, inputSize, normalizeDescriptors, engine, backend, target, out returnValue)
: features_ALIKED_create_NotWindows(modelPath, inputSize, normalizeDescriptors, engine, backend, target, out returnValue);

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus features_ALIKED_create_buffer(
byte[] modelData, IntPtr modelDataLength, Size inputSize, int normalizeDescriptors, int engine, int backend, int target, out IntPtr returnValue);

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus features_Ptr_ALIKED_delete(IntPtr ptr);

#endregion
}
70 changes: 70 additions & 0 deletions src/OpenCvSharp/Modules/features/ALIKED.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using OpenCvSharp.Internal;

namespace OpenCvSharp;

// ReSharper disable once InconsistentNaming
/// <summary>
/// ALIKED: deep learning based local feature detector and descriptor (OpenCV 5).
/// Runs an ONNX model through the DNN module. Requires OpenCV built with the dnn module.
/// </summary>
public class ALIKED : Feature2D
{
/// <summary>
/// Creates instance by raw pointer cv::ALIKED*
/// </summary>
private ALIKED(IntPtr smartPtr, IntPtr rawPtr)
: base(smartPtr, rawPtr, p => NativeMethods.HandleException(NativeMethods.features_Ptr_ALIKED_delete(p)))
{
}

/// <summary>
/// Creates ALIKED from a model file path.
/// </summary>
/// <param name="modelPath">Path to the ONNX model file.</param>
/// <param name="inputSize">Input image size for the network. Default is 640x640.</param>
/// <param name="normalizeDescriptors">Whether to L2-normalize descriptors. Default is true.</param>
/// <param name="engine">DNN engine type (cv::dnn::EngineType). Default is 2 (ENGINE_NEW).</param>
/// <param name="backend">DNN backend (see <see cref="Dnn.Backend"/>). Default is 0 (DEFAULT).</param>
/// <param name="target">DNN target (see <see cref="Dnn.Target"/>). Default is 0 (CPU).</param>
public static ALIKED Create(
string modelPath, Size? inputSize = null, bool normalizeDescriptors = true,
int engine = 2, int backend = 0, int target = 0)
{
if (modelPath is null)
throw new ArgumentNullException(nameof(modelPath));
var size = inputSize ?? new Size(640, 640);

NativeMethods.HandleException(
NativeMethods.features_ALIKED_create(
modelPath, size, normalizeDescriptors ? 1 : 0, engine, backend, target, out var smartPtr));
NativeMethods.HandleException(
NativeMethods.features_Ptr_Feature2D_get(smartPtr, out var rawPtr));
return new ALIKED(smartPtr, rawPtr);
}

/// <summary>
/// Creates ALIKED from an in-memory ONNX model buffer.
/// </summary>
/// <param name="modelData">Buffer containing the model data.</param>
/// <param name="inputSize">Input image size for the network. Default is 640x640.</param>
/// <param name="normalizeDescriptors">Whether to L2-normalize descriptors. Default is true.</param>
/// <param name="engine">DNN engine type (cv::dnn::EngineType). Default is 2 (ENGINE_NEW).</param>
/// <param name="backend">DNN backend (see <see cref="Dnn.Backend"/>). Default is 0 (DEFAULT).</param>
/// <param name="target">DNN target (see <see cref="Dnn.Target"/>). Default is 0 (CPU).</param>
public static ALIKED CreateFromMemory(
byte[] modelData, Size? inputSize = null, bool normalizeDescriptors = true,
int engine = 2, int backend = 0, int target = 0)
{
if (modelData is null)
throw new ArgumentNullException(nameof(modelData));
var size = inputSize ?? new Size(640, 640);

NativeMethods.HandleException(
NativeMethods.features_ALIKED_create_buffer(
modelData, new IntPtr(modelData.Length), size, normalizeDescriptors ? 1 : 0, engine, backend, target, out var smartPtr));
GC.KeepAlive(modelData);
NativeMethods.HandleException(
NativeMethods.features_Ptr_Feature2D_get(smartPtr, out var rawPtr));
return new ALIKED(smartPtr, rawPtr);
}
}
Loading
Loading