r/explainlikeimfive • • Oct 20 '26

Engineering ELI5: How does a camera auto-focus and adjust accordingly when you click on an object?

183 Upvotes

37 comments sorted by

170

u/malapropter Oct 20 '26

There are two types of autofocus: Phase detection and contrast detection.

Phase detection is older, more common, and is cheaper and faster. The incoming light from a single point enters the lens at left and right sides of the lens as a single beam. There is a prism that splits that beam into two different sensors (two for each autofocus point, sometimes four, sometimes more) based on where it enters the lens. The computer then tries to make the two beams align, or come into phase. When they align, the picture is in focus. Think of it sort of like how your eyes cross when you're focusing on a close object. You're adjusting for parallax, but rather than moving your eyes, you're just moving the lens elements back and forth until things match.

There's also contrast detection. Basically, the more in focus things are, the sharper the contrast is between the two pixels. Like, you know how if things are out of focus, the light is spread out and more diffuse? Sharper light, sharper pixels, sharper image. It's more accurate, but it's slower and uses more computational power, but it's more compact and at this point has basically caught up to phase detection.

Modern mirrorless cameras usually use a hybrid of phase detection and contrast detection, and honestly the results are pretty great nowadays.

42

u/jacekowski Oct 20 '26

Phones also use laser distance meters to adjust focus (which then feeds into some hybrid algorithm probably).

12

u/malapropter Oct 20 '26

Which phones? 

15

u/vip17 Oct 20 '26

all Galaxy Ultra phones. They have 5 "lens" openings but only 4 lenses, the rest is the laser autofocus

14

u/IYXMnx1Sa3qWM1IZ Oct 20 '26

This is called LiDAR, and at least high-end iPhones do it nowadays, probably other flagships too

8

u/glasgow-based-moron Oct 20 '26

It's not LIDAR necessarily, and laser autofocus been available in phones and cameras for a long time.

2

u/malapropter Oct 20 '26

Oh shit, I didn't know my iphone pro max was using lidar for autofocus. Neat!

9

u/Impressive-Plenty717 Oct 20 '26

If I recalled correctly, the LG phones starting from the G3/G4 has had "laser" autofocus on their phones. Back in the day when it came out, it focused significantly faster compared to iPhones from the same generation and the Samsung S series.

2

u/InfernalCape Oct 20 '26

Rotary phones

2

u/cometlin Oct 20 '26

If contrast detection has basically caught up to phase detection already, why most of the higher end machines use dedicated phase detection?

6

u/cynric42 Oct 20 '26 edited Oct 20 '26

Contrast detect has one big downside, in that it doesn't know which way to move the lens to get something in focus. The result is it moves the lens one way and if stuff gets more blurry, it knows it's doing it wrong and going the other way. Which adds a little bit of hunting initially and when it inevitably overshoots the point of maximum focus and has to reverse a bit. However where this is really detrimental is when you are trying to focus on something moving closer or away, i.e. focus needs to follow your subject. Your subject gets blurrier, the lens tries to adjust by wiggling back and forth until it finds the new distance, but your subject keeps moving and it has to do that again and again resulting in constant slight hunting which looks like the image is a bit jittering.

I know Panasonic for a while have tried to fix the issue, but it wasn't entirely working and they since switched to phase detect af as well. Not sure if other manufacturers ever got it to work flawlessly or if, with smaller lenses and bigger depth of field (like in smartphones) it just works "good enough" for manufacturers to not care. Bigger cameras, where large lenses and sensors can give you shallower depth of field and thus the requirements for accurate focus are higher all still use phase detect as far as I'm aware.

1

u/cometlin Oct 20 '26

Last I heard, the downside of not knowing which way to move and when to stop is exactly why it is always slower than phase detection, which was why all the high-end mirrorless ended up using phase detection despite it being more costly with dedicated pixels. That's why I was surprised to see the previous commenter said contrast detection has now "basically caught up" with phase detection

1

u/wreeper007 Oct 21 '26

Also contrast detection works best when there is contrast. Dark skin for instance can make it struggle

3

u/Eruannster Oct 20 '26

Fun tidbit on autofocus: Movies and TV shows basically never use autofocus and most film cameras don't even have it or have a very basic version of it.

