Emgu CV ArucoInvoke.InterpolateCornersCharuco给出错误“ Emgu.CV.Util.CvException:'OpenCV:0 <= i && i <(int)vv.size()'”

时间:2019-10-11 16:02:15

标签: c# opencv emgucv

我正在尝试使用EmguCV检测字符标记,并且已经从OpenCV网站复制并重写了example

我的代码如下:

Mat cameraMatrix = new Mat(new Size(3, 3), Emgu.CV.CvEnum.DepthType.Cv32F, 1);
Emgu.CV.Matrix<float> matrix = new Emgu.CV.Matrix<float>(3, 3);
matrix.Data[0, 0] = 925.6412f;
matrix.Data[0, 2] = 640.055f;
matrix.Data[1, 1] = 925.4835f;
matrix.Data[1, 2] = 338.2524f;
matrix.Data[2, 2] = 1;
cameraMatrix = matrix.Mat;
Mat distCoefficients = new Mat(new Size(5, 1), Emgu.CV.CvEnum.DepthType.Cv32F, 1);
Emgu.CV.Matrix<float> matrix2 = new Emgu.CV.Matrix<float>(5, 1);
matrix2.Data[0, 0] = 0;
matrix2.Data[1, 0] = 0;
matrix2.Data[2, 0] = 0;
matrix2.Data[3, 0] = 0;
matrix2.Data[4, 0] = 0;
distCoefficients = matrix2.Mat;
var image = new Image<Bgr, byte>("test.png");
var imageOut = new Image<Bgr, byte>(image.Width, image.Height);
var dictionary = new Dictionary(Dictionary.PredefinedDictionaryName.Dict4X4_100);
var detectorParameters = DetectorParameters.GetDefault();
var rejected = new VectorOfVectorOfPointF();
var board = new CharucoBoard(5, 5, 0.04f, 0.03f, dictionary);
VectorOfInt ids = new VectorOfInt();
VectorOfVectorOfPointF corners = new VectorOfVectorOfPointF();
ArucoInvoke.DetectMarkers(image, dictionary, corners, ids, detectorParameters, rejected);
if (ids.Size> 0)
{
    var charucoIds = new VectorOfInt();
    var charucoCorners = new VectorOfVectorOfPointF();
    ArucoInvoke.InterpolateCornersCharuco(corners, ids, image, board, charucoCorners, charucoIds, cameraMatrix, distCoefficients);
    // if at least one charuco corner detected
    if (charucoIds.Size > 0)
    {
        ArucoInvoke.DrawDetectedCornersCharuco(imageOut, charucoCorners, charucoIds, new MCvScalar(0, 255, 0));
        Mat rvec = new Mat();
        Mat tvec = new Mat();
        bool valid = ArucoInvoke.EstimatePoseCharucoBoard(charucoCorners, charucoIds, board, cameraMatrix, distCoefficients, rvec, tvec);       
        if (valid)
            ArucoInvoke.DrawAxis(imageOut, cameraMatrix, distCoefficients, rvec, tvec, 0.1f);
    }
}
imageOut.Save("out.png");

ArucoInvoke.InterpolateCornersCharuco方法中,我得到了例外:

  

OpenCV:0 <= i && i <(int)vv.size()“

corners.Size -> 12ids.values -> int[11]期间。

这是我的示例图片:

Charuco marker test image

我在做什么错?我正在使用EmguCV 4.1.1.3497版本。我希望有人可以帮助我解决这个问题。

2 个答案:

答案 0 :(得分:0)

我使用您提供的链接中的“ .png”图片尝试了您的代码,但未显示任何错误。但是,当我尝试使用以下命令时:your image,即“ .jpg”,它显示了。尝试改用“ .png”图片。

答案 1 :(得分:0)

经过一些测试,我找到了解决方案。由于某种原因,检测到的charuco角的输出类型不正确。

更改

var charucoCorners = new VectorOfVectorOfPointF();

var charucoCorners = new Mat();

解决了这个问题。