Exception in cvConvertScale in OpenCV calling solvePnP

半城伤御伤魂 提交于 2019-12-11 11:46:30

问题


I'm trying to use solvePnP from OpenCV (through OpenCvSharp) but I get an exception that I don't understand.

An unhandled exception of type 'OpenCvSharp.OpenCVException' occurred in OpenCvSharp.dll
Additional information: src.size == dst.size && src.channels() == dst.channels()

After some searching, I found that it comes from cvConvertScale in convert.cpp

Here is how I use it:

var objectPoints = new OpenCvSharp.CPlusPlus.Point3f[4] { o1, o2, o3, o4 };
var imagePoints = new OpenCvSharp.CPlusPlus.Point2f[4] { i1, i2, i3, i4 };
var intrinsic = new double[3, 3] { { d1, d2, d3 }, { d4, d5, d6}, { d7, d8, d9 } };

double[] rvec, tvec;

OpenCvSharp.CPlusPlus.Cv2.SolvePnP(objectPoints,
                                   imagePoints,
                                   intrinsic,
                                   null, out rvec, out tvec);

If I understand this exception right, it means there is a conversion of sort happening and the source and destination matrixes don't have the same size or the same number of channels. But both my list of points are of the same size. My camera matrix is 3x3 which should be fine. I don't get it.

Could someone shed some light on this ?


回答1:


Well I think I found the issue so I'll post it here in case anyone stumble upon this...

It seems my camera matrix was not properly initialized. I don't know why but I dont get a 3x3 matrix from my double[3,3]. So I initialized it using the OpenCV Mat constructor like this:

var intrinsic = new Mat(3, 3, MatType.CV_64F, new double[] { d1, d2, d3, d4, d5, d6, d7, d8, d9 });

And now it's working... if anyone know why my double[3,3] was not producing the corresponding Mat(3,3) feel free to share !



来源:https://stackoverflow.com/questions/29142642/exception-in-cvconvertscale-in-opencv-calling-solvepnp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!