1

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?

User
  • 806
  • 1
  • 11
  • 28
  • 1
    two image points? AFAIK it should be at least 4 points for most of the PnP algorithms (I think one of them accepts 3). Read the [note](https://docs.opencv.org/3.4.6/d9/d0c/group__calib3d.html#ga549c2075fac14829ff4a58bc931c033d) to see the points needed – api55 Jul 03 '19 at 12:03

1 Answers1

1

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

User
  • 806
  • 1
  • 11
  • 28