# Estimation of ball position (3d reconstruction)

I have a white small ball in camera view. The ball is in front of camera but higher than camera (Yball > Ycam). View is compensated by undistort(). I try to calculate X and Y position of the ball relative to camera position. What I know:
- know exact distance from the optical center of the camera to the ball,
- know shift dX,dY of my ball in pixels from center of cam view,
- know focal length in pixels (fx and fy from camera matrix determined by camera calibration).

I try calculate physical X and Y position relative to camera using dx,dy,fx,fy,distance. If the position of the ball is near the center of the screen then I can accurately calculate the position using fx and fy. But I noticed, that if the ball is on the side, near the edge of the view (but physicial Y position and distance from camera are the same as previous), then the ball in image is higher. Here's an example:

1) Ball X coordinate is in center

2) Ball X coordinate high (near right side of image)

On the second example, 'top' coordinate changed from 433px to 396px, but ball is on the same physical Y as in previous example and at the same distance from the optical center. So if I use 'fy' (focal length y) to calculate Y position, my estimated position will be different.
Could You please help me, what am I doing wrong? How to calculate the position of the ball, what parameter did not take into account?

edit retag close merge delete

Sort by ยป oldest newest most voted

Here is sample code to calculate the line of sight vector to a point. Since you know the distance to the ball (the red circle), you can simply scale the line of sight by that distance to get your answer.

Note that this provides the answer in camera coordinates. Where X is to the right, Y is down, and Z is out of the camera. Transform as necessary.

    double x = pt.x;
double y = pt.y;

Mat LOS(3,1,CV_64F);

LOS.at<double>(0) = ptsOut[0].x;
LOS.at<double>(1) = ptsOut[0].y;
LOS.at<double>(2) = 1;

LOS = cameraMatrix.inv()*LOS;

more

Official site

GitHub

Wiki

Documentation