Magic Lantern Forum

Developing Magic Lantern => Scripting Corner => Topic started by: garry23 on May 29, 2017, 06:57:06 PM

Title: Focus Stacking with Focus Bar
Post by: garry23 on May 29, 2017, 06:57:06 PM
Those that track 'my fun', will know I've implemented a focus bar in Lua.

In this post I'm releasing an enhanced focusing feature that allows you to get optimum focus stacking.

The only requirement is that the lens should report focus distance (which is assumed to be 'accurate').

As the current ML DoF reporting is broken (pull request submitted), I've made this release of the FOCUS script self contained, ie it does not rely on ML DoF info,

The script can be downloaded from here: https://gist.github.com/pigeonhill/cd1dbe0191ca1264fc0aa07aaafb515d

The use the focus stacking/bracketing feature, simply switch it to DoF mode (option -1). The script default for focus stacking is on (you can reverse this if you wish).

Here is a screen dump having just taken an image:

(http://thumb.ibb.co/h0KtHa/Image_A.jpg) (http://ibb.co/h0KtHa)

The focus bar is telling us what the optical (sic) DoFs are, and the script (at the bottom) is telling what the diffraction corrected DoFs, ie where the ML DoFs would normally be (switched off when running the script). The focus bar end (in DoF mode) show the 'worst case' DoFs, ie the optical ones that ignore diffraction. The green dots are the diffraction aware DoFs and the red dot the focus point.

In focus stacking mode, the DoFs at the time the image is taken are captured, so that we can compare these with other DoFs as we refocus.

Here is a screen dump showing refocusing towards the infinity end:

(http://thumb.ibb.co/fvFiHa/Image_B.jpg) (http://ibb.co/fvFiHa)

And here is a screen grab if we focus the other way, ie towards the macro end:

(http://thumb.ibb.co/hGzAxa/Image_C.jpg) (http://ibb.co/hGzAxa)

In both cases, the magenta shows the amount of focus overlap, which you will need to ensure is visible, ie  if no magenta is showing then you don't have focus overlap ;-)

Bottom line: I hope some find the focus stacking feature of value. Although I wrote it for 'landscape photographers', because you can use it 'both ways', ie relative to the image you are taking, it may be of use to others.

As usual, I welcome feedback.

Cheers

Garry

Title: Re: Focus Stacking with Focus Bar
Post by: garry23 on May 30, 2017, 07:47:03 PM
...and those interested in using the focus stacking enhancement to the focus bar script, here is a tutorial I've just published: http://photography.grayheron.net/2017/05/focus-stacking-using-focus-bar.html
Title: Re: Focus Stacking with Focus Bar
Post by: BBA on June 02, 2017, 02:51:48 PM
Don't know if it is the right place to report : it is not the functioning of the script  that is problematic but the functioning of the camera when the script is loaded.

Details (easy to test)

1st configuration (normal behaviour)
Lua.mo disabled
when I switch on the camera I get a black screen (no LV)  as usual ; if I then press the canon menu button, the menu shows up.

2d configuration (weird)
Lua.mo enabled
Focus.lua script set to be loaded
When I switch the camera on, the screen is black as usual.
If I then press the « menu » button, the canon menu does not appear; the screen does not remain black as I can see a little variation of the screen luminosity ; if I press again the button, the screen returns black as usual; this repeats itself as I continue pressing the same button many times in a row)
If I press the LV button and enter the ML menu, it seems to unlock things (the number of « states » of the system increases as one might think and becomes difficult to report).

FYI : firmware 113-ml-Nighy.2 (last nightly ; saw the same problem with a preceding one but could not narrow things at that time)
Title: Re: Focus Stacking with Focus Bar
Post by: garry23 on June 02, 2017, 04:18:45 PM
@BBA

The script needs some 'Lua fixes'. Could you try running with the experimental Lua fix build.

If you don't (sic) see XXXXXXXXXX on the screen with the focus bar then the Lua.module s OK, the XXXXXXXX on the screen is the sign the 'tansparent' isn't working, ie old build.

I've tested on an EOSM and a 5D3 and works fine...up to a point.

I also see the Menu strangeness and I clear it by accessing the menu, ie is appears in chunks as I click on the menu.

Cheers

Garry
Title: Re: Focus Stacking with Focus Bar
Post by: BBA on June 02, 2017, 05:13:00 PM
@garry23

I see XXXX on the screen => not the Lua module needed.
I will try with Lua experimental build

Thanks !
Title: Re: Focus Stacking with Focus Bar
Post by: BBA on June 02, 2017, 10:13:02 PM
@garry23

I used the Lua experimental build => no XXX anymore  :).

Some ideas/questions/feedback/reminder:
- So many options...for newbies like me
- saving the config from session to session : have seen in Lua API / module config : config:create_from_menu , config.data , config:save() , config:load() : I will try if I get the time  :P

(About the manual stacking)
- appreciate your tutorial on Grayheron  :)
- questioning about the best way to "restart" a stack : just set in menu off then on ?
- the magenta line is a clever idea for sequentially shooting from infinity to near field (also in the opposite direction) without adding too much complexity to the display

