3

我正在为 OpenCV 使用 EmguCV 包装器,并且我正在尝试通过基本矩阵进行未校准的校正。

我已经从两个相机中找到了带有 FindChessboardCorners 函数的图像点,然后我想找到基本矩阵,但是我在将参数传递给函数 eCvInvoke.cvFindFundamentalMat() CvInvoke.cvFindFundamentalMat( ) 时遇到问题

困扰我的是二维点数组,只是无法获得正确的格式将其传递给 OpenCv 函数 cvFindFundamentalMat。

据我了解,EmguCV/OpenCV openCv 需要通过 IntPtr 传递的 CvMat 类型的一维数组......但即使我这样做,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);

我究竟做错了什么?

4

1 回答 1

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;
}
于 2012-04-29T15:17:12.710 回答