Skip to content

Commit fec3a56

Browse files
committed
fix RecoverPose
1 parent 0668902 commit fec3a56

File tree

6 files changed

+152
-16
lines changed

6 files changed

+152
-16
lines changed

src/OpenCvSharp/Cv2/Cv2_calib3d.cs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2575,6 +2575,47 @@ public static Mat FindFundamentalMat(
25752575
return new Mat(ret);
25762576
}
25772577

2578+
/// <summary>
2579+
/// Calculates a fundamental matrix from the corresponding points in two images.
2580+
/// </summary>
2581+
/// <param name="points1">Array of N points from the first image.
2582+
/// The point coordinates should be floating-point (single or double precision).</param>
2583+
/// <param name="points2">Array of the second image points of the same size and format as points1 .</param>
2584+
/// <param name="method">Method for computing a fundamental matrix.</param>
2585+
/// <param name="param1">Parameter used for RANSAC.
2586+
/// It is the maximum distance from a point to an epipolar line in pixels, beyond which the point is
2587+
/// considered an outlier and is not used for computing the final fundamental matrix. It can be set to
2588+
/// something like 1-3, depending on the accuracy of the point localization, image resolution, and the image noise.</param>
2589+
/// <param name="param2">Parameter used for the RANSAC or LMedS methods only.
2590+
/// It specifies a desirable level of confidence (probability) that the estimated matrix is correct.</param>
2591+
/// <param name="mask">Output array of N elements, every element of which is set to 0 for outliers and
2592+
/// to 1 for the other points. The array is computed only in the RANSAC and LMedS methods. For other methods, it is set to all 1’s.</param>
2593+
/// <returns>fundamental matrix</returns>
2594+
public static Mat FindFundamentalMat(
2595+
IEnumerable<Point2f> points1,
2596+
IEnumerable<Point2f> points2,
2597+
FundamentalMatMethod method = FundamentalMatMethod.Ransac,
2598+
double param1 = 3.0,
2599+
double param2 = 0.99,
2600+
OutputArray? mask = null)
2601+
{
2602+
if (points1 == null)
2603+
throw new ArgumentNullException(nameof(points1));
2604+
if (points2 == null)
2605+
throw new ArgumentNullException(nameof(points2));
2606+
2607+
var points1Array = points1 as Point2f[] ?? points1.ToArray();
2608+
var points2Array = points2 as Point2f[] ?? points2.ToArray();
2609+
2610+
NativeMethods.HandleException(
2611+
NativeMethods.calib3d_findFundamentalMat_arrayF32(
2612+
points1Array, points1Array.Length,
2613+
points2Array, points2Array.Length, (int) method,
2614+
param1, param2, ToPtr(mask), out var ret));
2615+
mask?.Fix();
2616+
return new Mat(ret);
2617+
}
2618+
25782619
/// <summary>
25792620
/// Calculates a fundamental matrix from the corresponding points in two images.
25802621
/// </summary>
@@ -2608,7 +2649,7 @@ public static Mat FindFundamentalMat(
26082649
var points2Array = points2 as Point2d[] ?? points2.ToArray();
26092650

26102651
NativeMethods.HandleException(
2611-
NativeMethods.calib3d_findFundamentalMat_array(
2652+
NativeMethods.calib3d_findFundamentalMat_arrayF64(
26122653
points1Array, points1Array.Length,
26132654
points2Array, points2Array.Length, (int) method,
26142655
param1, param2, ToPtr(mask), out var ret));

src/OpenCvSharp/PInvoke/NativeMethods/calib3d/NativeMethods_calib3d.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,11 +321,17 @@ public static extern ExceptionStatus calib3d_findFundamentalMat_InputArray(
321321
int method, double param1, double param2, IntPtr mask,
322322
out IntPtr returnValue);
323323
[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
324-
public static extern ExceptionStatus calib3d_findFundamentalMat_array(
324+
public static extern ExceptionStatus calib3d_findFundamentalMat_arrayF64(
325325
Point2d[] points1, int points1Size,
326326
Point2d[] points2, int points2Size,
327327
int method, double param1, double param2, IntPtr mask,
328328
out IntPtr returnValue);
329+
[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
330+
public static extern ExceptionStatus calib3d_findFundamentalMat_arrayF32(
331+
Point2f[] points1, int points1Size,
332+
Point2f[] points2, int points2Size,
333+
int method, double param1, double param2, IntPtr mask,
334+
out IntPtr returnValue);
329335

330336
[Pure, DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
331337
public static extern ExceptionStatus calib3d_computeCorrespondEpilines_InputArray(

src/OpenCvSharpExtern/calib3d.h

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ CVAPI(ExceptionStatus) calib3d_findFundamentalMat_InputArray(
763763
*returnValue = new cv::Mat(mat);
764764
END_WRAP
765765
}
766-
CVAPI(ExceptionStatus) calib3d_findFundamentalMat_array(
766+
CVAPI(ExceptionStatus) calib3d_findFundamentalMat_arrayF64(
767767
cv::Point2d *points1, int points1Size,
768768
cv::Point2d *points2, int points2Size,
769769
int method, double param1, double param2,
@@ -778,6 +778,21 @@ CVAPI(ExceptionStatus) calib3d_findFundamentalMat_array(
778778
*returnValue = new cv::Mat(mat);
779779
END_WRAP
780780
}
781+
CVAPI(ExceptionStatus) calib3d_findFundamentalMat_arrayF32(
782+
cv::Point2f *points1, int points1Size,
783+
cv::Point2f *points2, int points2Size,
784+
int method, double param1, double param2,
785+
cv::_OutputArray *mask,
786+
cv::Mat **returnValue)
787+
{
788+
BEGIN_WRAP
789+
const cv::Mat points1M(points1Size, 1, CV_32FC2, points1);
790+
const cv::Mat points2M(points2Size, 1, CV_32FC2, points2);
791+
const auto mat = cv::findFundamentalMat(
792+
points1M, points2M, method, param1, param2, entity(mask));
793+
*returnValue = new cv::Mat(mat);
794+
END_WRAP
795+
}
781796

782797
CVAPI(ExceptionStatus) calib3d_computeCorrespondEpilines_InputArray(
783798
cv::_InputArray *points,
@@ -1059,7 +1074,7 @@ CVAPI(ExceptionStatus) calib3d_recoverPose_InputArray1(
10591074
int *returnValue)
10601075
{
10611076
BEGIN_WRAP
1062-
*returnValue = cv::recoverPose(*E, *points1, *points2, *cameraMatrix, *R, *t, *mask);
1077+
*returnValue = cv::recoverPose(*E, *points1, *points2, *cameraMatrix, *R, *t, entity(mask));
10631078
END_WRAP
10641079
}
10651080

@@ -1070,7 +1085,7 @@ CVAPI(ExceptionStatus) calib3d_recoverPose_InputArray2(
10701085
int *returnValue)
10711086
{
10721087
BEGIN_WRAP
1073-
*returnValue = cv::recoverPose(*E, *points1, *points2, *R, *t, focal, cpp(pp), *mask);
1088+
*returnValue = cv::recoverPose(*E, *points1, *points2, *R, *t, focal, cpp(pp), entity(mask));
10741089
END_WRAP
10751090
}
10761091

@@ -1081,7 +1096,7 @@ CVAPI(ExceptionStatus) calib3d_recoverPose_InputArray3(
10811096
int *returnValue)
10821097
{
10831098
BEGIN_WRAP
1084-
*returnValue = cv::recoverPose(*E, *points1, *points2, *cameraMatrix, *R, *t, distanceTresh, *mask, *triangulatedPoints);
1099+
*returnValue = cv::recoverPose(*E, *points1, *points2, *cameraMatrix, *R, *t, distanceTresh, entity(mask), entity(triangulatedPoints));
10851100
END_WRAP
10861101
}
10871102

test/OpenCvSharp.Tests/OpenCvSharp.Tests.csproj

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,6 @@
6565
<PackageReference Include="Xunit.StaFact" Version="1.0.37" />
6666
</ItemGroup>
6767

68-
<ItemGroup>
69-
<None Update="OpenCvSharpExtern.dll">
70-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
71-
</None>
72-
<None Update="opencv_videoio_ffmpeg440_64.dll">
73-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
74-
</None>
75-
</ItemGroup>
76-
7768
<PropertyGroup Condition=" '$(TargetFramework)' == 'net48' ">
7869
<DefineConstants>$(DefineConstants);DOTNET_FRAMEWORK;</DefineConstants>
7970
</PropertyGroup>

test/OpenCvSharp.Tests/calib3d/Calib3dTest.cs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,90 @@ public void SolvePnPTestByMat()
359359
Cv2.SolvePnP(objPtsMat, imgPtsMat, cameraMatrixMat, distMat, rvecMat, tvecMat);
360360
}
361361
}
362+
363+
[Fact]
364+
public void FindFundamentalMat()
365+
{
366+
var imgPt1 = new[]
367+
{
368+
new Point2d(1017.0883, 848.23529),
369+
new Point2d(1637, 848.23529),
370+
new Point2d(1637, 1648.7059),
371+
new Point2d(1017.0883, 1648.7059),
372+
new Point2d(2282.2144, 772),
373+
new Point2d(3034.9644, 772),
374+
new Point2d(3034.9644, 1744),
375+
new Point2d(2282.2144, 1744),
376+
};
377+
var imgPt2 = new[]
378+
{
379+
new Point2d(414.88824, 848.23529),
380+
new Point2d(1034.8, 848.23529),
381+
new Point2d(1034.8, 1648.7059),
382+
new Point2d(414.88824, 1648.7059),
383+
new Point2d(1550.9714, 772),
384+
new Point2d(2303.7214, 772),
385+
new Point2d(2303.7214, 1744),
386+
new Point2d(1550.9714, 1744),
387+
};
388+
389+
using Mat f = Cv2.FindFundamentalMat(imgPt1, imgPt2, FundamentalMatMethod.Point8);
390+
Assert.True(f.Empty()); // TODO
391+
}
362392

393+
// https://github.com/shimat/opencvsharp/issues/1069
394+
[Fact]
395+
public void RecoverPose()
396+
{
397+
var essentialData = new double[,]
398+
{
399+
{1.503247056657373e-16, -7.074103796034695e-16, -7.781514175638166e-16},
400+
{6.720398606232961e-16, -6.189840821530359e-17, -0.7071067811865476},
401+
{7.781514175638166e-16, 0.7071067811865475, -2.033804841359975e-16}
402+
};
403+
using var essential = Mat.FromArray(essentialData);
404+
405+
var p1Data = new[]
406+
{
407+
new Point2d(1017.0883, 848.23529),
408+
new Point2d(1637, 848.23529),
409+
new Point2d(1637, 1648.7059),
410+
new Point2d(1017.0883, 1648.7059),
411+
new Point2d(2282.2144, 772),
412+
new Point2d(3034.9644, 772),
413+
new Point2d(3034.9644, 1744),
414+
new Point2d(2282.2144, 1744)
415+
};
416+
var p2Data = new[]
417+
{
418+
new Point2d(414.88824, 848.23529),
419+
new Point2d(1034.8, 848.23529),
420+
new Point2d(1034.8, 1648.7059),
421+
new Point2d(414.88824, 1648.7059),
422+
new Point2d(1550.9714, 772),
423+
new Point2d(2303.7214, 772),
424+
new Point2d(2303.7214, 1744),
425+
new Point2d(1550.9714, 1744)
426+
};
427+
using var p1 = Mat.FromArray(p1Data);
428+
using var p2 = Mat.FromArray(p2Data);
429+
430+
var kData = new double[,]
431+
{
432+
{3011, 0, 1637},
433+
{0, 3024, 1204},
434+
{0, 0, 1}
435+
};
436+
using var k = Mat.FromArray(kData);
437+
438+
using var r = new Mat();
439+
using var t = new Mat();
440+
Cv2.RecoverPose(essential, p1 , p2 , k, r, t);
441+
442+
Assert.False(r.Empty());
443+
Assert.False(t.Empty());
444+
}
445+
363446
private static IEnumerable<Point3f> Create3DChessboardCorners(Size boardSize, float squareSize)
364447
{
365448
for (int y = 0; y < boardSize.Height; y++)

test/OpenCvSharp.Tests/text/DetectTextSWTTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using OpenCvSharp.Text;
22
using Xunit;
33

4-
namespace OpenCvSharp.Tests.text
4+
namespace OpenCvSharp.Tests.Text
55
{
66
public class DetectTextSWTTest : TestBase
77
{

0 commit comments

Comments
 (0)