Consider the following code:
std::vector<cv::Point2f> vecUV = Vec2DFromFile(argv[2]);
std::vector<cv::Point3f> vecXYZ = Vec3DFromFile(argv[3]);
cv::Mat matCalib(3, 3, CV_64F);
cv::setIdentity(matCalib);
cv::Mat matRot(3, 1, CV_64F);
cv::Mat matTrans(3, 1, CV_64F);
cv::Mat matDistortion = cv::Mat::zeros(4, 1, CV_64F);
cv::solvePnP(vecUV, vecXYZ, matCalib, matDistortion, matRot, matTrans);
Where vecUV and vecXYZ are both size 4. This fails with:
OpenCV(3.4.1) Error: Assertion failed (( (npoints >= 4) || (npoints == 3 && flags == SOLVEPNP_ITERATIVE && useExtrinsicGuess) ) && npoints == std::max(ipoints.checkVector(2, 5), ipoints.checkVector(2, 6))) in solvePnP, file /var/tmp/portage/media-libs/opencv-3.4.1-r5/work/opencv-3.4.1/modules/calib3d/src/solvepnp.cpp, line 65
terminate called after throwing an instance of 'cv::Exception'
what(): OpenCV(3.4.1) /var/tmp/portage/media-libs/opencv-3.4.1-r5/work/opencv-3.4.1/modules/calib3d/src/solvepnp.cpp:65: error: (-215) ( (npoints >= 4) || (npoints == 3 && flags == SOLVEPNP_ITERATIVE && useExtrinsicGuess) ) && npoints == std::max(ipoints.checkVector(2, 5), ipoints.checkVector(2, 6)) in function solvePnP
I don't understand why the assertion fails given that I have 4 points for each vector. I tried stepping into it with gdb, to no avail:
...
(gdb) s
cv::_InputArray::init (this=0x7fffffffe350, _flags=-2130509811, _obj=0x7fffffffe2f0)
at /usr/include/opencv2/core/mat.inl.hpp:67
67 { flags = _flags; obj = (void*)_obj; }
(gdb) s
cv::_InputArray::_InputArray<cv::Point_<float> > (this=0x7fffffffe350,
vec=std::vector of length 4, capacity 4 = {...}) at /usr/include/opencv2/core/mat.inl.hpp:85
85 { init(FIXED_TYPE + STD_VECTOR + traits::Type<_Tp>::value + ACCESS_READ, &vec); }
(gdb) s
OpenCV(3.4.1) Error: Assertion failed (( (npoints >= 4) || (npoints == 3 && flags == SOLVEPNP_ITERATIVE && useExtrinsicGuess) ) && npoints == std::max(ipoints.checkVector(2, 5), ipoints.checkVector(2, 6))) in solvePnP, file /var/tmp/portage/media-libs/opencv-3.4.1-r5/work/opencv-3.4.1/modules/calib3d/src/solvepnp.cpp, line 65
terminate called after throwing an instance of 'cv::Exception'
0x00007ffff6e7334f in __gnu_cxx::__verbose_terminate_handler ()
from /usr/lib/gcc/x86_64-pc-linux-gnu/8.3.0/libstdc++.so.6
I have read this,m this and this answer, but it does not seem to be the same case - there, a wrong parameter type was to blame.
.zip with minimum reproducible example - pass the input files as second and third parameters, first parameter is reserved.