Hope it can help...(not complete)

Title: Re: Focus Stacking with Focus Bar
Post by: garry23 on June 03, 2017, 07:42:17 AM
@BBA

Thanks for testing and the feedback.

I defaulted the focus bar to open in DoF mode, as this is the most useful for all.

You can reorder the script menu to your default preference, eg stacking off.

I must get config saving working, but haven't tried it yet.

Because of the way the focus stacking works, it is reset every time you take an image. Thus if you are starting a new stack from a point, I personally simply take an image at that point with my hand in front of the lens, then take another with the lens clear. At the end of a stack I use my hand again, now the stack is identifiable in LR, i.e. Between my hands. Simple but effective. You can even identify the start and end of the stack, eg hand open or closed, one of two fingers, front of hand vs back of hand etc.

Title: Re: Focus Stacking with Focus Bar
Post by: garry23 on June 03, 2017, 09:15:21 AM
@BBA

You may find my latest post of help, as you explore the focus bar: http://photography.grayheron.net/2017/06/getting-best-out-of-focus-bar.html

Postscript: just did a minor tweak to focus bar (https://gist.github.com/pigeonhill/cd1dbe0191ca1264fc0aa07aaafb515d) to correct something ;-)
Title: Re: Focus Stacking with Focus Bar
Post by: garry23 on June 03, 2017, 10:32:34 AM
@BBA / all

If you use focus bar please make sure you change/set the sensor size for your camera at line 136
Title: Re: Focus Stacking with Focus Bar
Post by: BBA on June 03, 2017, 02:59:01 PM
@garry23

a) Thanks for this article,  it is worth reading. Your are not the only user of the focus bar !

IMHO (please, take all of it with a grain of salt : as I reread myself, I feel the following "may" appear like if I were "giving lessons". Don't get me wrong : I write it "in general" (who would I be to give lessons to others I don't even know?); you know I have to struggle to try to give a better feedback and make me understandable  :P .
It would be easier for me to tell you that "everything is fine" or " I like it" or "thumbs up" ...

It is worth reading:
a.0) simply because what is learnt at school is right : it is of good practice to recall the ground basis of one's work : it's good communication. I know you know that because you are a blogger.
In development case, it goes further :
a.1) with software (some sort of expression/communication), one has to take decisions and refer to end users, back and forth, to know if everything suits them (I have done that as analyst and end user) : you make the scripts for your own needs and really know what you are doing (photography), this process is easier than it usually is;
a.2) this intense communication is useful to end users who can then smoothly "interface" with the software.

The following gives "an example" of something you write and that is very interesting to me in practice :
Quote
When the far DoF reaches 'infinity', the focus bar switches its info presentation to show the total blur at infinity, in microns, and the near DoF that corresponds to that blur criterion. This is necessary as far DoF distance now has no meaning, ie the far DoF is at infinity.
The meaning of something changes (for a good reason) : that's important to know.

That's why I appreciate to read your articles and why I am thankful for your efforts that really help me.

b) ease of development

