Ask Your Question

camera calibration - tracking object distance

asked 2017-04-29 05:13:37 -0500

Sinjon gravatar image


I'm trying to calibrate my camera so I can eventually figure how far an object is away from the camera.

I've been following this question to detail the equation for figuring distance of an object but don't have enough reputation to comment or enough to publish a link so there's a space in http: h ttp://

there are a couple things I'm unsure about, my matrix after running the with 30 or so photos, ran it another time with a different set of photos and got pretty much the same results.

RMS: 0.230393020863
camera matrix:
 [[ 294.17185696    0.          153.23247818]
 [   0.          295.43662344  119.46194893]
 [   0.            0.            1.        ]]
distortion coefficients:  [ 0.14143871 -0.76981318 -0.01467287 
-0.00334742  0.88460406]

in the other forum, the matrix was built for a iPhone 5S camera, the f_x and f_y results were 2.8036 but written down as:

f_x = 2803
f_y = 2805
c_x = 1637
c_y = 1271

why was it multiplied by 1000? should mine be as followed then?:

F_X = 294171
F_Y = 295436 
C_X = 153232
X_Y = 119419

The further down to calculate pixels in a lower-resolution the object is calculated to be 41 pixels. I've got code working to track a blue ball, shown below. what do I need to do to calculate the size of ball?

if len(cnts) > 0:
        c = max(cnts, key=cv2.contourArea)
        ((x, y), radius) = cv2.minEnclosingCircle(c)
        M = cv2.moments(c)
        center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))

        if radius > 10:
  , (int(x), int(y)), int(radius),
                (0, 255, 255), 2)
  , center, 5, (0, 0, 255), -1)

Thanks for your help!!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2017-04-29 11:17:50 -0500

Tetragramm gravatar image

They were written as 2803 because the number was 2.80360356e+03. Which is the same number, just with the decimal place rounded off. Note the scientific notation. (e+03)

Your number is just 294.

To calculate the distance to the ball, you must know the size of the ball. To calculate the size of the ball, you must know the distance to the ball. So measure one in the real world, and then you can know the other.

edit flag offensive delete link more


brilliant thanks you!

My end goal is to track the speed of the ball. If I determine the size of the ball in pixels can I use that to calculate speed? say the ball is 10cm in real life, in the video stream its 100px meaning that 10px is the equivalent of 1 cm. If I can find the distance moved per frame, can I use this info to calculate speed?

I can detect the ball but I'm not sure how to determine the balls pixel size.. @Tetragramm

Sinjon gravatar imageSinjon ( 2017-04-30 10:40:31 -0500 )edit

It is better to use the size of the ball to find the distance the ball is, then use that to get the speed in real life. If the distance to the ball is changing, the 10px = 1cm would change too, and everything would be wrong.

As to finding the size of the ball, there are many ways of doing that, so I can't tell you the "right" way. Take a look around and you'll find several, even in the OpenCV examples.

Tetragramm gravatar imageTetragramm ( 2017-05-01 17:46:24 -0500 )edit

the ball in this project will only be traveling up and down in this project so that'll work perfectly. I was able to get arclength working after some trial and error and could calculate the diameter! now the problem is that the diameter is constantly changing as the video detects the contours. I want to input the first 5 values into a np.array and calculate the mean to use as my object size.

But I'm having difficulty setting the array up to only contain 5 values.

av_diameter = np.array(diameter) 
av_diameter = np.mean(av_diameter)

I've tried putting (5), a data type and also using .shape but having no luck. Also, once the array is filled. will these values be maintained in there or be replaced with new values entering array? @Tetragramm

Sinjon gravatar imageSinjon ( 2017-05-02 12:25:54 -0500 )edit

I strongly recommend finding the distance and using that. Even travelling up and down, the distance from the camera will change. Especially if you need any precision, you will see errors.

As for how to use python, no idea. I work in C++, and you don't post nearly enough code to figure out how you're doing things.

Lastly, you don't need to tag me. You get notifications for any response to your post, your answer or something you've commented on.

Tetragramm gravatar imageTetragramm ( 2017-05-02 18:22:58 -0500 )edit

Question Tools

1 follower


Asked: 2017-04-29 05:13:37 -0500

Seen: 2,397 times

Last updated: Apr 29 '17