CHDK Wiki
Advertisement

Landscape Focus Bracketing Scripts

Written for/on: G7X and G1X
Should also work on: any camera that runs CHDK
Requires CHDK build: Anything recent as of 2018

Latest version of this script may be downloaded from: https://gist.github.com/pigeonhill/3fe9e52621db7149eb3505bd60b395cb

Notes & Usage Unlike macro focus bracketing, where the focus step is the same each time and very small; in landscape focus bracketing we need to calculate the focus for each step. To get the full benefits from this script, it is useful to remember that, ignoring diffraction, at the near and far depth of fields the defocus blur is, by definition, the so-called Circle of Confusion. This blur is usually accepted as being 30 microns for a 35mm full frame camera, and scaled by the crop factor for other formats, eg 11 microns on the G7X.

Having said this, 30 microns, is not the 'best focus' quality but you will not see the differences on your Facebook images, only when you create high (focus) quality prints that will be scrutinised close up, eg in a competition. As you need a line pair (black and white pixels, to see a line, it is not sensible to consider CoCs less than, say, 2 sensor pixels.

Defocus blur is, of course, zero at the point of focus. Also, at the Hyperfocal distance (H), the blur at infinity will be the CoC. It is useful to note that any blur at infinity can be 'dialed in', by simply focusing at a distance either side of H. Thus, on my G7X, with a CoC of 11 microns, if I focus at twice H, then the infinity blur will be CoC/2. Likewise, at H/4, the infinity blur will be 4*CoC.

The script scales everything relative to H and the following chart illustrates how defocus blur varies with respect to H. Note the chart shows an estimate of the near and far DoFs that can be used in your head: the script uses a more ‘accurate’ estimate.

H

When focus bracketing we must at least ensure the previous image's far DoF is the same as the next image's near DoF: assuming we are focusing near to far as we are in this script. However, many wish to introduce some 'insurance' and have an overlap between the focus brackets. The script provides four options for this overlap: none, 2*CoC/3, CoC/2 and diffraction aware.

With diffraction aware overlap, the script calculates the defocus blur to use, that ensures that the defocus blur and diffraction blur, taken together in quadrature, are equal to the CHDK CoC. This overlap blur criterion will never be less than the CHDK set CoC/3.

The following illustrates the overlap between image n and n+1. Note that using the overlap feature comes at the cost of needing to take more brackets

Pict 2

Having achieved the perfect focus brackets, the script will then create an additional bracket beyond H, ie to cover the infinity focus quality. The options being 2H, 3H or 4H, giving infinity blurs of CoC/2, CoC/3 or CoC/4. If diffraction aware is being used for the overlap, the last image will be around CoC/3.

By default the script creates a dark image at the beginning and end of the focus bracket set, to help differentiate the sequence in post. You can turn this off via the menu.

At each focus point you can also ask the script to create two additional exposure brackets at either -xEv & +xEv, -xEv & --xEv or at +xEv and ++xEv. X can be 1 or 2Ev from the base exposure. These options cover all the usual exposure bracketing options, eg if following an ETTR approach, then a +/++ logic would be the best choice.

In addition to the 'normal' bracketing option, the script offers two additional schemes. First, an ISO-less bracketing option (at ISO 800 or ISO 1600) that assumes (sic) the camera is ISO-less above ISO 800 or 1600 (check your camera to ensure you select the right ISO so the sensor read noise swamps the banding). Thus there is no point in taking brackets in the camera above this, ie just push in post. The ISO option is good for hand-holding situations and the script assumes your base ISO is 100.

G1X G7X

As an illustration, the chart on the right, from the DXOMark site, shows the G1X vs G7X. As can be seen the G7X can be assumed to be fully ISO invariant, which means doing ISO bracketing is pointless, ie simply push a single base exposure in post (as long as you are not too underexposed ;-))

On the other hand the G1X is only ISO-less above about 1600. Hence take a single additional bracket at, say, ISO1600.

The second additional scheme makes use of the 'Zero-Noise' approach, as suggested by Guillermo Lujik. In this mode a second time bracket is taken at +4Ev from the first one. To use the ZN option, simply ETTR to capture the highlights and the script will create a +4Ev additional image for the shadow areas. But beware of flares from bright highlights

To use the script, simply focus on the nearest point of interest and place the camera in manual focus mode.

The script is started in the normal way, eg by doing a shutter press.

Focus Test

Here is a test image where I focused on our cat's mouse, at about a foot away from the camera, and used diffraction aware overlap. The script took 7 perfectly abutted F/4 focus images, accounting for diffraction, and two additional exposure brackets at each focus station, ie 21 images. The post processing was carried out in Lightroom, with a round trip to Helicon Focus.

Finally, you will find information about this script and other photography matters on my blog at photography.grayheron.net

.

.

Script Code Save to your /SCRIPTS/ folder as usual.

The latest version will always be here:

https://gist.github.com/pigeonhill/3fe9e52621db7149eb3505bd60b395cb

--[[
@title Get Focus Brackets
'Perfect' focus brackets from near to blur defined 'infinity': focus overlap defined in terms of defocus CoC
Plus option of two additional exposure brackets at each focus step, using various logic:1Ev, 2Ev or 3Ev using -/+ or -/-- or +/++; ISOs at 800 or 1600; or one additional 
exposure (Zero Noise) at 4Ev
Script is gauranteed to take three focus brackets, even if initial focus is behond the defocus hyperfocal
Use Bookends to help differentiate bracket set in post
Camera should be in manual focus mode and M mode

Rev 1.0
April 2019
Tested on a G7X
(c) Garry George

@chdk_version 1.5
# overlap = 0 "Overlap at" {CoC 2CoC/3 CoC/2 diff}
# delta = 0 "Exposure bracket delta?" {None 1Ev 2Ev 3Ev 800 1600 ZN}
# logic = 0 "Exposure bracket logic?" {0/-/+ 0/-/-- 0/+/++}
# delay = 3 "Script Delay (s)" [0 5]
# bookends = 1 "Bookends?" {No Yes}
# quality = 1 "Infinity focus quality?" {CoC/2 CoC/3 CoC/4}
--]]

