DOF-based focus stack script NOT based on CHDK DOF calculations - Script Writing - CHDK Forum

DOF-based focus stack script NOT based on CHDK DOF calculations

  • 15 Replies
  • 22117 Views
*

Offline DBgit

  • *
  • 14
Advertisements
Here is a script I wrote to make focus stacks based on depth-of-field (DOF) with my S5IS camera. I thought I would share it with you in case it could be useful to someone...  The script was written for S5IS and should also work as is for S2IS and S3IS. Minor modifications would be required to make it work with other cameras (see below).
 
I know that many of similar scripts are available around but I could find none that works correctly for my camera. All of the DOF-based scripts I could find (i.e., scripts that use real DOF calculations to make the focus stack rather than proposing fixed focus steps) rely on CHDK calculations of DOF. The problem is that CHDK calculation of DOF is broken on S5IS (and probably other cameras) at focal lengths higher than ~50mm (e.g. DOF calculated is always “inf”).

The script first calculates and displays the optimal number of shots required to get every part of an object in good focus. The user needs only to input the desired nearest and furthest distance to be in focus. These can be determined easily by displaying the subject distance in the DOF calculator (CHDK menu - OSD param - DOF calculator). The script extracts the aperture and focal length and uses these values to make the DOF calculations (without using the commands “get_dof”, etc.). For each shot, the focus distance is increased by the calculated DOF. For the first shot, the distance is increased so that the edge of the region in focus corresponds to the initial position requested. For example, at focal length 6mm and F5.6 (with a coc of 5 um), the script takes 7 shots to get everything in focus from 0.1m to 10m. The shots are taken at 107mm, 127mm, 157mm, 204mm, 290mm, 507mm and 2000mm. You can verify that these shots are taken at the optimal positions to get everything in focus by looking at an online DOF calculator.
 
The script is not directly compatible with all cameras because, as far as I know, there is no universal CHDK script function to get the focal length. There is a small section of the script that is clearly identified where changes must be made. Basically, the relation between the function get_zoom and the actual focal length must be known. 
Notes: 
•   Camera must be in Av or M mode and Manual focus must be active. The script will try to set Manual focus automatically if it was not active (tested on S5IS).
•   Make sure to disable the option -Safety MF- in Canon menu. It is also better to set continuous shooting mode off, review off, and flash off.
•   The script works both with and without a close-up lens (e.g. DCR-250). I indeed use this script mostly for macro shots with a close-up lens where focus depth is very small (see the example in next post).
•   A typical value for the circle of confusion is about 30um/(crop factor). Thus, for S5IS (crop factor = 6), a CoC of 5 um should give good results. Smaller CoC increases focus overlap and number of shots, while higher CoC decreases number of shots but can lead to regions not in perfect focus.
•   A log file is saved to CHDK/LOGS/LOG_0179.txt.
« Last Edit: 12 / October / 2011, 21:19:52 by DBgit »

*

Offline DBgit

  • *
  • 14
Re: DOF-based focus stack script NOT based on CHDK DOF calculations
« Reply #1 on: 11 / April / 2011, 22:49:03 »
Here is an example of a stack taken with this script. This stack consists of 67 images (!) taken with a S5IS camera at 72 mm (12x) and F7.1 with a Raynox DCR-250 close-up lens (8 diopers). The subject was a small fly of about 1cm x 1cm (... and yes this poor guy was dead...).

Here is what the final stack looks like after processing (made using CombineZP)



High resolution is available version is available here.

Here is a 100% crop view from the center of the image:


Another great thing about focus stacks made with numerous images is that you can extract cool 3D views from your subject. Here is a video showing a rocking view of the fly.
http://www.youtube.com/watch?v=XpaR3pSWf0I#

Finally here is a movie showing each of the shots taken for this focus stack. We see how thin the depth of field is at high focal length with a macro lens.
http://www.youtube.com/watch?v=3JeuXTVndjc#

A high resolution example of one of the frame of the stack is available here.


« Last Edit: 11 / April / 2011, 22:59:12 by DBgit »

*

Offline SkyWalker9

  • ****
  • 301
  • SX20 IS (fw 1.02b)
Re: DOF-based focus stack script NOT based on CHDK DOF calculations
« Reply #2 on: 12 / April / 2011, 00:18:46 »
The SX20 uses the same PropCas for MF, so looks like I'll be trying this script this weekend when I get a chance. Thanks for sharing!

