I’m using Emgu CV (OpenCV C# wrapper) and trying to project 3D points onto a 2D image plane using CvInvoke.ProjectPoints
. However, I’m getting this OpenCV error:
OpenCV: d == 2 && (sizes[0] == 1 || sizes[1] == 1 || sizes[0]*sizes[1] == 0)
I’ve carefully verified the shape and type of all inputs. Here’s my full setup:
Setup:
Emgu.CV version: 4.11.0
Language: C#
Platform: .NET (WinForms)
My code: (csharp)
// Assume i is the current camera index
Mat rVec = new Mat();
Mat tVec = new Mat();
// Intrinsic parameters (from calibration)
Matrix<double> camMatrix = K[i]; // 3x3, Cv64F
Matrix<double> distCoeffs = D[i]; // 1x4, Cv64F
// 3D points in meters
var coords = new List<float[]>
{
new float[] { 0.1f, 0.2f, 0.5f },
new float[] { 0.2f, 0.1f, 0.5f },
new float[] { 0.3f, 0.1f, 0.5f },
new float[] { 0.4f, 0.1f, 0.5f },
new float[] { 0.5f, 0.1f, 0.5f },
new float[] { 0.6f, 0.1f, 0.5f }
};
var coordsArray = coords.Select(p => new MCvPoint3D32f(p[0], p[1], p[2])).ToArray();
// Corresponding 2D image points
List<PointF> pointsF = new List<PointF>
{
new PointF(1234f, 567f),
new PointF(1250f, 570f),
new PointF(1270f, 575f),
new PointF(1290f, 580f),
new PointF(1305f, 583f),
new PointF(1320f, 586f)
};
// SolvePnP
CvInvoke.SolvePnP(
coordsArray,
pointsF.ToArray(),
camMatrix,
distCoeffs,
rVec,
tVec
);
// Convert translation to meters if needed
// tVec.ConvertTo(tVec, tVec.Depth, 1.0 / 1000.0); // optional depending on units
// Project 3D points
var proj_obj = new Emgu.CV.Util.VectorOfPointF();
CvInvoke.ProjectPoints(
coordsArray,
rVec,
tVec,
camMatrix,
distCoeffs,
proj_obj // this throws the error
);
Inputs (confirmed): coordsArray.Length == 6
rVec: 3x1, type Cv64F, 1 channel
tVec: 3x1, type Cv64F, 1 channel
camMatrix: 3x3, type Cv64F, 1 channel e.g.:
[2416, 0, 960]
[ 0,2416, 540]
[ 0, 0, 1]
distCoeffs: 1x4, type Cv64F, 1 channel
Problem: Even with all input dimensions and types seemingly correct, I still get this error from ProjectPoints.
Question: What could cause CvInvoke.ProjectPoints in Emgu CV to throw this OpenCV error, even when all dimensions seem correct? Are there any edge cases or known issues I might be missing?
Tried: Verified dimensions of all inputs.
Replaced 0 Z values with small epsilon like 0.0001f.
Tried flattening tVec, or converting to different shapes — no luck.
Ensured all coordinate units are consistent (meters or millimeters).
Any help is greatly appreciated!