I need some help regarding measuring distances between two balls and their position relative to surface(coordinates). Dimensions of balls and surface are known. Do i need just one camera to do this(measuring with ray diagrams and trigonometry)or more?

I hope this is the right thread,I tried on physics and photography before and it wasn't correct thread

  • $\begingroup$ Could you elaborate on the 'relative' notion? Do you like to get the distance or a ratio of distances? $\endgroup$ Jan 14 '18 at 22:33
  • $\begingroup$ Hint: camera matrix $\endgroup$ Jan 14 '18 at 22:33
  • $\begingroup$ M.M., I don't think a pinhole camera model is an accurate enough model for precision measurements. I did a calibration by measuring many points on a grid and building a best fit multiple dimension generalized Taylor series expansion for the mapping. $\endgroup$ Jan 15 '18 at 3:49
  • $\begingroup$ @CedronDawg that's certainly true; the OpenCV-documentation-preferred approach of course is having a giant "lens" matrix that approximates optical deformations locally; but the idea I wanted to insert here is that you can invert a system of equations if you have enough knowns to resolve your ambiguities. $\endgroup$ Jan 15 '18 at 8:48
  • $\begingroup$ I added an answer to a related question that is also appropriate for your situation. See my answer at dsp.stackexchange.com/questions/6376. Briefly, include a mirror in the field of vision. $\endgroup$ Jan 16 '18 at 16:22

Since nobody has answered this for you yet, I'll give it a shot.

You have to understand how to do ray tracing math, which means vectors. You also have to be able to calibrate your camera so you know for each pixel, what is the direction of the the ray coming from the focal point.

It makes a big difference if the objects are in motion or are still. If they are in motion you will need to deal with blurry edges.

Since the screen is two dimensional, you will only be able to make two dimensional direction measurements. However, with something like a ball with a known diameter, you can calculate how far it is from the focal point using trigonometry.

Having two cameras on the scene can give you much more accurate depth measurements. Again, this requires knowing the rays and doing trigonometry.

If your surface is flat, you can define a mapping between your pixel locations and a coordinate system on the surface.

Many years ago, I wrote a program using a webcam, where when I identified a sheet of notebook paper on the floor by clicking the four corners with my mouse, the program calculated the location of the camera relative to the paper within a tenth of an inch.

Once you have the objects located in 3D space, finding the distance between them is merely an application of the distance formula, aka Pythagorean Theorem.

Hope this helps.



Followup (too long for an add on comment above):

@M.M., I've never used OpenCV so I searched on "lens distortion matrix openCV" and found this:

Camera calibration With OpenCV

This approach is different than what I did, though somewhat similar.

Suppose $(m,n)$ is the pixel location and $(x,y,1)$ is the direction vector where the z-axis is straight out from the lens. I used calibration points, and a standard best fit approach to find the coefficients of:

$$ x = A_x m^2 + B_x n^2 + C_x m n + D_x m + E_x n + F_x $$ $$ y = A_y m^2 + B_y n^2 + C_y m n + D_y m + E_y n + F_y $$

This required finding the inverse of a 6x6 matrix to solve. I wrote the program in VisualBasic3 (I think) on an i386, to give you an idea of when.



More detail:

Spheres are easy because they look like a solid circle from any angle.

Find the center of the circle in pixel coordinates. A centroid calculation is a brute force way to do this, but there are more efficient ways.

Translate the pixel coordinates of the center to a ray emanating from your lens.

Then process several points around the edge.

For each point:

1) Translate to a ray

2) Calculate the angle to the center ray.

3) Figure the distance to the center of the sphere from the lens

Average the distances (they should ideally be identical) to get a good read.

Calculate the location in 3D coordinates of the center of the sphere from the center ray direction and the calculated distance.

Calculate the distances between the spheres using the distance formula.

Angle calculation formula for two vectors $\vec a$ and $\vec b$

$$ \theta = cos^{-1} \left( \frac{ \vec a \cdot \vec b }{ \| \vec a \| \| \vec b \| } \right) $$

Distance to sphere where $r$ is the radius

$$ d = \frac{ r }{ \sin \theta } $$

The center of the sphere:

$$ \vec c_k = d \frac{ \vec a_k }{ \| \vec a_k \| } = ( x_k, y_k, z_k ) $$

The distance between spheres:

$$ d_{k,l} = \sqrt{ ( x_k - x_l )^2 + ( y_k - y_l )^2 + ( y_k - y_l )^2 } $$

Your surface will require a different approach, but since you haven't provided any details, I can't help you there.


  • $\begingroup$ Thanks Cedron for the answer,it was pretty helpful.Balls will not be in motion,so the result will be more accurate.I will use better camera than webcam,but tenth of a inch is acceptable to me. $\endgroup$
    – user33223
    Jan 16 '18 at 1:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy