C#-EmguCV FindFundamentalMat-输入参数
作者:互联网
我将EmguCV包装器用于OpenCV,并且尝试通过基本矩阵进行未经校准的整流.
我已经从两个相机的FindChessboardCorners函数中找到了图像点,然后我想找到基本矩阵,但是将参数传递给函数eCvInvoke.cvFindFundamentalMat() CvInvoke.cvFindFundamentalMat()时遇到问题
困扰我的是2D点的数组,只是无法获得正确的格式来将其传递给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);
我究竟做错了什么?
解决方法:
有点晚了,但是这是我的代码,如果它不再对您有帮助的话,可能会对其他人有所帮助.我无法确切地说出您的代码出了什么问题,我本人对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;
}
标签:emgucv,opencv,camera-calibration,image-processing,c 来源: https://codeday.me/bug/20191201/2081187.html