EmguCV FindFundamentalMat - input params

2k views Asked by At

I'm using EmguCV wrapper for OpenCV and I'm trying to do uncalibrated rectification via fundamental matrix.

I've found image points with FindChessboardCorners function,from both cameras, and then I would like to find fundamental matrix, but I have problem passing parameters to function eCvInvoke.cvFindFundamentalMat() CvInvoke.cvFindFundamentalMat()

What bothers me is the array of 2D points, just can't get the right format to pass it to OpenCv function cvFindFundamentalMat.

As I've understood EmguCV/OpenCV openCv expects one dimensional array of type CvMat, passed through IntPtr... But even when I do so, OpenCV throws an error message "OpenCV: Either the number of channels or columns or rows must be =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);

What am I doing wrong?

1

There are 1 answers

0
Ufuk Hacıoğulları On

It's a bit late but here's my code.It may help some others if it doesn't help you anymore. I can't say exactly what is wrong with your code, I'm very new to OpenCV myself:

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;
}