capmode = require("capmode")
if (capmode.get_name() ~= "M") then
    print("Switch to M mode")
    return -- exit script
end

if get_focus_mode() ~= 1 then
    print("Switch to MF mode")
    return -- exit script
end

if (get_focus() == -1) then
    print("@ Infinity")
    return -- exit script
end

press("shoot_half") -- get current set camera values
t = get_tick_count()
repeat 
    sleep(10)
    if get_tick_count() - t > 5000 then
        print("Unknown Error")
        release("shoot_half")
        return -- exit script
    end
until (get_shooting())
release("shoot_half")
repeat sleep(50) until (not get_shooting())

    s = get_tv96()
    av = get_av96()
    set_tv96_direct(s)
    set_av96_direct(av)
    dof = get_dofinfo()
    x = dof.focus
    base_h = dof.hyp_dist -- that is without diffraction accounted for
    fl = dof.focal_length/100

x_start = x
last_x = x
temp = 0
temp1 = 0
temp2 = 0
ecnt = get_exp_count()

function myshoot() -- works with X_bracket function
    local prevcnt=hook_shoot.count()
    local rawprevcnt=hook_raw.count()
    press'shoot_full_only'
    repeat sleep(10) until prevcnt ~= hook_shoot.count()
    release'shoot_full_only'
    repeat sleep(10) until rawprevcnt ~= hook_raw.count()
end

function bookend()
    set_tv96_direct(960)
    set_av96_direct(640)
    ecnt = get_exp_count()
    shoot()
    set_tv96_direct(s)
    set_av96_direct(av)
    repeat sleep(100) until (get_exp_count() ~= ecnt)
end

function refocus(xx)
    set_focus(xx)
    sleep(500) -- may need adjusting for some cams?
end

function X_bracket()
    press("shoot_half")
    repeat sleep(10) until (get_shooting())
    set_tv96_direct(s)
    if delta == 0 then 
        myshoot()
    elseif delta < 4 and logic == 0 then
        myshoot()
        set_tv96_direct(s-96*delta)
        myshoot()
        set_tv96_direct(s+96*delta)
        myshoot()
    elseif delta < 4 and logic == 1 then
        myshoot()
        set_tv96_direct(s+96*delta)
        myshoot()
        set_tv96_direct(s+2*96*delta)
        myshoot()
    elseif delta < 4 and logic == 2 then
        myshoot()
        set_tv96_direct(s-96*delta)
        myshoot()
        set_tv96_direct(s-2*96*delta)
        myshoot()
    elseif delta == 4 then
        local iso = get_sv96()
        set_sv96(sv96_market_to_real(iso_to_sv96(100)))
        myshoot()
        set_sv96(sv96_market_to_real(iso_to_sv96(800)))
        myshoot()
        set_sv96(iso)
    elseif delta == 5 then
        local iso = get_sv96()
        set_sv96(sv96_market_to_real(iso_to_sv96(100)))
        myshoot()
        set_sv96(sv96_market_to_real(iso_to_sv96(1600)))
        myshoot()
        set_sv96(iso)
    elseif delta == 6 then
        myshoot()
        set_tv96_direct(s-96*4)
        myshoot()
    end
    set_tv96_direct(s)
    release("shoot_half")
    repeat sleep(10) until (not get_shooting())
end

h = base_h -- no overlap case: brackets 'touch' at CHDK CoC

if overlap == 1 then -- adjust h to achieve the requested overlap, ie 'touching' at 2*CoC/3 or CoC/2 or at the diffraction aware defocus blur
    h = (h*3)/2
elseif overlap == 2 then
    h = 2*h
elseif overlap == 3 then -- use diffraction aware overlap: Assume CHDK CoC is total blur, formed in quadrature from defocus and diffraction blurs
    temp1 = 1000*dof.coc -- total blur set up for imath
    temp2 = (1342*dof.aperture)/1000 -- set up for imath. From diff_blur = 2.44*0.55*N (um)
    if temp1 > temp2 then -- can use diffraction aware overlap
        temp = imath.sqrt(imath.mul(temp1,temp1) - imath.mul(temp2,temp2)) -- diffraction aware defocus blur
        temp2 = imath.div(temp1,temp)
        if temp2 > 3000 then 
            h = 3*h
        else
            h = (h*temp2)/1000
        end
    else -- diff blur > total blur
        h = 3*h
    end
end

-- Main section

sleep(delay*1000) 

if bookends == 1 then bookend() end

while (x < h/3) do -- capture the focus brackets up to h/3
    refocus(x) -- move to x
    X_bracket()
    dof = get_dofinfo()
    x = dof.focus -- register actual x moved by cam
    last_x = x
    x = (x*(h*10 - 2*fl))/(h*10 - 2*x*10) -- position of next focus bracket
    if x <= last_x or x <= 0 then 
        print("Unknown Error")   
        refocus(x_start)
        return
    end   
end

refocus(h/3) -- take shot at h/3
X_bracket()

refocus(h) -- take shot at h
X_bracket()

temp2 = h*(quality+2)
refocus(temp2) -- take shoot at infinity blur quaility
X_bracket()     

if bookends == 1 then bookend() end

refocus(x_start)

Advertisement