-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1149 from shimat/FeaturesMatcher
Add FeaturesMatcher
- Loading branch information
Showing
19 changed files
with
1,089 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
src/OpenCvSharp/Internal/Vectors/VectorOfImageFeatures.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
Oops, something went wrong.