Skip to content

Commit

Permalink
Merge pull request #1149 from shimat/FeaturesMatcher
Browse files Browse the repository at this point in the history
Add FeaturesMatcher
  • Loading branch information
shimat authored Jan 3, 2021
2 parents 999db67 + 637964e commit 4d2b76b
Show file tree
Hide file tree
Showing 19 changed files with 1,089 additions and 211 deletions.
2 changes: 2 additions & 0 deletions OpenCvSharp.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=imshow/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=inlier/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=inliers/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=inliners/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=inpaint/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=inpainted/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=inpainting/@EntryIndexedValue">True</s:Boolean>
Expand Down Expand Up @@ -127,6 +128,7 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=trackbar/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=uchar/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Undistort/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Vecs/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=videoio/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Voronoi/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Writeable/@EntryIndexedValue">True</s:Boolean>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Diagnostics.Contracts;
using System.Runtime.InteropServices;
using OpenCvSharp.Detail;

#pragma warning disable 1591
#pragma warning disable CA1401 // P/Invokes should not be visible
Expand All @@ -23,7 +24,7 @@ static partial class NativeMethods
[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern IntPtr vector_uchar_getPointer(IntPtr vector);
[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void vector_vector_uchar_copy(IntPtr vector, IntPtr dst);
public static extern void vector_uchar_copy(IntPtr vector, IntPtr dst);
[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void vector_uchar_delete(IntPtr vector);
#endregion
Expand Down Expand Up @@ -289,6 +290,25 @@ static partial class NativeMethods
[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void vector_DTrees_Split_delete(IntPtr vector);

#endregion
#region cv::detail::ImageFeatures

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern IntPtr vector_ImageFeatures_new1();

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern nuint vector_ImageFeatures_getSize(IntPtr vector);

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void vector_ImageFeatures_getKeypointsSize(
IntPtr vector, [Out] nuint[] dst);

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void vector_ImageFeatures_getElements(IntPtr vector, [Out] WImageFeatures[] dst);

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void vector_ImageFeatures_delete(IntPtr vector);

#endregion
#region cv::line_descriptor::KeyLine
#if false
Expand All @@ -309,6 +329,18 @@ static partial class NativeMethods
#endif
#endregion

#region vector<uchar>
[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern IntPtr vector_vector_uchar_new1();
[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern nuint vector_vector_uchar_getSize1(IntPtr vector);
[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void vector_vector_uchar_getSize2(IntPtr vector, [In, Out] nuint[] size);
[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void vector_vector_uchar_copy(IntPtr vec, IntPtr[] dst);
[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void vector_vector_uchar_delete(IntPtr vector);
#endregion
#region vector<int>
[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern IntPtr vector_vector_int_new1();
Expand Down Expand Up @@ -403,8 +435,7 @@ public static extern void vector_string_getElements(
[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void vector_string_delete(IntPtr vector);
#endregion
#region vector<cv::line_descriptor::KeyLine>

#region vector<cv::line_descriptor::KeyLine>
#if false
[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern IntPtr vector_vector_KeyLine_new1();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,81 @@ namespace OpenCvSharp.Internal
static partial class NativeMethods
{
[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus stitching_computeImageFeatures1_1(
public static extern ExceptionStatus stitching_computeImageFeatures1(
IntPtr featuresFinder,
IntPtr[] images,
int imagesLength,
IntPtr[] features,
IntPtr[] masks);

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus stitching_computeImageFeatures1_2(
IntPtr featuresFinder,
IntPtr[] images,
int imagesLength,
IntPtr[] features,
IntPtr masks);
IntPtr featuresVec,
IntPtr[]? masks);

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern unsafe ExceptionStatus stitching_computeImageFeatures2(
IntPtr featuresFinder,
IntPtr image,
WImageFeatures* features,
IntPtr mask);


// FeaturesMatcher

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus stitching_FeaturesMatcher_apply(
IntPtr obj,
ref WImageFeatures features1,
ref WImageFeatures features2,
out int outSrcImgIdx,
out int outDstImgIdx,
IntPtr outMatches,
IntPtr outInliersMask,
out int outNumInliers,
IntPtr outH,
out double outConfidence);

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus stitching_FeaturesMatcher_apply2(
IntPtr obj,
WImageFeatures[] features, int featuresSize,
IntPtr mask,
IntPtr outSrcImgIdx,
IntPtr outDstImgIdx,
IntPtr outMatches,
IntPtr outInliersMask,
IntPtr outNumInliers,
IntPtr outH,
IntPtr outConfidence);

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

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


// BestOf2NearestMatcher

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus stitching_BestOf2NearestMatcher_new(
int tryUseGpu, float matchConf, int numMatchesThresh1, int numMatchesThresh2,
out IntPtr returnValue);

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

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


// AffineBestOf2NearestMatcher

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus stitching_AffineBestOf2NearestMatcher_new(
int fullAffine, int tryUseGpu, float matchConf, int numMatchesThresh1,
out IntPtr returnValue);

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus stitching_AffineBestOf2NearestMatcher_delete(
IntPtr obj);
}
}
114 changes: 114 additions & 0 deletions src/OpenCvSharp/Internal/Vectors/VectorOfImageFeatures.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using System;
using System.Linq;
using OpenCvSharp.Detail;

namespace OpenCvSharp.Internal.Vectors
{
/// <summary>
/// </summary>
public class VectorOfImageFeatures : DisposableCvObject, IStdVector<ImageFeatures>
{
/// <summary>
/// Constructor
/// </summary>
public VectorOfImageFeatures()
{
ptr = NativeMethods.vector_ImageFeatures_new1();
}

/// <summary>
/// Releases unmanaged resources
/// </summary>
protected override void DisposeUnmanaged()
{
NativeMethods.vector_ImageFeatures_delete(ptr);
base.DisposeUnmanaged();
}

/// <summary>
/// vector.size()
/// </summary>
public int Size
{
get
{
var res = NativeMethods.vector_ImageFeatures_getSize(ptr);
GC.KeepAlive(this);
return (int)res;
}
}

/// <summary>
/// Converts std::vector to managed array
/// </summary>
/// <returns></returns>
public ImageFeatures[] ToArray()
{
var size = Size;
if (size == 0)
return Array.Empty<ImageFeatures>();

VectorOfKeyPoint[]? keypointsVecs = null;
Mat[]? descriptors = null;
try
{
var nativeResult = new WImageFeatures[size];
keypointsVecs = new VectorOfKeyPoint[size];
descriptors = new Mat[size];
for (int i = 0; i < size; i++)
{
keypointsVecs[i] = new VectorOfKeyPoint();
descriptors[i] = new Mat();
nativeResult[i].Keypoints = keypointsVecs[i].CvPtr;
nativeResult[i].Descriptors = descriptors[i].CvPtr;
}

NativeMethods.vector_ImageFeatures_getElements(ptr, nativeResult);

var result = new ImageFeatures[size];
for (int i = 0; i < size; i++)
{
result[i] = new ImageFeatures(
imgIdx: nativeResult[i].ImgIdx,
imgSize: nativeResult[i].ImgSize,
keypoints: keypointsVecs[i].ToArray(),
descriptors: descriptors[i]);
}

// ElemPtr is IntPtr to memory held by this object, so make sure we are not disposed until finished with copy.
GC.KeepAlive(this);
return result;
}
catch
{
if (descriptors != null)
{
foreach (var mat in descriptors)
{
mat.Dispose();
}
}

throw;
}
finally
{
if (keypointsVecs != null)
{
foreach (var vec in keypointsVecs)
{
vec.Dispose();
}
}
}
}

private int[] KeypointsSizes(int size)
{
var ret = new nuint[size];
NativeMethods.vector_ImageFeatures_getKeypointsSize(ptr, ret);
GC.KeepAlive(this);
return ret.Select(v => (int)v).ToArray();
}
}
}
79 changes: 79 additions & 0 deletions src/OpenCvSharp/Internal/Vectors/VectorOfVectorByte.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;
using System.Linq;
using OpenCvSharp.Internal.Util;

namespace OpenCvSharp.Internal.Vectors
{
/// <summary>
/// </summary>
public class VectorOfVectorByte : DisposableCvObject, IStdVector<byte[]>
{
/// <summary>
/// Constructor
/// </summary>
public VectorOfVectorByte()
{
ptr = NativeMethods.vector_vector_uchar_new1();
}

/// <summary>
/// Releases unmanaged resources
/// </summary>
protected override void DisposeUnmanaged()
{
NativeMethods.vector_vector_uchar_delete(ptr);
base.DisposeUnmanaged();
}

/// <summary>
/// vector.size()
/// </summary>
public int GetSize1()
{
var res = NativeMethods.vector_vector_uchar_getSize1(ptr);
GC.KeepAlive(this);
return (int)res;
}

/// <summary>
/// vector.size()
/// </summary>
public int Size => GetSize1();

/// <summary>
/// vector[i].size()
/// </summary>
public IReadOnlyList<long> GetSize2()
{
var size1 = GetSize1();
var size2 = new nuint[size1];
NativeMethods.vector_vector_uchar_getSize2(ptr, size2);
GC.KeepAlive(this);
return size2.Select(s => (long)s).ToArray();
}

/// <summary>
/// Converts std::vector to managed array
/// </summary>
/// <returns></returns>
public byte[][] ToArray()
{
var size1 = GetSize1();
if (size1 == 0)
return Array.Empty<byte[]>();
var size2 = GetSize2();

var ret = new byte[size1][];
for (var i = 0; i < size1; i++)
{
ret[i] = new byte[size2[i]];
}

using var retPtr = new ArrayAddress2<byte>(ret);
NativeMethods.vector_vector_uchar_copy(ptr, retPtr.GetPointer());
GC.KeepAlive(this);
return ret;
}
}
}
Loading

0 comments on commit 4d2b76b

Please sign in to comment.