I really hope you have found a graphical development system for Lua scripting because it is much easier, especially when drawing on a screen.
Personally I have found some drawing capabilities in ZeroBrane studio but I am still far from enough : I use routines used by "turtle.lua" which come bundled with it.
It is then possible to emulate the Lua API routines you use with a "dummy module".
In ZeroBrane you can run a chunk of code "as a scratchpad" under the menu "Project": the same script is rerun as soon as you make any modification (like modify constants (colors values,....)) and rework it until it suits you, which can come much faster; I can show you if you are interested.

I think it could also be better to have something like "lua inspect" to check the code and find as much errors as possible before porting to the camera.

Maybe "editor.lua" on EOS M can be useful too for some last typos/changes.

my 2 cents.
Title: Re: Focus Stacking with Focus Bar
Post by: garry23 on June 03, 2017, 04:04:15 PM
@BBA

Many thanks for all the feedback 8)

As for my development environment; I'm afraid I'm in the stone age.

I use Visual Studio Code only for entering my code.

I then transfer the script to a card and see if it works in my camera. If I'm lucky I can read the screen errors, sometimes I can't, ie not quick enough.

Bottom line: I would love to hear how others develop script with Lua, and in time I hope camera emulation becomes accessible for all, ie not just those who know how to work the magic.

Cheers

