Friday, November 24, 2017

Focus Bar Tutorial

As regular readers of my blog will know, over the past year or so I’ve experimented with various Magic Lantern Lua scripts. One of my original objectives was to create an auto focus bracketing script, which I did; although, in the end, this didn’t work out as expected, as ML ‘only’ controls the EOS lens in non-linear distances, and, as of today, ML still doesn’t know how to control EOSM lenses.

Because of these limitations, earlier in the year I decided on a different approach, namely create a manually controlled focus bracketing script: which has become my Focus Bar script (downloadable from the right).

Thus I’ve withdrawn (support for) the Auto Landscape Bracketing Script (at least for now).

The Focus Bar’s prime use is to help the user achieve optimum ‘infinity focus’ and provide information to inform his/her focus bracketing. This coupled to other ML features, eg auto exposure bracketing, allows the user full control of exposure and focus space.

Rather than ‘only’ using depth of field distance information, I decided to mainly make use of ‘blurs’; which. I know, has created some confusion. The reason being that the depth of field distances are based on a blur criterion and distances are not always tractable in the field.

As we know, by making a few simplifying assumptions, eg making use of the thin-lens equation and ignoring pupillary magnification; we can derive of some simple, but powerful, focusing relationships.

For example, the hyperfocal distance is derived through just knowing the focal length, the aperture and a variable that is usually called the ‘circle of confusion (CoC)’. The CoC is the largest defocus blur spot that is defined by the user as being acceptable. The smaller the CoC, the lower the total depth of field will be, ie near to far.

It is said that, for a 35mm DSLR, an acceptable (defocus) CoC, say, for on-screen presentation, is around 0.03mm, ie 30 microns. This needs to be adjusted for a crop sensor, eg for a Canon crop it would be 0.03/1.6.

The above, however, ignores the impact of diffraction. Lens defocus blur and diffraction blur represent the main components of image ‘softening’. Although defocus blur varies over the scene, ie increasing away from zero at the point of focus; diffraction blur can be assumed to be ‘flat’ across the image scene, ie near to far.

We estimate the total blur by combining the defocus blur and the diffraction blur; and the usual practice is to take these two in quadrature, ie root mean square them together. Thus, if we know the diffraction blur, which, for non-macro photography, is mainly a function of the aperture, we can work out the required defocus blur to achieve the total blur criterion, ie in our case the CoC set in the ML Focus menu.

If this sounds complex, you needn’t worry, as the Focus Bar handles all the equations.

I wrote the Focus Bar script mainly for landscape or cityscape photographers, or those that wish to get as much depth of field as possible from near-to-far, ie from a near point of interest to, say, infinity. Having said that, the Focus Bar can be used in any situation where the photographer wishes to understand depth of field. It is not, however, designed for macro use.

Without (sic) the Focus Bar we essential have two focusing strategies.

First, we can use the calculated hyperfocal distance (note the current ML supplied hyperfocal distance and depths of field should not be used until they are ‘fixed’ by the ML gurus: thus the Focus Bar switches off the ML reported DoFs) and accept that at infinity, ie in the far field, the defocus blur will only ‘just’ be acceptable. This is because, by definition, at the hyperfocal distance, the defocus blur at infinity is based on the blur criterion that we set, ie the CoC: which will change if we are accounting for diffraction (as we should be).

By the way, as most people using the Focus Bar will be landscapers, and likely using a wide angle lens, it is worth remembering that ‘optical infinity’ is pretty close to you (see below).

Secondly, we could adopt the alternative strategy of focusing at ‘infinity’, or at the furthest point of interest (assuming we can resolve focus on our LV, say). This strategy will ensure the far distant part of the image will be as sharp as possible, ie the defocus blur is zero at the point of focus; but as we know the near depth of field will be compromised, ie positioned at or close to the hyperfocal distance [that is, the near depth of field of a lens can never be farther than the hyperfocal distance away, assuming you don’t over-focus ‘past infinity’ as some lenses allow].

We also know that for digital sensors, with discrete pixels, blurs less than two pixels are rather meaningless, ie you can’t resolve a line-pair (black line + white line) in one pixel: as minimum you need two pixels. Thus for the 5D3, say, with a pixel size of about 6.3 microns, this means defocus blurs less than about 12 microns are rather meaningless.

