Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add findHomography overload (Usac) #1398

Merged
merged 2 commits into from
Mar 13, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
733 changes: 394 additions & 339 deletions src/OpenCvSharp/Cv2/Cv2_calib3d.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,18 @@ static partial class NativeMethods
[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus calib3d_findHomography_InputArray(
IntPtr srcPoints, IntPtr dstPoints,
int method, double ransacReprojThreshold, IntPtr mask,
int method, double ransacReprojThreshold, IntPtr mask,
int maxIters, double confidence,
out IntPtr returnValue);
[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus calib3d_findHomography_vector(
Point2d[] srcPoints, int srcPointsLength,
Point2d[] dstPoints, int dstPointsLength, int method, double ransacReprojThreshold, IntPtr mask,
Point2d[] dstPoints, int dstPointsLength, int method, double ransacReprojThreshold, IntPtr mask,
int maxIters, double confidence,
out IntPtr returnValue);
[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern ExceptionStatus calib3d_findHomography_UsacParams(
IntPtr srcPoints, IntPtr dstPoints, IntPtr mask, ref WUsacParams @params,
out IntPtr returnValue);

[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
Expand Down
36 changes: 35 additions & 1 deletion src/OpenCvSharp/Modules/calib3d/Enum/HomographyMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace OpenCvSharp
/// <summary>
/// The method used to computed homography matrix
/// </summary>
[Flags]
public enum HomographyMethods
{
/// <summary>
Expand All @@ -27,5 +26,40 @@ public enum HomographyMethods
/// RHO algorithm
/// </summary>
Rho = 16,

/// <summary>
/// USAC algorithm, default settings
/// </summary>
USAC_DEFAULT = 32,

/// <summary>
/// USAC, parallel version
/// </summary>
USAC_PARALLEL = 33,

/// <summary>
/// USAC, fundamental matrix 8 points
/// </summary>
USAC_FM_8PTS = 34,

/// <summary>
/// USAC, fast settings
/// </summary>
USAC_FAST = 35,

/// <summary>
/// USAC, accurate settings
/// </summary>
USAC_ACCURATE = 36,

/// <summary>
/// USAC, sorted points, runs PROSAC
/// </summary>
USAC_PROSAC = 37,

/// <summary>
/// USAC, runs MAGSAC++
/// </summary>
USAC_MAGSAC = 38
}
}
87 changes: 87 additions & 0 deletions src/OpenCvSharp/Modules/calib3d/UsacParams.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using System;
using System.Runtime.InteropServices;

namespace OpenCvSharp
{
#pragma warning disable CS1591

public class UsacParams
{
#pragma warning disable CA1805
public double Confidence { get; set; } = 0.99;
public bool IsParallel { get; set; } = false;
public int LoIterations { get; set; } = 5;
public LocalOptimMethod LoMethod { get; set; } = LocalOptimMethod.INNER_LO;
public int LoSampleSize { get; set; } = 14;
public int MaxIterations { get; set; } = 5000;
public NeighborSearchMethod NeighborsSearch { get; set; } = NeighborSearchMethod.GRID;
public int RandomGeneratorState { get; set; } = 0;
public SamplingMethod Sampler { get; set; } = SamplingMethod.UNIFORM;
public ScoreMethod Score { get; set; } = ScoreMethod.MSAC;
public double Threshold { get; set; } = 1.5;

public WUsacParams ToNativeStruct() => new WUsacParams
{
Confidence = Confidence,
IsParallel = IsParallel ? 1 : 0,
LoIterations = LoIterations,
LoMethod = LoMethod,
LoSampleSize = LoSampleSize,
MaxIterations = MaxIterations,
NeighborsSearch = NeighborsSearch,
RandomGeneratorState = RandomGeneratorState,
Sampler = Sampler,
Score = Score,
Threshold = Threshold,
};
}

#pragma warning disable CA1815
[StructLayout(LayoutKind.Sequential)]
public struct WUsacParams
{
public double Confidence;
public int IsParallel;
public int LoIterations;
public LocalOptimMethod LoMethod;
public int LoSampleSize;
public int MaxIterations;
public NeighborSearchMethod NeighborsSearch;
public int RandomGeneratorState;
public SamplingMethod Sampler;
public ScoreMethod Score;
public double Threshold;
}

public enum SamplingMethod : int
{
UNIFORM,
PROGRESSIVE_NAPSAC,
NAPSAC,
PROSAC
}

public enum LocalOptimMethod : int
{
NULL,
INNER_LO,
INNER_AND_ITER_LO,
GC,
SIGMA
}

public enum ScoreMethod : int
{
RANSAC,
MSAC,
MAGSAC,
LMEDS
}

public enum NeighborSearchMethod : int
{
FLANN_KNN,
GRID,
FLANN_RADIUS
}
}
44 changes: 41 additions & 3 deletions src/OpenCvSharpExtern/calib3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@

#include "include_opencv.h"

struct CV_EXPORTS_W_SIMPLE MyUsacParams
{
double confidence;
int isParallel;
int loIterations;
cv::LocalOptimMethod loMethod;
int loSampleSize;
int maxIterations;
cv::NeighborSearchMethod neighborsSearch;
int randomGeneratorState;
cv::SamplingMethod sampler;
cv::ScoreMethod score;
double threshold;
};

CVAPI(ExceptionStatus) calib3d_Rodrigues(cv::_InputArray *src, cv::_OutputArray *dst, cv::_OutputArray *jacobian)
{
BEGIN_WRAP
Expand All @@ -16,24 +31,47 @@ CVAPI(ExceptionStatus) calib3d_Rodrigues(cv::_InputArray *src, cv::_OutputArray
CVAPI(ExceptionStatus) calib3d_findHomography_InputArray(
cv::_InputArray *srcPoints, cv::_InputArray *dstPoints,
int method, double ransacReprojThreshold, cv::_OutputArray *mask,
int maxIters, double confidence,
cv::Mat** returnValue)
{
BEGIN_WRAP
const auto ret = cv::findHomography(*srcPoints, *dstPoints, method, ransacReprojThreshold, entity(mask));
const auto ret = cv::findHomography(*srcPoints, *dstPoints, method, ransacReprojThreshold, entity(mask), maxIters, confidence);
*returnValue = new cv::Mat(ret);
END_WRAP
}
CVAPI(ExceptionStatus) calib3d_findHomography_vector(
cv::Point2d *srcPoints, int srcPointsLength,
cv::Point2d *dstPoints, int dstPointsLength,
int method, double ransacReprojThreshold, cv::_OutputArray *mask,
int method, double ransacReprojThreshold, cv::_OutputArray *mask,
int maxIters, double confidence,
cv::Mat **returnValue)
{
BEGIN_WRAP
const cv::Mat srcPointsMat(srcPointsLength, 1, CV_64FC2, srcPoints);
const cv::Mat dstPointsMat(dstPointsLength, 1, CV_64FC2, dstPoints);

const auto ret = cv::findHomography(srcPointsMat, dstPointsMat, method, ransacReprojThreshold, entity(mask));
const auto ret = cv::findHomography(srcPointsMat, dstPointsMat, method, ransacReprojThreshold, entity(mask), maxIters, confidence);
*returnValue = new cv::Mat(ret);
END_WRAP
}
CVAPI(ExceptionStatus) calib3d_findHomography_UsacParams(
cv::_InputArray* srcPoints, cv::_InputArray* dstPoints, cv::_OutputArray* mask, MyUsacParams *params,
cv::Mat** returnValue)
{
BEGIN_WRAP
cv::UsacParams p;
p.confidence = params->confidence;
p.isParallel = params->isParallel != 0;
p.loIterations = params->loIterations;
p.loMethod = params->loMethod;
p.loSampleSize = params->loSampleSize;
p.maxIterations = params->maxIterations;
p.neighborsSearch = params->neighborsSearch;
p.randomGeneratorState = params->randomGeneratorState;
p.sampler = params->sampler;
p.score = params->score;
p.threshold = params->threshold;
const auto ret = cv::findHomography(*srcPoints, *dstPoints, entity(mask), p);
*returnValue = new cv::Mat(ret);
END_WRAP
}
Expand Down
88 changes: 76 additions & 12 deletions test/OpenCvSharp.Tests/calib3d/Calib3dTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void Rodrigues()
Assert.Equal(3, matrix2.GetLength(0));
Assert.Equal(3, matrix2.GetLength(1));
for (var i = 0; i < matrix2.GetLength(0); i++)
for(var j = 0; j < matrix2.GetLength(1); j++)
for (var j = 0; j < matrix2.GetLength(1); j++)
Assert.Equal(matrix[i, j], matrix2[i, j], 3);
}

Expand All @@ -75,7 +75,7 @@ public void FindChessboardCorners()
using var image = Image("calibration/00.jpg");
using var corners = new Mat();
bool found = Cv2.FindChessboardCorners(image, patternSize, corners);

if (Debugger.IsAttached)
{
Cv2.DrawChessboardCorners(image, patternSize, corners, found);
Expand Down Expand Up @@ -126,11 +126,11 @@ public void CalibrateCameraByArray()

var objectPoints = Create3DChessboardCorners(patternSize, 1.0f);
var imagePoints = corners.ToArray();
var cameraMatrix = new double[,] {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
var cameraMatrix = new double[,] { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } };
var distCoeffs = new double[5];

var rms = Cv2.CalibrateCamera(new []{objectPoints}, new[]{imagePoints}, image.Size(), cameraMatrix,
distCoeffs, out var rotationVectors, out var translationVectors,
var rms = Cv2.CalibrateCamera(new[] { objectPoints }, new[] { imagePoints }, image.Size(), cameraMatrix,
distCoeffs, out var rotationVectors, out var translationVectors,
CalibrationFlags.UseIntrinsicGuess | CalibrationFlags.FixK5);

Assert.Equal(6.16, rms, 2);
Expand Down Expand Up @@ -161,7 +161,7 @@ public void CalibrateCameraByMat()
Assert.Equal(6.16, rms, 2);
Assert.Contains(distCoeffValues, d => Math.Abs(d) > 1e-20);
}

[Fact]
public void FishEyeCalibrate()
{
Expand Down Expand Up @@ -219,7 +219,7 @@ public void ProjectPoints()
tVec.Set<double>(1, -7.6847683212704716e+000);
tVec.Set<double>(2, 2.6169795190294256e+001);

using var distCoeffs = new Mat(4, 1, MatType.CV_64FC1);
using var distCoeffs = new Mat(4, 1, MatType.CV_64FC1);
distCoeffs.Set<double>(0, 0);
distCoeffs.Set<double>(1, 0);
distCoeffs.Set<double>(2, 0);
Expand Down Expand Up @@ -293,7 +293,7 @@ public void SolvePnPTestByArray()
};
var dist = new double[] { 0, 0, 0, 0, 0 };

var objPts = new []
var objPts = new[]
{
new Point3f(0,0,1),
new Point3f(1,0,1),
Expand All @@ -307,7 +307,7 @@ public void SolvePnPTestByArray()

Cv2.SolvePnP(objPts, imgPts, cameraMatrix, dist, ref rvec, ref tvec);
}

[Fact]
public void SolvePnPTestByMat()
{
Expand All @@ -321,7 +321,7 @@ public void SolvePnPTestByMat()
};
var dist = new double[] { 0, 0, 0, 0, 0 };

var objPts = new []
var objPts = new[]
{
new Point3f(0,0,1),
new Point3f(1,0,1),
Expand Down Expand Up @@ -371,7 +371,7 @@ public void FindFundamentalMat()
using Mat f = Cv2.FindFundamentalMat(imgPt1, imgPt2, FundamentalMatMethods.Point8);
Assert.True(f.Empty()); // TODO
}

// https://github.com/shimat/opencvsharp/issues/1069
[Fact]
public void RecoverPose()
Expand Down Expand Up @@ -424,7 +424,71 @@ public void RecoverPose()
Assert.False(r.Empty());
Assert.False(t.Empty());
}


[Fact]
public void FindHomography()
{
var points1 = new Point2f[]
{
new(10, 20),
new(20, 30),
new(30, 40),
new(40, 50),
new(50, 60),
};
var points2 = new Point2f[]
{
new(11, 22),
new(22, 33),
new(33, 44),
new(44, 55),
new(55, 66),
};

using var m1 = Mat.FromArray(points1);
using var m2 = Mat.FromArray(points2);

using var dst = Cv2.FindHomography(m1, m2);

Assert.False(dst.Empty());
Assert.Equal(3, dst.Rows);
Assert.Equal(3, dst.Cols);
Assert.True(dst.GetArray(out double[] dstArray));
Assert.Equal(9, dstArray.Length);
Assert.All(dstArray, d =>
{
Assert.False(double.IsNaN(d));
Assert.False(double.IsInfinity(d));
});
}

[Fact]
public void FindHomographyUsac()
{
var points1 = Enumerable.Range(1, 5).Select(i => new Point2f(i * 10, i * 20)).ToArray();
var points2 = points1.Select(p => new Point2f(p.X + p.X / 10, p.Y + p.Y / 10)).ToArray();

using var m1 = Mat.FromArray(points1);
using var m2 = Mat.FromArray(points2);
using var mask = new Mat();
var usacParams = new UsacParams();

using var dst = Cv2.FindHomography(m1, m2, mask, usacParams);

// TODO
/*
Assert.False(dst.Empty());
Assert.Equal(3, dst.Rows);
Assert.Equal(3, dst.Cols);
Assert.True(dst.GetArray(out double[] dstArray));
Assert.Equal(9, dstArray.Length);
Assert.All(dstArray, d =>
{
Assert.False(double.IsNaN(d));
Assert.False(double.IsInfinity(d));
});*/
}

private static IEnumerable<Point3f> Create3DChessboardCorners(Size boardSize, float squareSize)
{
for (int y = 0; y < boardSize.Height; y++)
Expand Down