liad inon
liad inon

Reputation: 263

I am trying to use cv2.solvePnP() but I am getting an error

This is the error:

cv2.solvePnP(obj_points, image_points, mtx, dist)
cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\calib3d\src\solvepnp.cpp:754: error:
(-215:Assertion failed) ( (npoints >= 4) || (npoints == 3 && flags == SOLVEPNP_ITERATIVE && 
useExtrinsicGuess) ) && npoints == std::max(ipoints.checkVector(2, CV_32F), ipoints.checkVector(2, 
CV_64F)) in function 'cv::solvePnPGeneric'

And this is my code:

mtx = np.load("./camera_params/mtx.npy")
dist = np.load("./camera_params/dist.npy")
obj_points = np.array([[0, 0, 0], [297, 0, 0], [297, 210, 0], [0, 210, 0]])
image_points = np.array([[416, 268], [422, 535], [826, 543], [829, 264]])
cv2.solvePnP(obj_points, image_points, mtx, dist)

I don't have any idea how to solve it. I tried to play with the arguments but it didn't help. If you know a way to solve this error it be very helpful.

Upvotes: 6

Views: 4826

Answers (1)

spaghettibaguetti
spaghettibaguetti

Reputation: 274

I had this error very recently, and I resolved it by making the argument ndarrays floats instead of ints. You can do that in 2 ways:

obj_points = np.array([[0.0, 0.0, 0.0], [297.0, 0.0, 0.0], [297.0, 210.0, 0.0], [0.0, 210.0, 0.0]])
image_points = np.array([[416.0, 268.0], [422.0, 535.0], [826.0, 543.0], [829.0, 264.0]])
obj_points = np.array([[0, 0, 0], [297, 0, 0], [297, 210, 0], [0, 210, 0]])
obj_points = obj_points.astype('float32')
image_points = np.array([[416, 268], [422, 535], [826, 543], [829, 264]])
image_points = image_points.astype('float32')

Upvotes: 5

Related Questions