Skip to content

Commit

Permalink
Added KeyPointsFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
shimat committed Aug 19, 2015
1 parent 92de424 commit e5e28a0
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/OpenCvSharp/OpenCvSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
<Compile Include="Cv2\Cv2_video.cs" />
<Compile Include="Cv2\Cv2_superres.cs" />
<Compile Include="Modules\features2d\AgastFeatureDetector.cs" />
<Compile Include="Modules\features2d\KeyPointsFilter.cs" />
<Compile Include="Modules\features2d\Enum\FASTType.cs" />
<Compile Include="Modules\features2d\Enum\AGASTType.cs" />
<Compile Include="Modules\features2d\Enum\AKAZEDescriptorType.cs" />
Expand Down Expand Up @@ -119,6 +120,7 @@
<Compile Include="Modules\ml\Enum\SampleTypes.cs" />
<Compile Include="Modules\ml\ParamGrid.cs" />
<Compile Include="Modules\ml\TrainData.cs" />
<Compile Include="PInvoke\features2d\NativeMethods_features2d_KeyPointsFilter.cs" />
<Compile Include="PInvoke\features2d\NativeMethods_features2d_Feature2D.cs" />
<Compile Include="PInvoke\features2d\NativeMethods_features2d_AgastFeatureDetector.cs" />
<Compile Include="PInvoke\features2d\NativeMethods_features2d_AKAZE.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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 void features2d_KeyPointsFilter_runByImageBorder(
IntPtr keypoints, Size imageSize, int borderSize);

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)]
public static extern void features2d_KeyPointsFilter_runByKeypointSize(
IntPtr keypoints, float minSize, float maxSize);

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)]
public static extern void features2d_KeyPointsFilter_runByPixelsMask(
IntPtr keypoints, IntPtr mask);

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)]
public static extern void features2d_KeyPointsFilter_removeDuplicated(
IntPtr keypoints);

[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl)]
public static extern void features2d_KeyPointsFilter_retainBest(
IntPtr keypoints, int npoints);
}
}
113 changes: 113 additions & 0 deletions src/OpenCvSharp/modules/features2d/KeyPointsFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
using System;
using System.Collections.Generic;