PS. Excellent shots!
« Last Edit: 12 / April / 2011, 00:39:17 by SkyWalker9 »

Re: DOF-based focus stack script NOT based on CHDK DOF calculations
« Reply #3 on: 12 / April / 2011, 04:08:19 »
SDM has this function built-in, no script is needed, just focus on the nearest subject and press the button.

See 'Automatic capture of images from focused distance to Infinity' at http://stereo.jpn.org/eng/sdm/capture.htm.

A log file of focused distances may also be produced.

Do you know if it works with S5is ?


David

*

Offline DBgit

  • *
  • 14
Re: DOF-based focus stack script NOT based on CHDK DOF calculations
« Reply #4 on: 12 / April / 2011, 20:55:28 »
The SX20 uses the same PropCas for MF, so looks like I'll be trying this script this weekend when I get a chance. Thanks for sharing!

PS. Excellent shots!

Even if the SX20 uses the same propCase for MF, you will still need to change the part of the script calculating the focal length  because the SX20 and S5 do not share the same lens. You just need to take the value from the get_zoom function and transfer it to focal length. This can be done by looking at the focal length table of your camera in the source code.

For the SX20, you should use the following table (with linear interpolation between the points) .
{result of get_zoom, focal_length in micron},
{   0,   5000 },
{  11,   6190 },
{  41,  12610 },
{  64,  25240 },
{  86,  45470 },
 { 105,  65880 },
 { 128, 100000 },

Also, note that I used (focal length in mm)*10 for the calculations in the script.
Good luck!

*

Offline DBgit

  • *
  • 14
Re: DOF-based focus stack script NOT based on CHDK DOF calculations
« Reply #5 on: 12 / April / 2011, 21:40:02 »
SDM has this function built-in, no script is needed, just focus on the nearest subject and press the button.

See 'Automatic capture of images from focused distance to Infinity' at http://stereo.jpn.org/eng/sdm/capture.htm.

A log file of focused distances may also be produced.

Do you know if it works with S5is ?

David

Yes, I am aware that SDM has this function, but I did not test it and do not know if it works on S5IS. I however assumed that SDM is based on CHDK source code and that it would use the same code for the calculation of the DOF... Let me know if it is not the case...

BTW, I took a deeper look at the values of DOF given by CHDK. I think I found what is the problem by playing with a SX30IS of one of my friend. The SX30IS shows exactly the same bug in DOF calculation as the S5IS, so this might be a bug affecting all cameras. I found that the DOF calculations are accurate until the value of the hyperfocal distance reaches about 65536 (2^16). This occurs only at focal length higher than about 50 mm for compact cameras. After that, the value returned by the function get_hyp_dist becomes negative (16 bit int overflow...). This is a problem because, in the source code (/trunk/core/gui_osd.c), the near and far limits of DOF are calculated from the value of the hyperfocal distance. This explains why the CHDK DOF calculation returns the value "inf" for near and far limits at a focal length higher than 50mm. Note that, as the focal length is increased further, the value of hyperfocal distance eventually goes back to positive again (around a focal length of about 65mm). The values of near and far limit of DOF are then not returned as "inf" anymore, but they are clearly not good anyway. For SX30IS, the hyperfocal distance goes back and forth from negative to positive 5 or 6 times when the camera is zoomed in from 4.3 mm to 150.5 mm.

Anyway, let's hope that someone more knowledgeable (and with more time) than me can correct this bug! :)
« Last Edit: 12 / April / 2011, 21:41:48 by DBgit »

Re: DOF-based focus stack script NOT based on CHDK DOF calculations
« Reply #6 on: 13 / April / 2011, 05:09:03 »
Yes, I am aware that SDM has this function, but I did not test it and do not know if it works on S5IS. I however assumed that SDM is based on CHDK source code and that it would use the same code for the calculation of the DOF... Let me know if it is not the case...

That is not the case.

All you need to do is temporarily replace DISKBOOT.BIN with the SDM version.

If I can build-up the enthusiasm I will ask an S5IS owner to test it sometime.

*

Offline SkyWalker9

  • ****
  • 301
  • SX20 IS (fw 1.02b)
