# Calculating the size of an object from a single image, given the distance from the camera

I was looking at other questions on here and I'm pretty sure it's not a duplicate so here it goes. I'm working on a problem where I need to calculate the size of a particular object in an image. The setup allows me to only have the object I need to measure in the view so I can't introduce an object I can relatively measure with a known length.

I have already looked at What is the relationship between size of object with distance? however I'm at a bit of a loss.

I'd have the distance between the object and the camera and I need to create a formula that lets me calculate the pixel-per-metric (PPM) of that object based on the distance from the camera.

I'm thinking that I can do this by taking samples where I know both the distance and the actual width of the object, I could deduce the relationship between the PPM and distance by finding the PPM for these samples and finding their correlation.

Edit: If it's relevant I'm using a Logitech brio, finding the focal length and sensor sizes for it has been impossible

You can calculate that distance given the formula for the pinhole camera model (which is an approximation, but should work well in the cases of interest). You need to know the focal distance (which by assumption, you should know — in practice, you can usually extract it from the EXIF data of the image) and the size of each pixel.

For the size of a pixel, you don't need to obtain it "experimentally" from sample images to determine the ratio. If you know the size of the sensor and the resolution, you're done. For example, on a 24Mpix full-frame sensor camera, the size of the sensor is 36mm by 24mm, and resolution is 6000 by 4000 (approximately — for example, my camera is actually 6064 by 4040, and I think it is 35.9mm by 23.9mm or something like that). That means that the pixel to mm conversion factor is 166.67 pixels per mm, or 0.006 mm per pixel (pixels should normally be perfectly square, meaning that the factor should be the same in both axes).

So, given a subject of size s pixels in the image when shot at a distance d from the camera, your formula for the size of the actual object, S given a focal length f would be:

S / d = s / f (this is because the triangle between the pinhole and the image plane, with sides f (distance from the pinhole to the image plane) and s is proportional to the triangle from the pinhole to the subject (sides d and S.

Applying the conversion factor to express s in mm instead of in pixels, you get:

S = d (any units) · s (px) · 0.006 (mm/px) / f (mm)

This gives you the size of the subject in the same units that you used for the distance of the subject to the camera (since the mm of the pix to mm factor cancels out with the mm of the focal distance, and the pixels just cancel out)

EDIT:
As you do not have information on the geometry of the camera (focal length or sensor/pixel size), you would be left with what you suggested: knowing that the actual size of the object is proportional to the size in the image (in pixels) and also to the distance, by a factor that depends on the focal length and pixel size, then you determine this factor experimentally: take an image of an object of a known size, at a known distance, and obtain the factor. This factor would correspond to the 0.006 / f in the equation above:

S = d (any units) · s (px) · camera_scaling_factor

Therefore:

camera_scaling_factor = S / ( d · s )

(notice that the actual size of your reference object and its distance to the camera need to be in the same units)

• Unfortunately certain parameters are quite hard to pinpoint, there's no published size or focal length of the logitech brio Oct 31, 2022 at 12:59
• I deleted my previous comment (after writing it, I noticed that you had indeed edited the question) Oct 31, 2022 at 15:27

The projected size `s` on your sensor behind a lens of focal length `f` of an objects of size `S` at distance `D` is:

``````s = (f × S) ÷ D
``````

If your measure is in pixels then your size in pixels `p` is in a linear relation with the projected size `s` on the sensors:

``````p = k × s
``````

where k in some constant. So your size in pixels is also:

``````p = k × (f × S) ÷ D
``````

If you reorganize this:

``````S = (D × p) ÷ (k × f)
``````

where you can replace `(k × f)`, a product of two constants by a single constant K (so K is just a constant that lumps together the focal length of the lens and the pixel density of the sensor):

``````S = (D × p) ÷ K  
``````

You can compute K in any shot where you have the distance, the real size and the size in pixels:

``````K = (D × p) ÷ S
``````

Once you have it, you can reuse it in formula #1 where you don't know the actual size but know the distance and the size in pixels.

This of course assumes that the focal length of the lens doesn't change when you focus.