Help Wanted: FocusMeteringAction Best Practices

73 views
Skip to first unread message

Javier Gerardo Martinez Salomon

unread,
Feb 21, 2024, 9:57:59 AMFeb 21
to Android CameraX Discussion Group
Hi guys,

I'm still struggling a bit to get the best focus possible for this kind of devices, specially when it comes to performing manual focus, I've observed that in normal conditions the CameraX AF routine does a fairly good job at focusing in the ID(see picture in the first message), however AF struggles when there's a change in the distance of the target object and fails to re-focus automatically unless I issue a manual `FocusMeteringAction`, when I do that, I get worse results than the ones I would get with AF, that is, the soft edges are way more noticeable.

I am wondering, what is the best way of issuing a manual `FocusMeteringAction` to focus on an ID such as the one attached to this thread?, this is what I'm doing right now.

1. Get the x and y coordinates from the center of the SurfaceView, since I assume that the object is centered as well.
2. I use a `SurfaceOrientedMeteringPointFactory` to build the MeteringPoint, I use the default size but have tried changing it to different values with the same results.
3. Then I use the focus point like this:
val meteringAction =
FocusMeteringAction.Builder(meteringPoint, FocusMeteringAction.FLAG_AF)
.setAutoCancelDuration(AF_AUTO_CANCEL_TIME_MS, TimeUnit.MILLISECONDS)
.build()
val focusMeteringFuture = cameraControl.startFocusAndMetering(meteringAction)

I am using CameraX 1.3.0 and a Pixel 6 for testing and I'd at least want my manual focus to be as good as the auto focus results, I've tried adding multiple focus points to the builder but I'm still getting the same results.

Thanks in advance

Eino-Ville Talvala

unread,
Feb 21, 2024, 1:42:56 PMFeb 21
to Javier Gerardo Martinez Salomon, Android CameraX Discussion Group
Hi Javier -

When you say 'AF struggles when there's a change', is this when you're just running normal autofocus, with no use of FocusMeteringAction? Or other adjustments to CameraX's focus settings?
If so, that's not CameraX's autofocus, that's just the device's own autofocus routine running in continuous mode.

If you've issued a FocusMeteringAction already, then note that once the focusing completes (that is, after you receive the listenable future returned by startFocusAndMetering), it is locked, and won't respond to scene changes unless you start another manual focus scan or cancel the action via cancelFocusAndMetering (or the lock auto-times out, if you have that enabled).

How close are you holding the ID to the phone? There are minimum focus distances involved; generally without a macro mode a smartphone camera can't focus closer than about 10 cm. If you need to have more magnification than allowed by a > 10 cm distance, you need to use digital zoom.

In general, multiple focus points will not help you, and are generally not supported by most phone's camera hardware anyway.

- Eddy



--
You received this message because you are subscribed to the Google Groups "Android CameraX Discussion Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to camerax-develop...@android.com.
To view this discussion on the web visit https://groups.google.com/a/android.com/d/msgid/camerax-developers/544fa005-8b75-4762-8838-18aa52fabd93n%40android.com.

Javier Gerardo Martinez Salomon

unread,
Feb 23, 2024, 6:31:59 PMFeb 23
to Android CameraX Discussion Group, etal...@google.com, Android CameraX Discussion Group, Javier Gerardo Martinez Salomon
Hi Eddy,


> When you say 'AF struggles when there's a change', is this when you're just running normal autofocus, with no use of FocusMeteringAction? Or other adjustments to CameraX's focus settings?
It's when running the normal autofocus routines from the continuous AF mode, and I've encountered this in some devices like the pixel 6 when changing the height I'm holding the phone over the document, for example, when going from a 25cm distance to a ~12cm distance the AF routine can't re-focus automatically unless I issue a manual FocusMeteringAction