We therefore have a zone of (sensible) focus to aim for; that is between the hyperfocal distance, based on a ‘just acceptable’ focus criterion, and a distance where the defocus blur at infinity is two sensor pixels (which you set in the script to suit your camera).

The Focus Bar provides all the information to inform your focusing. That is, once you move past the hyperfocal distance, the Focus Bar shows the defocus blur (green bar, for above the 2 x pixel limit, or red bar for below the 2 x pixel limit), the diffraction blur (yellow bar) and the total blur (blue bar). All referenced to the total CoC blur criterion that you set in ML, ie the white bar.

The horizontal Focus Bar also gives a (grey scale) visual impression of the focus field, including, at the 1/10 points between the near and far depths of field, the defocus blurs. White means zero blur, black means the blur is the ML set (total CoC) criterion, and grey is anything in-between.

Finally, if, in the infinity focusing mode, the near depth of field, based on the defocus blur at infinity, is also shown on the left of the focus bar.

Although the above is a major step forward in focus control, the Focus Bar has more to offer. The most important feature, beyond the infinity focus control, is the ability to inform focus stacking or bracketing.

Landscape focus stacking is more complex that macro focus stacking. In macro focus stacking the distance you move from focus point to focus point is not only small, ie millimetres or less, but essentially invariant. Thus, if you need to cover, say, a scene of x mm, then you can simply calculate the depth of field, say y mm, at your point of focus and aperture, and divide this into the scene’s depth of field, accounting for a sensible overlap. When macro focusing the near and far depths of field are equal, so focus bracketing is a very simple, but laborious, matter.

Using the Focus Bar, having taken your first image, say using the FB’s infinity blur feedback, you now move the focus away from infinity and see two new pieces of information. First,if focus stacking is enabled (see below), a purple area appears on the horizontal focus bar, which represents the amount of overlap between the current depth of field and the last image. Secondly, the near depth of field distance (top left) is augmented with the defocus blur at the point of overlap: for example if you are presented with “(5)”, this means the at the focus overlap point, between the last and current, images, the blur is 5 microns, ie a very healthy overlap. The following two charts illustrate the depth of field and overlap blurs.


It is up to you to decide how much overlap you want. The less overlap, the lower the number of images you will need. The more the overlap, the ‘sharper’ the merged image will be through the scene, ie the defocus blur from one image to the next will never be more than the overlap defocus blur (other than at the current near depth of field distance, where it will be the defocus blur based on what you set as the CoC in ML).

If in focus stacking mode, an additional piece of information is also presented, namely the minimum number of images needed to focus stack from where you are to the hyperfocal, assuming no overlap, ie focus stacks just touching.

Enough of the words: let’s look at the Focus Bar via a few screen shoots.

The first port of call after placing the FB script in the Lua folder, is to enable the script’s auto startup. The only other necessity is to ensure the total CoC, ie total blur, is set in the ML Depth of Field Menu. Note that the FB switches off the ML depth of field display, as all DoF information is provided by the Focus Bar. Also, you can change the start-up defaults in the scripts, eg the default is that the focus bar is not displayed, that focus stacking is off and that the focus bar is in ‘depth of field’ mode. For more information I encourage you to read the FB script’s header, ie open it in a .txt editor.

Having switched the FB auto start up on, from the ML Scripts menu, and run the FB script or switched the camera off and on, the FB will auto load each time the camera is switched on. Having said that, when you next switch on the camera the Focus Bar will not be visible, as this is the default. To make the FB visible simply do a 3+ second half shutter press. Repeating this will toggle the FB display on and off.

If you do a 6+ second half shutter press, then the FB will be enabled in an enhanced mode that shows additional information when infinity focusing, ie when focusing past the hyperfocal distance. This addition info shows the near DoF distance when infinity focusing, for three blur configurations: the near DoF using the infinity defocus blur; twice the infinity defocus blur; and the defocus blur using the ML set CoC (blur), as corrected for diffraction if it is on (as it should be). The next two images show the FB screen, when the infinity focusing, in both modes:



So what information are we seeing above? First we get a visual breakdown of the relative blurs at the focus distance we’re at. Because we see blur information, we know we are past the hyperfocal distance and less than the lens reported infinity.