Re: DOF-based focus stack script NOT based on CHDK DOF calculations
« Reply #7 on: 13 / April / 2011, 23:10:38 »
Even if the SX20 uses the same propCase for MF, you will still need to change the part of the script calculating the focal length  because the SX20 and S5 do not share the same lens. You just need to take the value from the get_zoom function and transfer it to focal length. This can be done by looking at the focal length table of your camera in the source code.

For the SX20, you should use the following table (with linear interpolation between the points) .
{result of get_zoom, focal_length in micron},
{   0,   5000 },
{  11,   6190 },
{  41,  12610 },
{  64,  25240 },
{  86,  45470 },
 { 105,  65880 },
 { 128, 100000 },

Also, note that I used (focal length in mm)*10 for the calculations in the script.
Thanks for the table! After recoding the script for the SX20, I tested both versions of the code (S5 & SX20) by looping through all 129 zoom steps and then charted the S5 & SX20 results on the same chart using Excel. They very closely paralleled each other (never crossing each other) so I hope to test the modified script on my SX20 sometime this weekend. Thanks for your help!

*

Offline Nando

  • *
  • 16
Re: DOF-based focus stack script NOT based on CHDK DOF calculations
« Reply #8 on: 12 / June / 2011, 18:28:21 »
Hi DBgit,
thanks for the scripts and the videos!

Did you read my private message?
I've modified your script a bit and would like to imagine it in CHDK-DE.

It works well with CHDK-DE. I have tested it.
- focus on a motive
- focus little reduced
- start of the script
- focus series behind the motive or infinite focus


fokus_stacker.lua
Code: (lua) [Select]

--[[
 
@title Fokus-Stacker-LUA
 
]]
 
print_screen (100*get_time("m")+get_time("s"))
 
 
press( "shoot_half" )

e = get_dofinfo()
 
f = e.focal_length/100
q = 65535

n = ((e.hyp_dist/(2*e.focus)-e.hyp_dist/(2*q))*e.focus)/((10*e.focus-f)/10)+1

print ( "Startfokus: " ..  e.focus )
print ( "Fotos: " ..  n+1 .. ", F" .. e.aperture/1000 .. ", " .. e.focal_length/1000 .. "mm" )
print ( " " )
sleep (5000)
print ( "Fokus " .. "Ziel-Fokus " ..  "DoF-Ziel" )

shoot ()


for i=1,n do


    h = e.focus+e.dof
    p = e.dof
    focus = e.focus
   
    if h > e.focus then
      set_focus (h)
      shoot ()
    end
   
    e = get_dofinfo()

    focusdiff = e.focus-focus

   if i==n and e.focus_valid==false then
    set_focus (65535)
    shoot ()
    e = get_dofinfo()
    print ( "" .. focus .. "  " .. h .. "-".. e.focus .. "  " .. p .. "-" .. focusdiff )
   end
   
    print ( "" .. focus .. "  " .. h .. "-".. e.focus .. "  " .. p .. "-" .. focusdiff )

end
    cls()
    exit_alt ()
    sleep(1000)

bye,
Nando
Canon Powershot SX200 IS

*

Offline DZK

  • *
  • 1
Re: DOF-based focus stack script NOT based on CHDK DOF calculations
« Reply #9 on: 28 / September / 2011, 09:35:47 »
The SX20 uses the same PropCas for MF, so looks like I'll be trying this script this weekend when I get a chance. Thanks for sharing!

PS. Excellent shots!

Even if the SX20 uses the same propCase for MF, you will still need to change the part of the script calculating the focal length  because the SX20 and S5 do not share the same lens. You just need to take the value from the get_zoom function and transfer it to focal length. This can be done by looking at the focal length table of your camera in the source code.

For the SX20, you should use the following table (with linear interpolation between the points) .
{result of get_zoom, focal_length in micron},
{   0,   5000 },
{  11,   6190 },
{  41,  12610 },
{  64,  25240 },
{  86,  45470 },
 { 105,  65880 },
 { 128, 100000 },

Also, note that I used (focal length in mm)*10 for the calculations in the script.
Good luck!

What exactly should be changed in the original SX30/S5 .bas file in order for the script to work on SX20. Excuse me for the lame question,but I`m not quite familiar with script modding.

 

Related Topics


SimplePortal © 2008-2014, SimplePortal