Garry
Title: Re: Focus Stacking with Focus Bar
Post by: a1ex on June 03, 2017, 04:41:16 PM
One way I've found to be a great time-saver regarding camera transfers was a Wi-Fi SD card (http://www.magiclantern.fm/forum/index.php?topic=17041). It's way too slow for transferring photos (e.g. to transfer a single CR2 it's usually faster to use the card reader), but for small files (such as a ML module or a Lua script), uploading it via wifi is much faster. It still needs a camera restart for reloading the script (because Canon's filesystem driver doesn't expect the card contents to be changed externally), but this can be worked around.

Using the USB cable also works; I've tried this approach years ago, but didn't like this setup because of the extra cable (so it's not really ready to use).

QEMU works on Windows as well (though I didn't try to compile it on this platform yet - only ran the binaries from their website). Currently it doesn't emulate LiveView, so it has limited usefulness with Lua for now. It does emulate the GUI side and OS internals pretty well, so it's still useful for testing menu code, graphics, memory usage, multitasking, file I/O, or for hunting bugs (it has some valgrind-like tools).
Title: Re: Focus Stacking with Focus Bar
Post by: garry23 on June 03, 2017, 05:01:01 PM
@A1ex

Thanks for the insight.

My next 'uplift' is simply getting Lua tested on my PC, ie weeding out silly typos like "=" for "==".

Cheers

Garry
Title: Re: Focus Stacking with Focus Bar
Post by: a1ex on June 03, 2017, 05:35:59 PM
This step is easy:

luac -p script.lua


This step is done for scripts placed under magic-lantern/scripts, when you compile the Lua module (look up syntax_check.log in the Makefile).

Random thought: what about making QEMU available as a web service?
Title: Re: Focus Stacking with Focus Bar
Post by: garry23 on June 03, 2017, 05:45:19 PM
@A1ex

If I understand you correctly, running QEMU without having to do all the coding would be great.
Title: Re: Focus Stacking with Focus Bar
Post by: BBA on June 03, 2017, 05:50:09 PM
@garry23

Think we have to move hand in hand with our devs who know things way better then we do.

I am sure they also want to improve the situation and they know the best way (for us) to do it. Please trust me they are able to foresee and weight the amount of work needed. As things go deeper, naïve implementation becomes insufficient or a real burden which can become even worse than what there is now.

@A1ex

Thank you for your help
I also think QEMU as a web service would be great.
Title: Re: Focus Stacking with Focus Bar
Post by: BBA on June 03, 2017, 05:51:46 PM
luac can detect syntax errors before execution which is great.
Title: Re: Focus Stacking with Focus Bar
Post by: BBA on June 03, 2017, 06:27:28 PM
Look at my naïve prototype for 7 segments display emulation.

https://www.dropbox.com/s/gvv57it6f9ru6u5/SEVEN_SEGMENTS.lua?dl=0 (https://www.dropbox.com/s/gvv57it6f9ru6u5/SEVEN_SEGMENTS.lua?dl=0)

if you have ZeroBrane studio installed you can run it (ask me how to do it if you want).

It is prototyping => used to see what appears on the screen, the aspect ratio, the colors, distance between the chars,...

When it is thoroughly tested and appears ok, it can be integrated in the bigger project.

Title: Re: Focus Stacking with Focus Bar
Post by: a1ex on June 03, 2017, 06:40:16 PM
@BBA

Nice one, may I include it in api_test.lua?
Title: Re: Focus Stacking with Focus Bar
Post by: BBA on June 03, 2017, 08:23:58 PM
@A1ex
Yes, for ML community, it goes without saying : I fully support open source : I am glad if I can help !
But I am not glad with the script as it is :  it needs OO redesigning, it is slow (many lines), ...
Title: Re: Focus Stacking with Focus Bar
Post by: garry23 on June 15, 2017, 08:42:35 PM
For those interested in using the focus bar and focus stacking, you may be interested in my latest post: http://photography.grayheron.net/2017/06/close-enough.html
Title: Re: Focus Stacking with Focus Bar
Post by: garry23 on June 17, 2017, 11:25:59 AM
A minor update, but a useful one.

In DoF mode the focus bar now displays the 10% distances between the the near and far DoFs.

This is useful when deciding overlaps when focus stacking, eg aim for a 10% overlap at least.

https://gist.github.com/pigeonhill/cd1dbe0191ca1264fc0aa07aaafb515d
Title: Re: Focus Stacking with Focus Bar
Post by: garry23 on June 28, 2017, 11:25:38 AM
Another update: mainly cleaning up the 'user guide' text at the front of the script.

Plus the addition of showing the defocus blurs at the 10% distance points.

http://photography.grayheron.net/2017/06/focus-bar-update.html
Title: Re: Focus Stacking with Focus Bar
Post by: ironmarshal on July 01, 2017, 11:45:16 PM
I have been interested in trying focus stacking for a while for landscapes and after coming across your blog (which is a wealth of information) I decided to download ML and give it a try. This Focus Bar seems like a great way to approach it, but as I am new I am unfamiliar with Lua and exactly how to download and install this. I have installed the nightly build on my card, what else do I need to do?

Also, as a future thought, particularly for people like myself that are not programmers, an awesome improvement, which I think many people would appreciate, would be to implement the focus stacking and bracketing as a single function. I'm thinking of something like exposure bracketing where you take one picture and ML then figures out how many additional photos are necessary to cover the dynamic range. In this case, you could either take on image, or press the Set button and then have ML take as photos as necessary to achieve optimal front to back sharpness.

At any rate, thanks again for the great blog and development of the focus bar.

Title: Re: Focus Stacking with Focus Bar
Post by: garry23 on July 02, 2017, 08:22:51 AM
@ironmarshal

Things should work with the latest nightly, however, you might be better off using the latest Lua fix: http://builds.magiclantern.fm/experiments.html

Simply replace the ML files/folders on your card with the Lua fix experimental ones.

I would then go into the ML Lua folder on your card and go into the scripts folder. Create a new folder (call it whatever you wish) and move all the scripts into there. You now have a clean scripts area, ie no scripts will load other than the ones you wish.

Place my FOCUS script into the script folder and my Auto Bracketing V4 script. Read the text at the beginning of each script, ie a simple user guide.

Replace the card in the camera and switch on.

I assume you have switched on the modules you need. I personally use Lua, Silent, ETTR and Dual, as my scripts tap into these.

Once you have turned those modules on, switch the camera off and on.

Now go to the Scripts tab in the ML Menu and switch on the Autorun for the two scripts that are there. Switch the camera off and on again, to ensure the autorun loading is OK.

If all went well you now have the focus bar running and access to my auto focus and exposure bracketing scripts.

Both these scripts have been fully tested on my 5D3 and the focus bar script on my EOSM.

To run the auto focusing script, simply go into the menu and select what you wish to do and run the script, which focus brackets from the near to the far, either the far DoF or the FP 'at' infinity.

Hope you have some fun :-)

BTW what camera are you using?

Cheers

Garry
Title: Re: Focus Stacking with Focus Bar
Post by: ironmarshal on July 06, 2017, 04:05:24 AM
After doing a little more reading I'm not sure this is going to work for me. I have a T4i and Tamron lenses....I don't know if the lenses report focus distance, which I believe is a requirement for the script to work.

On another note, I came across a fascinating article on your website about super resolution. You keep inspiring with all kinds of neat ideas.
Title: Re: Focus Stacking with Focus Bar
Post by: garry23 on July 06, 2017, 07:44:05 AM
@ironmarshal

I use my scripts with a Sigma lens.

The 650D/T4i runs ML, including the experimental Lua fix.

You will know if the Tamron reports focus distance if you can see that being reported in the ML bottom bar.

Cheers

Garry
Title: Re: Focus Stacking with Focus Bar
Post by: ironmarshal on July 11, 2017, 03:02:53 AM
Sadly neither the Tamron 17-50 or the 28-75 report distance :( All I see on the bottom bar is the MF/AF designation.
Title: Re: Focus Stacking with Focus Bar
Post by: garry23 on July 11, 2017, 07:28:10 AM
@ironmarshal

Let's try one more time :-)

The Tamron 17-50 is an AF lens: correct?

The AF functionality works: correct?

You have ML loaded: correct?

With ML loaded what focus distance is reported? 0.0 or some other number?
Title: Re: Focus Stacking with Focus Bar
Post by: garry23 on July 14, 2017, 10:47:12 AM
Focus by Colours!

I'm pleased to announce an update of my focus bar script, which you can read about and download here: http://photography.grayheron.net/2017/07/focusing-by-colour.html

As usual I welcome feedback.

Cheers

Garry
Title: Re: Focus Stacking with Focus Bar
Post by: garry23 on July 14, 2017, 07:02:08 PM
A quick update ;-) http://photography.grayheron.net/2017/07/more-colour-means-more-info.html
Title: Re: Focus Stacking with Focus Bar
Post by: ironmarshal on July 14, 2017, 08:41:37 PM
Distance says 0.0
Title: Re: Focus Stacking with Focus Bar
Post by: garry23 on July 14, 2017, 09:01:38 PM
@ironmarshal

:( :(

Pity.

Title: Re: Focus Stacking with Focus Bar
Post by: garry23 on July 20, 2017, 10:18:38 PM
The latest update provides a refined way to tune the near depth of field when you are focusing between the HFD and Infinity: http://photography.grayheron.net/2017/07/your-choice.html

You can now select what infinity blur criterion you use in the HFD-Infinity focus zone to calculate the near DoF, eg defocus only, 2 x defocus, total blur or 2 x total blur.

The bottom DoF feedback also shows the defocus blur component of the total blur, including as it changes in the HFD-Infinity focusing zone.
Title: Re: Focus Stacking with Focus Bar
Post by: garry23 on July 31, 2017, 09:56:03 PM
I've had some feedback that the focus bar is too complex.

I hope this post helps those struggling with using this new way of getting optimum focus, especially for landscape photographers: http://photography.grayheron.net/2017/07/for-goodness-sake-garry-kiss.html
Title: Re: Focus Stacking with Focus Bar
Post by: garry23 on August 05, 2017, 01:25:38 PM
Following in a similar way as the previous post, here is a short piece for those who struggle to understand the advantages of focusing using my focus bar: http://photography.grayheron.net/2017/08/understanding-focus-blurs.html

I hope this helps those who are confused with blur and in particular infinity blur.
Title: (UPDATE) Focus Stacking with Focus Bar
Post by: garry23 on September 30, 2017, 11:00:44 AM
I've tidied up my focus bar script, including a few options, algorithms and code.

https://gist.github.com/pigeonhill/cd1dbe0191ca1264fc0aa07aaafb515d
Title: Re: Focus Stacking with Focus Bar
Post by: garry23 on October 10, 2017, 08:37:17 PM
Another update. This time to check that the focus bar can run, ie lens reports aperture, focus distance and focal length.

Note: you still need to put your sensor pixel size in the script, as well as change frequency if you are shooting with an IR conversion.

https://gist.github.com/pigeonhill/cd1dbe0191ca1264fc0aa07aaafb515d
Title: Focus Stacking with Focus Bar: Update
Post by: garry23 on October 26, 2017, 03:56:37 PM
Update on Focus Bar

For those that are users of my Focus Bar script, you may be interested in the latest version.

The latest changes are directed at the UI and making the script more 'integrated' with ML.

The first change is that the focus bar will not work if the lens is incompatible with the script. Also, the script switches off the ML DoF reporting, ie all DoF feedback is via the script.

The next change is that the focus bar will not show on start up, ie when the ML console is running, but will start as soon as you start focusing.

Next, when you are focusing beyond the hyperfocal distance, ie when the script shows the various infinity blurs (defocus blur, diffraction blur and total blur), the script shows the near DoF for three blur criteria: using the infinity defocus blur, twice the infinity defocus blur and the DoF based on using the ML set defocus blur, ie accounting for diffraction - this allows you to make the final focus decision based on the best DoF estimates.

Next, if you over focus, ie focus distance is greater than (lens reported) infinity, the script will warn you.

Finally, although I wrote the Focus Bar script for my own use, I freely share it with others. Having said this, I would welcome knowing if anyone, other than me, uses the script ;-)

Updated script can be found here: https://gist.github.com/pigeonhill/cd1dbe0191ca1264fc0aa07aaafb515d
Title: Re: Focus Stacking with Focus Bar
Post by: garry23 on November 01, 2017, 11:05:31 AM
I've written this post for cathartic reasons and in the hope that others may benefit from my slow progress in getting better at Lua programming.

Ever since I started writing scripts, I've 'suffered' from not being able to dynamically handle the Lua drawing with the Canon drawing.

In my latest version of the Focus Bar (FB) script I've decided to adopt a different approach.

In this version of the script the FB is hidden at start-up.

To expose the FB all you need to do is a greater than 3 sec halfshutter press. To hide the FB simply do another greater than 3 sec halfshutter press.

Thus you can turn the FB display on and off, allowing you to cleanly access the Canon menus, eg MENU and INFO etc.

The latest version of the script is here: https://gist.github.com/pigeonhill/cd1dbe0191ca1264fc0aa07aaafb515d
Title: Re: Focus Stacking with Focus Bar
Post by: garry23 on November 24, 2017, 11:14:12 PM
***Please note, FB defaults to hidden or 'off' mode. Simply do either a 3 or 6 sec half-shutter press to toggle the two working modes. Also, to toggle stacking mode on or off, move lens to infinity and do a 3sec half-shutter press. Change the script's menu to have the defaults you want.***

For those that use my Focus Bar, here is a tutorial and an update: http://photography.grayheron.net/2017/11/focus-bar-tutorial.html
Title: Re: Focus Stacking with Focus Bar
Post by: garry23 on December 09, 2017, 05:09:59 PM
Further refinements of my Focus Bar and Toggler scripts, which you can read about here:http://photography.grayheron.net/2017/12/further-refinements.html

The FB screen now shows 'all' the focus details you need for perfect focus control


(http://thumb.ibb.co/kZ6c0G/VRAM3.jpg) (http://ibb.co/kZ6c0G)


In this screen shot we see that the last image's DoF was 56cm (near) and 1.52cm (far), ie based on SQRT[CoC^2 - Diff^2].

That the defocus blur, based on the ML set CoC and the diffraction, is 14 microns.

We see the current image is focused at 45cm and has DoFs of 36cm and 60cm (near and far).

We see that the blur at the focus overlap, ie relative to the last image, is 12 microns. The magenta shows the overlap.

The cyan area shows where the blur is less than the sensor limit, ie twice the pixel size.

Finally the number [3] shows that the number of focus brackets to the hyperfocal is three: based on the minimal acceptable defocus overlap. That is it could be more than this.

As usual I welcome feedback.
Title: Re: Focus Stacking with Focus Bar
Post by: garry23 on December 11, 2017, 04:14:43 PM
A few tweaks to ensure Toggler and Focus Bar work well together.

Download from here (on the right): http://photography.grayheron.net/ (http://photography.grayheron.net/)
Title: Re: Focus Stacking with Focus Bar
Post by: garry23 on December 16, 2017, 02:16:53 PM
As some think using pictures and others through text & numbers, I've added a data-based focus quality indicator to my Focus Bar script: http://photography.grayheron.net/2017/12/there-are-two-kinds-of-people-in-world.html