The white bar’s height represents the CoC value that we set in ML. That is the total blur that we wish to tolerate, ie made up of defocus and diffraction (if set) blurs.

The green or red vertical bar represents the defocus (only) blur. Green if the blur is greater than two camera pixels and red if it is less than two camera pixels. Remember the defocus blur does not account for diffraction, it ‘just’ represents the blur caused by the lens optics.

The yellow bar shows the diffraction blur, which is simply a function of the aperture number. That is the diffraction at F/16 is twice that at F/8. This does not change with focus position (but would if we were in the macro zone, ie magnifications approaching or greater than unity.

The blue bar shows the total blur at infinity, made up of the defocus blur and the diffraction blur, taken in quadrature.

At the bottom of the screen, in brackets, we see three numbers. The middle number is the defocus blur that meets the total, ML-set, blur criterion. The other two numbers, either side, represent the near and far DoFs of the last image taken, if in focus stacking mode: which will be zero when the camera is first switched on or when the focus stacking is reset (see below), ie as no image has been taken.

When ‘infinity focusing’, ie focusing past the hyperfocal distance, you should use the infinity blur to decide when to stop focusing. If you are only showing an image on the PC screen, then a total blur (blue bar) of, say, 30 microns (assuming this is the number you set in ML), will likely be OK. If on the other hand you are printing for close scrutiny, ie in a competition, then a blur closer to the minimum, ie 2 x sensor pixel size, might be indicated. For example, on my 5D3 the minimum blur will be around 12 microns.

Once you have decided the infinity blur you want to use, you can simply read off the near depth of field distance on the focus bar. In simple mode, the near depth of field distance is that which satisfies the infinity defocus blur. This is why it is sometimes better to use the FB in the more advanced mode, ie showing the additional depth of field information. For example in the above we see that the infinity total blur is 16 microns and that the depths of field that satisfy the infinity defocus, twice the infinity defocus and the ML-CoC-defocus are 64cm, 43cm and 57cm respectively.

If the near depth of field is not sufficient, than you either need to back off the infinity blur or take some focus brackets.

If focus stacking is enabled, once you take your first image, the depth of field info at the bottom of the screen gets updated to be the depth of field of that image. If you have focus bracketing switched off, then the DoF information at the bottom of the screen (dynamically) shows the DoF at the point of focus.

If you then decide to take a focus bracket, and (for example) assuming your first image was at infinity, you will move the focus away from infinity. As you do this the focus bar will show (purple) the focus overlap between the current depth of field and the last depth of field. That is, assuming you are moving away from inifinity, the overlap between the last near DoF and the current far DoF.

Two pieces of additional info will also appear whenever the purple is showing, ie whenever you are focus overlapping with the last image. These appear to the right of the near DoF and represents the defocus blur at the overlap and the (minimum) number of brackets to the hyperfocal, eg “near-DoF (overlap_blur)  [number_brackets]”.

Once you decide you have reached the ‘best’ overlap condition, simply take an image, following which the FB will update ready for another focus bracket if you need it.

Before looking at the Focus Bar in operation, let’s do a final reminder of the defocus and diffraction blurs, and what the depth of field ‘looks like’.

As already said, for non-macro work, we can treat the diffraction blur as a softening of our image that occurs equally in all places, ie irrespective of the distance into the scene. The diffraction blur only varies with aperture, ie the F-number. The smaller the aperture, ie the larger the F-number, the more diffraction blur we will get.

Defocus blur, however, varies throughout the scene, near-to-far, and varies according to four user controlled variables:

  • Focus Distance 
  • Focal Length of the lens 
  • The Aperture of the lens, ie the F-number 
  • The so-called ‘circle of confusion’ or CoC
The CoC is simply the largest blur circle that we can tolerate before the image looks too soft in the areas where it shouldn’t. Essentially, defocus blurs less than the CoC look in-focus and defocus blurs larger than the CoC look out of focus. But note, there is not a sharp line that differentiates in and out of focus: it is strong function of our eye-brain system, as well as our viewing distance and the image’s (presentational) scale.

Having said that, a reasonable starting place, at least for a full frame DSLR, is to assume a total blur (nee total CoC) of around 0.03mm.

To ge
t an understanding of the lens defocus blur, I can recommend Heiko Kinzel's cBlur tool: http://www.cblur.org/en/, which has a two camera mode to allow A-B comparisons. There are some limitations in the web version, eg you can’t choose your own CoC, but as we are simply trying to understand defocus, these shortcomings can be ignored.

Let’s assume we have a full frame DSLR with a pixel size of 6.3 microns, eg the 5D3. We can dial that into cBlur as our first user input. The web version of cBlur allows you to choose between two CoC values. One based on the ‘industry standard’, ie 29 microns (0.0288mm in the example below); and one based on the sensor limit, ie two times the sensor pixel size, ie 12.6 microns in our case. Let’s use the 29 micron CoC for now and switch off (cBlur) diffraction. Let’s also assume a 16mm lens, with an aperture set to F/8 and ask cBlur to focus at the hyperfocal. The defocus field looks like this:


As cBlur plots blurs relative to the CoC, ie 29 microns in our case, a blur of 2 means 2x29 microns and a blur of 0.5 is 29/2 microns.

The above plot, once again, clearly shows the defocus field is non-linear and varies dramatically through the scene. By definition, the acceptable depth of field lies between where the blur is less than 1, ie less than the total CoC criterion.

Obviously, as we know, at the point of focus the blur is zero.

The key take away from this plot is to emphasize that as your point of interest moves away from the point of focus, the near and far defocus fields behave differently. The near falls off rather sharply and there is not much depth of field in front of the focus point, ie relative to behind it. The far defocus collapses to an asymptotic value.

[As an aside, once you have the Focus Bar up and running, it is useful to simply play around with the various camera settings and watch the impact on focus. For example, you will see that as you approach focus towards the macro end of a lens, the depths of field either side of the point of focus approach being equal.]

In the above example, where we are at the hyperfocal point, the defocus blur in the far field is never more than the CoC, ie a relative blur of unity in cBlur.

For the landscape photographer, who will likely be seeking maximum sharpness near to far, the implication is, that focusing at the hyperfocal means that the majority of your scene will only ‘just’ be in focus: based on the CoC you have chosen; and unless you are doing the hyperfocal calculations yourself, most standard hyperfocal tables assume a CoC of around 30 microns (full frame). That is good enough for screen presentation, but not for printing and/or close scrutiny.

As stated above, the sweet zone for defocus blur is between the ‘just acceptable’ (screen-based) CoC and the sensor limited blur, ie twice the sensor pixel size or pitch.

Note: if we accept that around 30 microns is our ‘worst case’ blur, ie total CoC, then the defocus blur or CoC needs to account for diffraction. Thus we really have a defocus sweet spot between SQRT[Total_CoC^2 – Diffraction_blur^2] to [2 x Pixel size]. But let’s continue to ignore diffraction for now. The illustration above explained the impact of diffraction on depth of field, eg:



Another useful question to ask is where is infinity? This may appear a stupid question, as infinity is, well, infinity. However, from a photography perspective, according to the lens being used, is it 100m away, 1000m away or 10,000m away etc.

If we accept that we can’t resolve a line-pair (one pixel of white and one pixel of black) when the blur is less that two pixels, then this conveniently becomes a working definition of infinity. In our example above, when the defocus blur criterion we have chosen (irrespective of diffraction softening) reaches twice the sensor pixel size, ie around 12-13 microns in the above (5D3) example, we are essentially at infinity.

In general, a reasonable working estimate of infinity is provided by the following: [(Focal_Length^2)/(Aperture_Number*2*Sensor_Pitch)]. Thus for a 5D3 at F/8 and with a 16mm lens, our working infinity = (16*16)/(8*2*0.0063) = 2540mm, say any distance past 3m. For a 70-200mm lens at F/8 and set at 70mm, the working infinity is (70*70)/(8*2*0.0063) = 48611, ie any distance past, say, 50m: at F/8 and 200mm it is (200*200)/(8*2*0.0063), ie some 400m.


The above chart illustrates working infinity idea, ie once the infinity blur has reached 2 x sensor pitch, ie about 0.4 relative blur on 5D3. Which is why we should remember the sagely photography advice of Buzz Lightyear: ‘to infinity, but not beyond!’

Enough about blurs, let’s finally look at the Focus Bar and how to use it, by looking at a specific use case, namely wishing to maximise the focus over a large depth of field, larger than can be achieved in a single image.

There are essentially two ways to approach this: from near-to-far or from far-to-near.

Although there is no right or wrong approach here, I slightly favour the ‘near-to-far’ strategy, for the simple reason I can either use AF or zoom-in LV to ‘nail’ the near (first) bracket’s focus and let the Focus Bar help me get the rest of the focus bracket set.

As an aside, this is why it is best to nail the shadow exposure first when bracketing, ie to meet your needs, and let the ML auto bracketing handle the number of brackets to ensure the highlight end is captured. Rather, say, than let ML auto bracketing handle both the shadow and highlight end, ie outside of your control.

Thus, I would select the near point of interest that I wished to be tack sharp and set focus on that, having also enabled the Focus Bar.

Having taken this first image, or exposure bracket set, the Focus Bar now provides the ability to optimally get the remaining focus bracket set.

First, at the top left, in square brackets, to the right of the near depth of field distance, we see the estimated (sic) number brackets that will need to be taken from where you are currently focused to the hyperfocal distance, based on the diffraction-informed ML-CoC set defocus blur. This number will always/only appear if the current focus point is less than the hyperfocal distance that meets the ML-CoC informed defocus blur criterion.

If you thought you might like to use a more stringent focus criterion, eg half the defocus blur related to the ML-CoC, then it is a simple matter to estimate this, as the number of focus brackets is linearly related to the (infinity) blur criterion. Thus, if you wanted to adopt a smaller (overlap) blur, eg instead of an ML-CoC defocus blur of B, use B/2; simply double the number of focus bracket. A B/3 means triple the number.

Note: the Focus Bar doesn’t know when you are starting a focus bracketing sequence, or what overlap criterion you are using, so you will need to interpret the [number of brackets] information, or ignore it. As a minimum is gives you an impression of the focus bracketing ‘burden’.

Also, you can toggle the focus stacking on or off by moving the lens to infinity and doing a 3+ second half shutter press. By default, focus stacking is switched off. 

Having taken the first, near field, bracket and knowing the number of brackets to take, at least the minimum number, it is now a simple matter to refocus towards infinity and use the purple bar and the overlap blur information (the middle number presented on the left of Focus Bar).

Once you have reached the next focus condition and taken the next image, simply repeat the process until you reach the infinity focusing domain, ie focus distance greater than the hyperfocal distance, where the Focus Bar infinity blur breakdown is presented, thus giving you the information you require to decide where the last bracket is taken.

Note that, as you take your focus brackets, the distances at which you are taking each image is of secondary importance. Your focus decisions should be based on blurs, either the overlap (defocus) blur or the infinity (defocus) blur.

This post is the longest one I have made, however, I hope this tutorial has helped demystify the focus bar approach to focusing. Although the focus bar makes a few assumptions, eg thin lens theory, it represents a very powerful tool for those that wish to maximise the focus quality in their images.

2 comments:

  1. I was running version 2.21 of Focus Bar - it worked fine. I upgraded to version 2.97 of the script - it loads but nothing happens in the user interface,no focus bar ,no nothing.Any ideas as to why? Does this new version depend on other newer features in ML?

    ReplyDelete
  2. I think I know what has happen, but in general there is always a risk with Lua scripts. The general risk is, as the ML Lua gets updated, a script will get out of kilter with Lua, as I can't control what version of ML you are using.

    I can confirm that the Focus Bar works with the latest Lua Fix version, ie Branch: lua_fix Latest Build (2017-11-24 20:43): which you download from http://builds.magiclantern.fm/experiments.html

    But I think the problem you are having is simply that the FB now defaults to being hidden or 'off'.

    To toggle FB on and off (as stated in the tutorial above) simply do a 3 sec half shutter press (you will see the led blink once). If you do a 6 sec press FB will be in the 'enhanced mode', ie showing more DoF info.

    Finally, by default, the focus stacking feature is off, ie you wont see the purple bar. To toggle it on and off, simply move the focus to infinity, ie infinity being reported by ML, and do a 6 sec half shutter press (the led will blink 3 times).

    Don't hesitate to come back with any other questions or suggestions to make FB better.

    ReplyDelete