PnP exception. Duplicate in another program works

The following code in program A works:

Mat rvec, tvec;
Mat R;
solvePnP(model_points, image_points, K, distCoeffs, rvec, tvec);

rvec/tvec have values

In program B the solvePnP throws an exception:



Mat rvec, tvec;
Mat R;
solvePnP(model_points, image_points, K, distCoeffs, rvec, tvec);

this code has a result in which PnP has empty rvec[] and tvec[].

What am I missing here? Would any additional info be helpful?

if so, which exception, exactly ?

what is in image / model points ? K ?
what is the difference between your programs ?

1 Like
Unhandled exception at 0x00007FFF5BECA388 in programalpha.exe: Microsoft C++ exception: cv::Exception at memory location 0x0000009A7552D8B0.

image points are camera display coordinates. K are camera intrinsics:

Mat K(3, 3, CV_64FC1);
std::vector<cv::Point2d>image_points;

to reproduce your problem, we need to see the values of those

1 Like
image_points:[260, 44;
 617, 122;
 768, 292;
 170, 289]
K =[260.257998425127, 0, 386.6963736491591;
 0, 260.1583925187085, 234.7746525148251;
 0, 0, 1]
Radial distortion: 
[-0.2666308246430311;
 0.06474699227144737;
 0.0003621024764932747;
 -0.000726010205813438;
 -0.006384634912197317]
rvec =[0;
 0;
 0]
tvec =[0;
 0;
 0]
1 Like

and what are the model points? (values)

different programs, different arguments. don’t expect solvePnP to return the same when the inputs aren’t the same.

1 Like

Here is object data. I had to recollect a new batch, since the old model(object) data was left out originally in error, had to make all of it current.

object points:[0, 0;
 384, 0;
 384, 216;
 0, 216]
image_points:[253, 34;
 584, 152;
 692, 316;
 121, 280]

K =[260.257998425127, 0, 386.6963736491591;
 0, 260.1583925187085, 234.7746525148251;
 0, 0, 1]

Radial distortion: [-0.2666308246430311;
 0.06474699227144737;
 0.0003621024764932747;
 -0.000726010205813438;
 -0.006384634912197317]
rvec =[]
tvec =[]

ok so, I added a Z=0 to the object points. then solvePnP is happy.

if that was the issue, you should have seen a violated assertion like so:

cv2.error: OpenCV(4.5.2-dev) P:\opencv\modules\calib3d\src\solvepnp.cpp:826: error: (-215:Assertion failed) ( (npoints >= 4) || (npoints == 3 && flags == SOLVEPNP_ITERATIVE && useExtrinsicGuess) || (npoints >= 3 && flags == SOLVEPNP_SQPNP) ) && npoints == std::max(ipoints.checkVector(2, CV_32F), ipoints.checkVector(2, CV_64F)) in function 'cv::solvePnPGeneric'

if that was the issue and you didn’t see that assertion message, something is messed up in your setup.

here’s the code I used:

objp = np.float32([0,0,0, 384,0,0, 384,216,0, 0,216,0]).reshape((-1, 1, 3))
imp = np.float32([253,34, 584,152, 692,316, 121,280]).reshape((-1, 1, 2))
K = np.float32([[260.257998425127, 0, 386.6963736491591], [0, 260.1583925187085, 234.7746525148251], [0,0,1]])
dc = np.float32([-0.2666308246430311, 0.06474699227144737, 0.0003621024764932747, -0.000726010205813438, -0.006384634912197317])

(rv, rvec, tvec) = cv.solvePnP(objectPoints=objp, imagePoints=imp, cameraMatrix=K, distCoeffs=dc)

# rv
True
# rvec
array([[-0.48069],
       [-0.39676],
       [-0.01492]])
# tvec
array([[ -95.71424],
       [-145.90773],
       [ 129.29055]])

results seem somewhat sensitive to the exact distortion coefficients. oh well. at least it doesn’t just return nothing.

1 Like

That’s exactly right better than coming up with nothing. Thank you.

As it turns out my object data was missing a column of zeros, that you remedied by adding a z = 0: See the error in my object points immediately below?

change :

[0,0
384,0
384, 216
0, 216]

to

[0,0,0
384,0,0
384, 216, 0
0, 216, 0]

I was stuck in the holography state of mind of 2D to 2D and forgot the object points are 3D.

Thanks

how do you mean that?

yes, obviously I did see that.

If i came accross as anything other than humbled from a great answer which took time and accurscy to post, my apologies.

I was trying to demonstrate that i understood your answer. Somehow ive got things turned backwards.

If i came accross as anything other than humbled from a great answer which took time and accurscy to post, my apologies.

I was trying to demonstrate that i understood your answer. Somehow ive got things turned backwards

If i came accross as anything other than humbled from a great answer which took time and accurscy to post, my apologies.

I was trying to demonstrate that i understood your answer. Somehow ive got things turned backwards.

** your great answer