Instantly share code, notes, and snippets.

@pigeonhill /lbs.lua
Last active Jan 20, 2019

Embed
What would you like to do?
CHDK Landscape Bracketing
--[[
@title Landscape Bracketing
'Perfect' focus brackets from near to blur defined 'infinity', as a fraction of camera's CoC
Plus option of (3) exposure brackets at each focus step of 1Ev, 2Ev or 3Ev using -/0/+ or -/-- or +/++
Camera should be in manual focus mode
Release 1.71
Tested on a G7X
(c) Garry George
@chdk_version 1.5
@param p Exposure bracket delta?
@default p 0
@values p None 1Ev 2Ev 3Ev
@param j Exposure bracket logic?
@default j 0
@values j -/0/+ 0/-/-- 0/+/++
@param g Overlap at?
@default g 3
@values g CoC 2CoC/3 CoC/2 diff
@param n Max number of focus brackets?
@default n 10
@range n 4 30
@param c Script Delay (s)
@default c 3
@range c 0 5
@param b Bookends?
@default b 1
@values b No Yes
@param q Infinity focus quality?
@default q 1
@values q CoC/2 CoC/3 CoC/4
@param v Show bracket pos?
@default v 1
@values v No Yes
@param k Create Log?
@default k 1
@values k No Yes
--]]
set_mf(1) -- just in case
dof = get_dofinfo()
x = dof.focus
x_start = x
last_x = x_start
s=get_tv96()
base_h = dof.hyp_dist
temp = 0
temp1 = 0
temp2 = 0
ok = true
error = false
fl = dof.focal_length/100
log ={}
count = 0
h = base_h -- no overlap: brackets 'touch' at CHDK CoC
if g == 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 g == 2 then
h = 2*h
elseif g == 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
print("Warning: High Diff: using CoC/3")
else
h = (h*temp2)/1000
end
else -- very high diffraction condition: diff blur > defocus blur
h = 3*h
print("Warning: Diff>Defocus: using CoC/3")
end
end
if x < base_h then
num = ((10*h*(base_h - x))/(2*x*base_h) + 5)/10
else
num = -1
ok = false
end
if num > n-2 then -- accounting for last 2 brackets for infinity focus
ok = false
end
function bookend()
if b == 1 then
set_tv96(960)
shoot()
set_tv96(s)
end
end
function refocus(xx)
local dis = 0
set_focus(xx)
repeat
dis = get_focus()
sleep(100) -- seems to work on G7X, may need increasing on other cameras
until dis == get_focus()
end
function X_bracket()
if p ~= 0 then
if j == 0 then
set_tv96(s-96*p)
shoot()
set_tv96(s+96*p)
elseif j == 1 then
set_tv96(s+96*p)
shoot()
set_tv96(s+2*96*p)
elseif j == 2 then
set_tv96(s-96*p)
shoot()
set_tv96(s-2*96*p)
end
shoot()
set_tv96(s)
end
end
-- Main Section
if x_start < base_h and ok then -- start bracketing
bookend()
sleep(c*1000)
refocus(x_start) -- explicitly refocus, just in case
dof = get_dofinfo()
x = dof.focus
last_x = x
if k == 1 then log[#log + 1] = "\n"..os.date() end
while (x <= h) -- capture brackets: use break to exit loop before h is reached
do
shoot()
X_bracket()
temp = get_exp_count().." @ "..x.."mm"
if v == 1 then print(temp) end
if k == 1 then log[#log + 1] = temp end
count = count + 1
x = (x*(h*10 - 2*fl))/(h*10 - 2*x*10)
if g == 3 then
if (x <= 0) or (x > h) then break end
else
if (x <= 0) or (x > base_h) then break end
end
if x <= last_x then
error = true
break end
last_x = x
refocus(x)
end
if not error then
if g == 0 then -- distances for the final two images (this if block provides the framework to use other logic)
temp1 = base_h
temp2 = (q+2)*base_h
elseif g == 1 then
temp1 = base_h
temp2 = (q+2)*base_h
elseif g == 2 then
temp1 = base_h
temp2 = (q+2)*base_h
elseif g == 3 then
temp1 = h
if h > 3*base_h then temp2 = h else temp2 = 3*base_h end
end
refocus(temp1)
shoot()
X_bracket()
temp = get_exp_count().." @ "..temp1.."mm"
if v == 1 then print(temp) end
if k == 1 then log[#log + 1] = temp end
count = count + 1
if temp2 ~= temp1 then
refocus(temp2)
shoot()
X_bracket()
temp = get_exp_count().." @ "..temp2.."mm"
if v == 1 then print(temp) end
if k == 1 then log[#log + 1] = temp end
count = count + 1
end
bookend()
temp = count.." focus brackets"
print(temp)
if k == 1 then log[#log + 1] = temp end
temp1 = ((10*dof.coc*base_h)/temp2 + 5)/10
temp = "Infinity blur "..temp1.."um"
if k == 1 then log[#log + 1] = temp end
if p ~= 0 and k == 1 then log[#log + 1] = "Exposure offset = "..p.."Ev" end
else
print("Unknown Error")
if k == 1 then log[#log + 1] = "Unknown Error" end
end
else
if not ok and num ~= -1 then
print("Warning")
temp2 = num + 2
print("^ max # brackets to "..temp2)
end
if num == -1 then
print("Beyond H")
refocus(x_start)
shoot()
X_bracket()
temp = get_exp_count().." @ "..x_start.."mm"
if v == 1 then print(temp) end
if k == 1 then log[#log + 1] = temp end
end
end
refocus(x_start)
if k == 1 and ok then
print_screen(-2306)
for i = 1, #log do
print(log[i].."\n")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment