Measuring objects in photographs
Version March 11, 2024
Bart Hazes
© 2024. This work is licensed under a CC BY NC SA 4.0 license.
Index
How big is that thing in my picture?
Shouldn’t the camera know this?
Real-world image width dimension
Camera brand or lens limitations
Have you ever wanted to know how big something is in a photo? How would you do it? For police investigations or archeological excavations they simply place a ruler in the scene being photographed. But what if you have an image without a ruler to use for scale? I ran into this issue when needing to measure the size of fish from images I take underwater. Holding a ruler next to a swimming fish is clearly not an option.
You could calculate the real world dimensions of an image if you knew the size of the camera sensor and the magnification factor for the particular lens and focus distance. The first we know exactly and manufacturers already report the magnification at the minimum focus distance for all lenses they make. We would not have a problem if they just recorded the actual magnification factor for each image you took. Unfortunately, life is not that simple 🙁
Inside each digital image there is an EXIF header with data about the image. This includes the date and time the image was taken, lens used, shutter time etc. Unfortunately, there is no accurate magnification factor information in the EXIF produced by my Olympus mirrorless cameras and, to my knowledge, the same applies to other brands. You can use the free program exiftool to read the EXIF data. Below are the first and last 5 lines for the left image of figure 3, as well as a few lines with focus-related information.
ExifTool Version Number : 12.42
File Name : EG224025.ORF
Directory : .
File Size : 17 MB
File Modification Date/Time : 2023:09:26 02:16:53+02:00
...
Focus Step Count : 18914
Focus Step Infinity : 1348
Focus Step Near : 18597
Focus Distance : 0.185 m
...
Field Of View : 11.6 deg (0.04 m)
Focal Length : 60.0 mm (35 mm equivalent: 120.2 mm)
Hyperfocal Distance : 85.71 m
Lens ID : Olympus M.Zuiko Digital ED 60mm F2.8 Macro
Light Value : 9.9
Figure 1: sections of EXIF data read with exiftool
The EXIF data includes other information that could be used. For instance, it records the field of view and focus distance (bold font above). Together those define the magnification factor but unfortunately the focus distance is very inaccurate as has been discussed by others online. My Olympus camera even records the real-world dimension of the image diagonal (0.04 m in the second bold line above). To test if this is accurate I shot images of a ruler from different distances. In Figure 2 I plot the reported EXIF size against the size based on the ruler. The results clearly show that the EXIF information does not match reality.
Figure 2: Diagonal image size recorded in the EXIF data does not accurately match the measured diagonal length.
We can solve the problem if we know the focus position of the lens but, as mentioned, the focus distance reported in the EXIF data is too inaccurate. Fortunately the Olympus EXIF data also records the Focus Step Count. This is the actual ‘number of steps’ by which the focus lens was moved by the stepper motor to achieve focus. Because there are many thousands of steps between closest and infinity focus, the step count very accurately defines the focus point. What is not clear is how it relates to the focus distance. Perhaps this relationship even varies from lens to lens and companies don’t use it because it would be too expensive to calibrate each lens. But we can calibrate the lens ourselves!
Figure 3: close-up at 1:1 macro (left) and from a greater distance (right). Because I measure distance underwater I have to calibrate underwater. So this was done in a swimming pool.
To calibrate your lens, you simply need to take a number of pictures of an object of known size at different focus distances. The easy solution is to take pictures of a ruler (Fig. 3). The camera will record the Focus Step Count and you can use the image of the ruler to determine the actual width of the image. You can then plot the image width as a function of the step count (Fig. 4). The left panel shows a clear, but non-linear, relationship between the step count and image width. You can convert this to a linear relationship by plotting the logarithm of the step count against the logarithm of the image width (right panel). With this relationship between the step count and image width you can now calculate the full image width for any Focus Step Count by interpolation or, better, curve-fitting.
Figure 4: Relationship between Focus Step Count and full image width as measured with a ruler. Direct, non-linear, relationship (left panel), approximately linear relationship after logarithmic transformation (right panel).
The calibration should be done under conditions that are the same as the ones you plan to use for taking images. For me that means I need to calibrate with underwater images (diffraction effects cause a 1.3x magnification underwater). Similar you can calibrate while using a diopter, extension tube, or teleconverter.
To test the accuracy of the method I used the recorded Focus Step Count to calculate the full width of each ruler image. The left panel of Figure 5 shows how well the calculated and measured values match up. In a perfect world all points lie on the diagonal, which is approximately the case. The right panel shows the percent deviation between the measured and calculated values. In most cases the error is less than 1% and never more than 4%. The calculation is actually more accurate than that because part of the deviation is due to errors in measuring the image width. In conclusion, image dimensions can be measured with high accuracy.
Figure 5: Validation of the method. Left panel compares the measured and calculated image width. The right panel shows the error relative to the measured image width, for different image widths. Each point is based on an image of the ruler. Points to the left are close-up images, points to the right are at longer focus distances.
Using the relationship that came out of the calibration process you can now calculate the full width of any image based on its Focus Step Count. You could write this information in the legend of an image or you could use it to draw a scale bar on the image. Both approaches will give a visual sense of scale.
Another solution is to determine the real-world dimension covered by a single pixel in the image. You do that by dividing the full image width by the number of pixels along the width of the sensor. To find the distance between two points in an image, you simply determine the number of pixels between them and multiply it with the single pixel dimension. On my Macbook laptop I can do that for instance with the default image viewer Preview (Fig. 6). In this example, the total length of the fish is x pixel_size.
Figure 6: Measuring horizontal and vertical pixel dimensions of an object with the Preview image viewer on a Macbook.
I realize that this method takes more math and computational skills than many photographers can muster. If that applies to you but you really would like to use this, then reach out to me. If there is enough interest I can work on an online tool to do the calibration and curve fitting followed by applying that curve to a set of user images. That way you only need to make a set of calibration images like the ones shown in figure 3.
The biggest limitation is that not all camera brands record the Focus Step Count information in the EXIF header. For instance, I am considering switching from Olympus to Sony but that may not happen because, as far as I can see, Sony does not record this information 🙁. Different lenses also use different focus motors, and not all of them are stepper motors. These lenses may have a special encoder to detect the focus lens position but I have not looked into that yet.
The calculated dimensions are valid only at the focal distance. If your object is out of focus the size will be over or underestimated if it is closer or further than the focal plane, respectively. To visually confirm correct focus you may want to take the picture with the aperture wide open. That gives maximum blurring for objects that are not in focus.
For my use, measuring the length of fish underwater, it is important to take the picture with the fish perpendicular to the lens. Otherwise you will underestimate its length.
So far I have only used this for my 60 mm macro lens. It would work the same for any other prime lens. In principle, you can calibrate a zoom lens at different settings of both focus distance and focal length (zoom setting), but I have not done this (yet). For now, you can simply calibrate the zoom lens for each focal length of interest within the zoom range.
Figure 5 shows that for large image width, which corresponds to a long focus distance, the method is less accurate and it seems to systematically underestimate the true image width. This makes sense because beyond a certain distance the lens reaches ‘infinity focus’ where the focus distance and step count no longer change if you focus on objects beyond that distance.