> If you've issued a FocusMeteringAction already, then note that once the focusing completes (that is, after you receive the listenable future returned by startFocusAndMetering), it is locked, and won't respond to scene changes unless you start another manual focus scan or cancel the action via cancelFocusAndMetering (or the lock auto-times out, if you have that enabled).
This is a good piece of info, the cancelFocusAndMetering is set to 5 seconds by default, so I guess that would explain why once the time runs out and the normal AF takes over the focus can get worse. I've found that indeed the AF_STATE is set to FOCUSED_LOCKED for the time cancelFocusAndMetering is set to and then goes into PASSIVE_SCAN.

> How close are you holding the ID to the phone? There are minimum focus distances involved; generally without a macro mode a smartphone camera can't focus closer than about 10 cm. If you need to have more magnification than allowed by a > 10 cm distance, you need to use digital zoom.
It's usually different per device but the minimum distance is close to 12cm.

My testing strategy is that I would normally rely on the continuous AF functionality to get a sharp picture, if I detect that the image is not sharp enough then I issue a FocusMeteringAction, It is assumed that the object of interest is in the center of the preview so I target the center as well and use the default point size.
  • Does that focus strategy sounds reasonable? specially targeting the center and using the default point size? I've noticed that some devices focus on the center and the edges of the object become slightly blurred. Increasing the point size has not worked for me but I'm curious if it's a best practice across devices in general.
  • Is there any camera property that I can query to determine if a device will be able to handle the focus at such a short distance?
  • Do you have any recommendation on properties/options to adjust or strategies to get a better focus or capture more detailed images?
Thanks

Scott Nien

unread,
Feb 27, 2024, 6:29:40 AMFeb 27
to Javier Gerardo Martinez Salomon, Android CameraX Discussion Group, etal...@google.com
Not sure if it helps, but you can try to set the MeteringPoint to represent the whole camera sensor by the following code snippet.  This will have the same metering region as the continuous default AF . 


MeteringPointFactory factory = new SurfaceOrientedMeteringPointFactory(1.0f, 1.0f);
FocusMeteringAction action =
new FocusMeteringAction.Builder(
factory.createPoint(/*center point x*/
0.5f, /*center point x*/ 0.5f, /* full width and height */ 1.0f), FocusMeteringAction.FLAG_AF ).build();

Javier Gerardo Martinez Salomon

unread,
Feb 28, 2024, 11:09:56 AMFeb 28
to Android CameraX Discussion Group, scot...@google.com, Android CameraX Discussion Group, etal...@google.com, Javier Gerardo Martinez Salomon
Thanks Scott,

I've tried that already and I'm curious about how the `size` parameter affects the focus, for a pixel 6, I've found that using a size between 0.1f and 0.5f gives me the same results, I can't notice an extended focus area despite the size being bigger, that said, the focused area looks very sharp and the edges tend to be softer. If I set the size to something bigger than, 0.5f, let's say 0.6f, then it does behave as you describe, I get similar results to those I'd get with the regular continuous AF routine, that is there's generally not a sharp image but the edges of the object aren't as soft as with the manual focus. All the focus points are set to the center of the screen, take a look at the following image that compares a manual focus at a size of 0.5f and 1.0f at the center so you can also observe the "soft edges" and "sharp center" effects.

What I'd like to know is:
  • Is the result of the manual focus for the pixel 6, which does not get bigger as the size increases and generates this sharp center with softer edges a hardware limitation?
    • If it is, is there a way to predict that this kind of results would be observed based on hardware?
  • Is there a way to know the point size that would make the manual focus transition from a specific region to a more general continuous AF region as observed with the pixel after 0.5f point size?
  • is it expected in general that the focus region increases as the point size increases for all devices or the behavior would be similar to the one of the pixel 6?
Thanks again!
focus_comparison.jpg

Scott Nien

unread,
Mar 8, 2024, 4:36:17 AMMar 8
to Javier Gerardo Martinez Salomon, Android CameraX Discussion Group, etal...@google.com
I did a test on both Pixel 6 and Samsung S24 Ultra,  and it seems the same Point size has different effects on these 2 devices. 
so my approach probably doesn't work to fix your issue. 

Reply all
Reply to author
Forward
0 new messages