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.
Ced
=============================================
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.
Ced
=============================================
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.
Ced