如何修复未处理的OpenCVException来调用Cv2.SolvePnP()?

时间:2016-05-17 15:01:54

标签: c# opencv exception opencvsharp

我尝试像Cv2.SolvePnP()那样致电opencvsharp

using System.Collections.Generic;

using OpenCvSharp;

namespace Test
{
    public class SolvePnPTest
    {
        public static void Main(string[] args)
        {
            var objectPoints = new List<Point3f>();
            objectPoints.Add(new Point3f(1, 2, 3));
            objectPoints.Add(new Point3f(1, 2, 3));
            objectPoints.Add(new Point3f(1, 2, 3));
            objectPoints.Add(new Point3f(1, 2, 3));

            var imagePoints = new List<Point2f>();
            imagePoints.Add(new Point2f(1, 2));
            imagePoints.Add(new Point2f(1, 2));
            imagePoints.Add(new Point2f(1, 2));
            imagePoints.Add(new Point2f(1, 2));

            var rvec = new double[3];
            var tvec = new double[3];

            var cameraMatrix = new double[,] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
            var distortionCoefficients = new double[] { 1, 2, 3, 4, 5 };

            Cv2.SolvePnP(
                objectPoints,
                imagePoints,
                cameraMatrix,
                distortionCoefficients,
                out rvec,
                out tvec);
        }
    }
}

Cv2.SolvePnP的调用因未处理的OpenCVException而失败:

src.size == dst.size && src.channels() == dst.channels()

什么是src?什么是dst

包括堆栈跟踪在内的错误详情如下:

HResult=-2146232832
Message=src.size == dst.size && src.channels() == dst.channels()
Source=OpenCvSharp
ErrMsg=src.size == dst.size && src.channels() == dst.channels()
FileName=..\..\..\modules\core\src\convert.cpp
FuncName=cvConvertScale
Line=5475
StackTrace:
     at OpenCvSharp.NativeMethods.<>c.<.cctor>b__1332_0(ErrorCode status, String funcName, String errMsg, String fileName, Int32 line, IntPtr userdata)
     at OpenCvSharp.NativeMethods.calib3d_solvePnP_vector(Point3f[] objectPoints, Int32 objectPointsLength, Point2f[] imagePoints, Int32 imagePointsLength, Double[,] cameraMatrix, Double[] distCoeffs, Int32 distCoeffsLength, Double[] rvec, Double[] tvec, Int32 useExtrinsicGuess, Int32 flags)
     at OpenCvSharp.Cv2.SolvePnP(IEnumerable`1 objectPoints, IEnumerable`1 imagePoints, Double[,] cameraMatrix, IEnumerable`1 distCoeffs, Double[]& rvec, Double[]& tvec, Boolean useExtrinsicGuess, SolvePnPFlags flags)
     at Test.SolvePnPTest.Main(String[] args) in <file path>.
InnerException: 

我的代码有什么问题?

convert.cpp中的断言在这个opencv函数中:

CV_IMPL void
cvConvertScale( const void* srcarr, void* dstarr,
                double scale, double shift )
{
    cv::Mat src = cv::cvarrToMat(srcarr), dst = cv::cvarrToMat(dstarr);

    CV_Assert( src.size == dst.size && src.channels() == dst.channels() );
    src.convertTo(dst, dst.type(), scale, shift);
}

2 个答案:

答案 0 :(得分:1)

嗯,我认为

的唯一原因
src.size == dst.size && src.channels() == dst.channels() in cvConvertScale()

是你正在使用一个函数,其中源矩阵(src)与目标矩阵(dest)不同[就mat.size或/和mat.channels而言(其中一个有不同)尺寸,是未初始化的,当另一个是灰度时,一个是RGB ...)],它们必须是相同的。

这就是“为什么?”为此。

现在,我不是列表的专家,我不确定这是否有意义,但在SolvePnp documentation中,他们使用向量,试一试:

 std::vector<Point3f> objectPoints;
 objectPoints.push_back(new Point3f(1, 2, 3));
 objectPoints.push_back(new Point3f(1, 2, 3));
 objectPoints.push_back(new Point3f(1, 2, 3));
 objectPoints.push_back(new Point3f(1, 2, 3));

答案 1 :(得分:1)

由于该方法的重载失败,

public static void SolvePnP(
    IEnumerable<Point3f> objectPoints,
    IEnumerable<Point2f> imagePoints,
    double[,] cameraMatrix,
    IEnumerable<double> distCoeffs,
    out double[] rvec,
    out double[] tvec,
    bool useExtrinsicGuess = false,
    SolvePnPFlags flags = SolvePnPFlags.Iterative
)

我尝试了the other

public static void SolvePnP(
    InputArray objectPoints,
    InputArray imagePoints,
    InputArray cameraMatrix,
    InputArray distCoeffs,
    OutputArray rvec,
    OutputArray tvec,
    bool useExtrinsicGuess = false,
    SolvePnPFlags flags = SolvePnPFlags.Iterative
)

执行。完整的示例代码:

using System.Collections.Generic;

using OpenCvSharp;

namespace Test
{
    public class SolvePnPTest
    {
        public static void Main(string[] args)
        {
            var objectPoints = new List<Point3f>();
            objectPoints.Add(new Point3f(1, 2, 3));
            objectPoints.Add(new Point3f(1, 2, 3));
            objectPoints.Add(new Point3f(1, 2, 3));
            objectPoints.Add(new Point3f(1, 2, 3));

            var imagePoints = new List<Point2f>();
            imagePoints.Add(new Point2f(1, 2));
            imagePoints.Add(new Point2f(1, 2));
            imagePoints.Add(new Point2f(1, 2));
            imagePoints.Add(new Point2f(1, 2));

            var rvec = new double[3];
            var tvec = new double[3];

            var cameraMatrix = new double[,] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
            var distortionCoefficients = new double[] { 1, 2, 3, 4, 5 };

            /*
            Cv2.SolvePnP(
                objectPoints,
                imagePoints,
                cameraMatrix,
                distortionCoefficients,
                out rvec,
                out tvec);
             * 
             * */

            Cv2.SolvePnP(
                new Mat(objectPoints.Count, 3, MatType.CV_64F, objectPoints.ToArray()),
                new Mat(imagePoints.Count, 2, MatType.CV_64F, imagePoints.ToArray()),
                new Mat(3, 3, MatType.CV_64F, cameraMatrix),
                new Mat(5, 1, MatType.CV_64F, distortionCoefficients),
                new Mat(3, 1, MatType.CV_64F),
                new Mat(3, 1, MatType.CV_64F));
        }
    }
}