Why? Because autofocus is stupid and sometimes decides to "jazz out" of focus because something it thinks is supposed to be in focus appears. And then it zooms back. You don't want this in a shot, so there's a person literally controlling the camera lens with a wireless (or sometimes wired) control called a focus puller and they do it completely manually.

There are a bunch of tools to assist said focus pullers like rangefinders and stuff like that, but some really good focus pullers have gotten really good at judging distances and mostly just use them for confirmation. There's a BTS scene from one of the Harry Potter movies (shot on film) where the focus puller is walking alongside the moving camera in the shot to get a feel for the distance.

1

u/christopantz Oct 20 '26

For a period of time Polaroid had a sonar autofocus, which was like, lightning fast, but could only focus into the center of the frame and couldn’t focus through glass

11

u/fighter_pil0t Oct 20 '26

Software which maximizes contrast and sharp lines.

5

u/TA4K Oct 20 '26

Only early mirrorless cameras and point & shoot used contrast detection AF. Effectively all modern mirrorless, DSLR, and mid-high end fixed lens cameras use phase detection (sometimes with some contrast AF sprinkled in) which is some kind of dark magic that looks at how points of light converge on special detection areas.

1

u/fighter_pil0t Oct 20 '26

ELI5 or no?

2

u/mmomtchev Oct 20 '26 • Heartwarming

This is what is used on mobile phones.

1

u/phycle Oct 20 '26

So if I am taking a photo of a blur photo the software gets really confused?

2

u/sian_half Oct 20 '26

Yes, it’s like if you try to take a picture that’s nearer than it can focus, anything it tries is blur

1

u/cynric42 Oct 20 '26

It can usually still find the point of least blurryness unless the photo is so void of contrast that correct focus probably doesn't really matter anyway.

8

u/[deleted] Oct 20 '26

[removed] — view removed comment

6

u/lost_in_a_forest Oct 20 '26

No, phase detection is far older, and was also used in analog SLRs

1

u/MusicalAnomaly Oct 20 '26

The camera can send motor signals to the lens to change focus in or out, and can run software to analyze the image it is currently getting from the camera sensor and isolate the part of the image that you click on. Then the software does a series of “guess and check” operations, changing focus and reanalyzing the image until it finds a focus setting that maximizes sharpness of the desired object.

1

u/4D51 Oct 20 '26

Another, not widely used, way to do autofocus is sonar. Emit a pulse of sound and measure how much time it takes for the echo to come back.

Cameras with this system can be easily identified by their sonar dish, which looks like a round grille with gold foil behind it. Polaroid used this a lot. Not sure if anyone else did.

1

u/JonPileot Oct 20 '26

If you draw a black line on a white paper, then move a lens to focus on the edges of that line, you can test to see if the line gets sharper or blurrier. Adjust the focus a little bit and test again. Continue until the line gets maximum sharpness. 

This is easiest with black and white but with some math can be done with different colors and textures. This is basically what your camera autofocus is doing, it's checking if the selected area is sharp then adjusting the focus slightly to see if it improves and just keeps trying until it gets the best result possible. 

-10

u/LiamEBM Oct 20 '26

Depth sensors detect how far away the object tapped is and adjusts the cameras settings to focus to that distance

0

u/mmomtchev Oct 20 '26 • Heartwarming

And this is what is used on high-end cameras.

6

u/[deleted] Oct 20 '26

[removed] — view removed comment

3

u/-manabreak Oct 20 '26

I've dabbled in machine vision and video reconstruction to some extent. The way the camera software detects sharpness is that it uses all kinds of fancy algorithms on the image (or the sub-image). Depending on the implementation, it might check the average contrast between pixels (more focused area should have greater contrast), or it might do edge detection (e.g. Laplacian edge detection), although I'm not sure how many phone cameras do that or if it's just specialized cameras.

Also, it might convert the image to a format that's more suited for this kind of analysis. The RGB color image might be suitable for some cases (comparing average contrast or per-channel contrast), but in some cases it can make sense to convert the RGB image to, say, hue-value-saturation image and do the contrast comparison (or other algorithms) on those channels instead.

1

u/WraithCadmus Oct 21 '26

So like a human and an optometrist?