namespace OpenCvSharp
{
// ReSharper disable once InconsistentNaming

/// <summary>
/// A class filters a vector of keypoints.
/// </summary>
public static class KeyPointsFilter
{
/// <summary>
/// Remove keypoints within borderPixels of an image edge.
/// </summary>
/// <param name="keypoints"></param>
/// <param name="imageSize"></param>
/// <param name="borderSize"></param>
/// <returns></returns>
public static KeyPoint[] RunByImageBorder(IEnumerable<KeyPoint> keypoints, Size imageSize, int borderSize)
{
if (keypoints == null)
throw new ArgumentNullException("keypoints");

using (var keypointsVec = new VectorOfKeyPoint(keypoints))
{
NativeMethods.features2d_KeyPointsFilter_runByImageBorder(
keypointsVec.CvPtr, imageSize, borderSize);
return keypointsVec.ToArray();
}
}

/// <summary>
/// Remove keypoints of sizes out of range.
/// </summary>
/// <param name="keypoints"></param>
/// <param name="minSize"></param>
/// <param name="maxSize"></param>
/// <returns></returns>
public static KeyPoint[] RunByKeypointSize(IEnumerable<KeyPoint> keypoints, float minSize,
float maxSize = Single.MaxValue)
{
if (keypoints == null)
throw new ArgumentNullException("keypoints");

using (var keypointsVec = new VectorOfKeyPoint(keypoints))
{
NativeMethods.features2d_KeyPointsFilter_runByKeypointSize(
keypointsVec.CvPtr, minSize, maxSize);
return keypointsVec.ToArray();
}
}

/// <summary>
/// Remove keypoints from some image by mask for pixels of this image.
/// </summary>
/// <param name="keypoints"></param>
/// <param name="mask"></param>
/// <returns></returns>
public static KeyPoint[] RunByPixelsMask(IEnumerable<KeyPoint> keypoints, Mat mask)
{
if (keypoints == null)
throw new ArgumentNullException("keypoints");
if (mask == null)
throw new ArgumentNullException("mask");
mask.ThrowIfDisposed();

using (var keypointsVec = new VectorOfKeyPoint(keypoints))
{
NativeMethods.features2d_KeyPointsFilter_runByPixelsMask(
keypointsVec.CvPtr, mask.CvPtr);
GC.KeepAlive(mask);
return keypointsVec.ToArray();
}
}

/// <summary>
/// Remove duplicated keypoints.
/// </summary>
/// <param name="keypoints"></param>
/// <returns></returns>
public static KeyPoint[] RemoveDuplicated(IEnumerable<KeyPoint> keypoints)
{
if (keypoints == null)
throw new ArgumentNullException("keypoints");

using (var keypointsVec = new VectorOfKeyPoint(keypoints))
{
NativeMethods.features2d_KeyPointsFilter_removeDuplicated(keypointsVec.CvPtr);
return keypointsVec.ToArray();
}
}

/// <summary>
/// Retain the specified number of the best keypoints (according to the response)
/// </summary>
/// <param name="keypoints"></param>
/// <param name="npoints"></param>
/// <returns></returns>
public static KeyPoint[] RetainBest(IEnumerable<KeyPoint> keypoints, int npoints)
{
if (keypoints == null)
throw new ArgumentNullException("keypoints");

using (var keypointsVec = new VectorOfKeyPoint(keypoints))
{
NativeMethods.features2d_KeyPointsFilter_retainBest(
keypointsVec.CvPtr, npoints);
return keypointsVec.ToArray();
}
}
}
}
1 change: 1 addition & 0 deletions src/OpenCvSharpExtern/OpenCvSharpExtern.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@
<ClInclude Include="features2d_FastFeatureDetector.h" />
<ClInclude Include="features2d_GFTTDetector.h" />
<ClInclude Include="features2d_KAZE.h" />
<ClInclude Include="features2d_KeyPointsFilter.h" />
<ClInclude Include="features2d_MSER.h" />
<ClInclude Include="features2d_ORB.h" />
<ClInclude Include="features2d_SimpleBlobDetector.h" />
Expand Down
3 changes: 3 additions & 0 deletions src/OpenCvSharpExtern/OpenCvSharpExtern.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@
<ClInclude Include="features2d_AgastFeatureDetector.h">
<Filter>Header Files\features2d</Filter>
</ClInclude>
<ClInclude Include="features2d_KeyPointsFilter.h">
<Filter>Header Files\features2d</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="Source Files">
Expand Down
36 changes: 36 additions & 0 deletions src/OpenCvSharpExtern/features2d_KeyPointsFilter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef _CPP_FEATURES2D_KAZE_H_
#define _CPP_FEATURES2D_KAZE_H_

#include "include_opencv.h"


CVAPI(void) features2d_KeyPointsFilter_runByImageBorder(
std::vector<cv::KeyPoint> *keypoints, MyCvSize imageSize, int borderSize)
{
cv::KeyPointsFilter::runByImageBorder(*keypoints, cpp(imageSize), borderSize);
}

CVAPI(void) features2d_KeyPointsFilter_runByKeypointSize(
std::vector<cv::KeyPoint> *keypoints, float minSize, float maxSize)
{
cv::KeyPointsFilter::runByKeypointSize(*keypoints, minSize, maxSize);
}
CVAPI(void) features2d_KeyPointsFilter_runByPixelsMask(
std::vector<cv::KeyPoint> *keypoints, cv::Mat *mask)
{
cv::KeyPointsFilter::runByPixelsMask(*keypoints, *mask);
}

CVAPI(void) features2d_KeyPointsFilter_removeDuplicated(
std::vector<cv::KeyPoint> *keypoints)
{
cv::KeyPointsFilter::removeDuplicated(*keypoints);
}

CVAPI(void) features2d_KeyPointsFilter_retainBest(
std::vector<cv::KeyPoint> *keypoints, int npoints)
{
cv::KeyPointsFilter::retainBest(*keypoints, npoints);
}

#endif

0 comments on commit e5e28a0

Please sign in to comment.