我正在为OpenCV使用EmguCV包装器,我正试图通过基本矩阵进行未经校准的整流。
我已经从两个摄像头找到了具有FindChessboardCorners功能的图像点,然后我想找到基本矩阵,但我有问题将参数传递给函数eCvInvoke.cvFindFundamentalMat() CvInvoke.cvFindFundamentalMat()
困扰我的是2D点数组,只是无法获得正确的格式将其传递给OpenCv函数cvFindFundamentalMat。
据我所知,EmguCV / OpenCV openCv需要CvMat类型的一维数组,通过IntPtr ... 但即使我这样做,OpenCV也会抛出一条错误消息“OpenCV:通道数或列数或行数必须= 1”
PointsF[] points1 = Camera1.Points;
PointF[] points2 = Camera2.Points
Matrix<float> points1 = new Matrix<float> (1, Camera1.ImagePoints[0].Length*2, 1);
for (int i =0; i < Camera1.ImagePoints[0].Length-1; i+=2)
{
points1[0, i] = Camera1.ImagePoints[0][i].X;
points1[0, i+1] = Camera1.ImagePoints[0][i].Y;
}
Matrix<float> points2= new Matrix<float>(1, Camera2.ImagePoints[0].Length * 2, 1);
for (int i = 0; i < Camera2.ImagePoints[0].Length-1; i+=2)
{
points1[0, i] = Camera2.ImagePoints[0][i].X;
points1[0, i+1] = Camera2.ImagePoints[0][i].Y;
}
IntPtr points1Matrix = Marshal.AllocHGlobal(StructSize.MCvMat);
GCHandle handlePoints1Ptr = GCHandle.Alloc(points1.MCvMat, GCHandleType.Pinned);
points1Matrix = handlePoints1Ptr.AddrOfPinnedObject();
IntPtr points2Matrix = Marshal.AllocHGlobal(StructSize.MCvMat);
GCHandle handlePoints2Ptr = GCHandle.Alloc(points2.MCvMat, GCHandleType.Pinned);
points2Matrix = handlePoints2Ptr.AddrOfPinnedObject();
_fundamentalMatrix = new Matrix<double>(3, 3, 1);
CvInvoke.cvFindFundamentalMat(points1Matrix, points2Matrix, _fundamentalMatrix.Ptr, CV_FM.CV_FM_RANSAC, 1.0, 0.99, IntPtr.Zero);
我做错了什么?
答案 0 :(得分:3)
这有点晚了,但这是我的代码。如果它不再帮助你,它可以帮助其他人。我不能确切地说你的代码有什么问题,我自己对OpenCV很新:
public void Calculate()
{
_pointCount = _leftPoints.Count;
IntPtr points1 = CreatePointListPointer(_leftPoints);
IntPtr points2 = CreatePointListPointer(_rightPoints);
IntPtr status = CvInvoke.cvCreateMat(1, _pointCount, MAT_DEPTH.CV_8U);
IntPtr fundamentalMatrix = CvInvoke.cvCreateMat(3, 3, MAT_DEPTH.CV_32F);
int fmCount = CvInvoke.cvFindFundamentalMat(points1,
points2,
fundamentalMatrix,
CV_FM.CV_FM_7POINT,
3.0,
0.99,
status);
}
public IntPtr CreatePointListPointer(IList<PointF> points)
{
IntPtr result = CvInvoke.cvCreateMat(_pointCount, 2, MAT_DEPTH.CV_32F);
for (int i = 0; i < _pointCount; i++)
{
double currentX = points[i].X;
double currentY = points[i].Y;
CvInvoke.cvSet2D(result, i, 0, new MCvScalar(currentX));
CvInvoke.cvSet2D(result, i, 1, new MCvScalar(currentY));
}
return result;
}