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

SolvePnP overload with non-Mat arguments throws Exception #387

Closed
BenjaminPelletier opened this issue Sep 19, 2017 · 1 comment
Closed

Comments

@BenjaminPelletier
Copy link

Summary of your issue

The SolvePnP overload that uses IEnumerable sets of points throws the Exception shown below. Apparently I'm not the first person to discover this problem: https://stackoverflow.com/questions/37279776/how-to-fix-unhandled-opencvexception-from-calling-cv2-solvepnp

Environment

Visual Studio 2015
OpenCVSharp3-AnyCPU v3.2.0.20170911

What did you do when you faced the problem?

Invoked Cv2.SolvePnP

Example code:

double[] rvec;
double[] tvec;
Console.WriteLine("objPts: " + objPts.GetType().Name + " (" + objPts.Length + " points)");
Console.WriteLine("imgPts: " + imgPts.GetType().Name + " (" + imgPts.Length + " points)");
Console.WriteLine("mat: " + mat.GetType().Name + " " + mat.GetLength(0) + "x" + mat.GetLength(1) + " (" + mat.Length + " elements)");
Console.WriteLine("dist: " + dist.GetType().Name + " (" + dist.Length + " elements)");
Cv2.SolvePnP(objPts, imgPts, intrinsics.Mat, intrinsics.Dist, out rvec, out tvec);

Output:

objPts: Point3f[] (5 points)
imgPts: Point2f[] (5 points)
mat: Double[,] 3x3 (9 elements)
dist: Double[] (5 elements)
OpenCvSharp.OpenCVException: src.size == dst.size && src.channels() == dst.channels()
   at OpenCvSharp.NativeMethods.<>c.<.cctor>b__1555_0(ErrorCode status, String funcName, String errMsg, String fileName, Int32 line, IntPtr userdata)
   at OpenCvSharp.NativeMethods.calib3d_solvePnP_vector(Point3f[] objectPoints, Int32 objectPointsLength, Point2f[] imagePoints, Int32 imagePointsLength, Double[,] cameraMatrix, Double[] distCoeffs, Int32 distCoeffsLength, Double[] rvec, Double[] tvec, Int32 useExtrinsicGuess, Int32 flags)
   at OpenCvSharp.Cv2.SolvePnP(IEnumerable`1 objectPoints, IEnumerable`1 imagePoints, Double[,] cameraMatrix, IEnumerable`1 distCoeffs, Double[]& rvec, Double[]& tvec, Boolean useExtrinsicGuess, SolvePnPFlags flags)

What did you intend to be?

I intended for SolvePnP to work properly given valid inputs.

@BenjaminPelletier
Copy link
Author

Here's complete code to reproduce:

var rvec = new double[] { 0, 0, 0 };
var tvec = new double[] { 0, 0, 0 };
var m = new double[3, 3]
{
    { 1, 0, 0 },
    { 0, 1, 0 },
    { 0, 0, 1 }
};
var dist = new double[] { 0, 0, 0, 0, 0 };

var objPts = new Point3f[]
{
    new Point3f(0,0,1),
    new Point3f(1,0,1),
    new Point3f(0,1,1),
    new Point3f(1,1,1),
    new Point3f(1,0,2),
    new Point3f(0,1,2)
};

double[,] jacobian;
Point2f[] imgPts;
Cv2.ProjectPoints(objPts, rvec, tvec, m, dist, out imgPts, out jacobian);

Cv2.SolvePnP(objPts, imgPts, m, dist, out rvec, out tvec);

The last line throws the Exception.

@shimat shimat closed this as completed in 7b86104 Nov 10, 2017
shimat added a commit that referenced this issue Nov 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant