Search code examples
c++opencvcamerazbarpose-estimation

OpenCV solvePnP method returns NaN values


I'm doing barcode detection using zbar library with opencv in C++. The barcode detection is working good and I'm getting results like that


result


Now I want to use cv::solvePnP to get the pose of my camera (already calibrated). As 3D points I'm using a template in which, at program start, I compute the same barcode detection and I take the top left corner and bottom right corner. I then compute the world coordinates of these two points with respect to the center of the barcode in this way:

(pt.x - imageSize.width / 2) * px_to_mm,
(pt.y - imageSize.height / 2) * px_to_mm,
0.

imageSize is the size of the barcode (in pixels), px_to_mm is the ratio "length in meters of the barcode height divided by the total number of pixel of the barcode height" and pt is the point (either top left or bottom right).

The template is


template


I checked that the resulting point of the barcode detection are correct. The world coordinates are top_left =[0.054160003, 0.025360001, 0], bottom_right = [0.085200004, 0.046080004, 0]. I assume that these are correct since the dimensions in pixels of the barcode are 388 x 200 and the height in meters is 0.016.


When I run cv::solvePnP I get these results

translation: [-nan, -nan, -nan] 
rotation: [-nan, nan, -nan;
           -nan, -nan, nan;
           nan, -nan, -nan]

The input of the method are the two image points of the barcode detection and the two world points computed using the template. What is the problem?


Solution

  • As api55 said in his comment the problem was the number of points. Adding the other 2 corners worked.