Is it possible to start focus stacking via half-press of the shutter button?

Started by pulsar124, October 08, 2018, 08:25:34 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

pulsar124

I am trying to use the ML's focus stacking feature (along with full resolution silent picture, FRSP) to automate photogrammetry (making 3D models from lots of photos of the object from different angles) for small objects (where the DoF is too narrow, and one has to use focus stacking for each angle of the camera). This is operated by my own 3D Scanner gadget based on Arduino:

https://pulsar124.wikia.com/wiki/3D_Scanner

The official code for the 3D Scanner only supports continuous shooting mode (the platform with the object continuously rotates, while camera is taking pictures with the flash used to freeze the motion). I recently modified the code, and now it can be used in a non-continuous fashion: it turn the platform a bit, then stops, and half-presses the camera's shutter button to do the shot, then it tuns again and so on.

This works fine with regular shooting (with no focus stacking). It would also work with focus stacking - if only I could figure out how to trigger ML's focus stacking from my Arduino gadget. Right now the gadget can operate half-press (AF pin) and full press (shutter pin), via opto-couplers.

Now I am stuck. I already discovered that focus stacking works great with FRSP. (One twist was that I had to set Step Wait to "off" in Focus Settings to make it work.) But I can't figure out how to trigger focus stacking from Arduino. The stacking can be triggered with the Set, Play, and Left Arrow buttons, but none of them can be controlled remotely. If I half-press the shutter button while the cursor is pointing at the Run Focus Stack option, the camera takes the regular FSRP photo, no focus stacking happens.

Is there a trick or special setting (either in ML, or camera's menu) I can use to enable remote triggering of focus stacking? If not, perhaps there is an easy ML code fix to make this happen? (For example, when the cursor is pointing at the Run Focus Stack option, Half-Press button is reassigned from triggering FRSP to starting a focus stack.) Or may be a simple Lua script can do this?

BTW my camera is Canon 6D mk1. I am using the latest stable ML version (from July of this year).

Thanks for any advise!

a1ex

Very nice experiment! Some details on the 3D reconstruction process would be nice (maybe I didn't know where to look).

A bit hackish, but appears to work:

menu.close()
sleep(2)
menu.set("Focus Stacking", "Run focus stack", 1)
-- exact value doesn't matter; the heuristic in menu.set will notice that menu is not going to change its value, so it will just run the action once and give up.


Now you just need to run this on half-shutter press.

pulsar124

Thanks a bunch! This is a Lua script, correct? How do I use it? Just enable Lua module, and place the script in ML/SCRIPTS? And what exactly you mean by "you just need to run this on half-shutter press"? Do I still need to add more lines to the script?

Regarding photogrammetry - one can start from the wikipedia article, https://en.wikipedia.org/wiki/Photogrammetry . I have some experience using an open source software, VisualSFM , but got better results with PhotoScan. Here is my test scan of a 20cm high statue, using ~100 photos (made with my 3D Scanner gadget - FRSP + flash):



This doesn't work with smaller objects (say 5cm high) because of the limited DoF - hence my need for automating focus stacking with ML. I want to 3D scan some of the clay figures made by my daughter, and then to 3D print them.


a1ex

Yeah. Forgot to mention - you need the latest lua_fix build for that script to work.

Half-shutter:

function on_keypress(key)
    if key == KEY.HALFSHUTTER then
        menu.set("Focus Stacking", "Run focus stack", 1)
    end
end

event.keypress = on_keypress


Small bug: it will trigger both a regular silent picture and a focus stack sequence. Workaround: you could disable the half-shutter trigger in silent.c by editing the source.

TODO: figure out how to solve this without workarounds.

bouncyball

@pulsar124

Quote from: pulsar124 on October 08, 2018, 09:55:37 PM
Here is my test scan of a 20cm high statue, using ~100 photos (made with my 3D Scanner gadget - FRSP + flash):
Very cool it is so detailed. Can you post some pictures from different angles of only wireframe without a texture?

pulsar124

I don't have any raw images (it's been a while), but I do have the 3D model as a PDF file (you'd need a recent Adobe PDF reader to see it as a 3D model):

https://www.physics.mcmaster.ca/~syam/statue3d.pdf

There is an option there to choose what to see (solid, wireframe etc).

bouncyball


pulsar124

To tell you the truth, this is so far the only good scan I had - likely because it's fairly large (~25cm high), so DoF wasn't an issue, plus it has lots of texture, which really helps in photogrammetry. I also tried smaller items (5-10cm high), but narrow DoF would result in poor quality models. Now that I modified my Arduino code - so it can do non-continuous shooting, making focus stacking possible - I am hoping to get much better results with smaller items.


pulsar124

Alex: I tried your second script - copied it as is to a *.lua file, ran it from my camera (latest lua_fix ML build), and when in Live View I point the cursor in ML menu to Run focus stack , then briefly press half-shutter, the focus stacking sequence is executed. At the end I see the Live View on the screen. So far so good! But when I try to repeat the sequence by again pressing the half-shutter, it only takes a single FRSP shot, then displays it on the screen with wording "Waiting for long half-press". So the second focus stacking sequence is not taken. To make it work again, I have to use camera's controls to bring the ML menu back, and point the cursor at Run focus stack.

What am I doing wrong here? It feels like I'm almost there, just a small piece is missing.

Here are the details: exposure 1/4s, Expo Override On, ExpSim On, FRSP with DNG.

pulsar124

Any further pointers? I am stuck trying to figure out how to fix the script.

pulsar124

I figured this out - I had to place the earlier scriplet inside the second one. I also had to reduce the sleep time to 1s (it didn't work reliably with 2s). Here is the complete script:


--Stacking
function on_keypress(key)
    if key == KEY.HALFSHUTTER then
        menu.close()
        sleep(1)
        menu.set("Focus Stacking", "Run focus stack", 1)
    end
end

event.keypress = on_keypress


It seems to work reliably. It does produce an extra shot at the beginning, but this is easy to take care of during post-processing. Using DNG format FRSP on my Canon 6D + 135L with an extension tube, the time it takes to run the full focus stacking sequence (including the rewinding part) is

dt_seconds = 11 + 7.77*N

Here N is the number of shots taken (including the first one; I am not counting the extra shot at the beginning). With N=10 it takes 88s; with N=20 it takes 167s. I used focus stacking step=10, 0s Start Delay, and the default Step size=2 in Focus settings. The exposure is 0.4s.

The real test will be running focus stacking 40 times in a row, automatically (controlled by my 3D Scanner gadget). I am concerned about the possibility that the starting focus point will be drifting - because I set Step Wait to OFF in Focus settings (that was the only way to make focus stacking work with FRSP).

a1ex

Quote from: pulsar124 on October 22, 2018, 02:02:41 AM
because I set Step Wait to OFF in Focus settings (that was the only way to make focus stacking work with FRSP).

I don't see why this would change anything regarding FRSP; this option only affects the delay between focus commands.

Quote
It does produce an extra shot at the beginning

That's because there are two "listeners" for the half-shutter event: the regular silent picture and the focus stacking script. Will try to find some easy way around it; general idea: disable the silent picture menu entry during idle times.

pulsar124

Quote from: a1ex on October 22, 2018, 10:17:10 AM
I don't see why this would change anything regarding FRSP; this option only affects the delay between focus commands.

I am not quite sure how, but with the Wait ON (default 10, I also tried much larger values) focus stacking didn't work at all with FRSP (both DNG and MLV). I struggled for a while until I discovered that setting Wait to OFF resolves this issue. It's almost like with the Wait ON the focusing move took too long time, and something in the focus stacking algorithm timed out.

a1ex

That's unusual. Camera is 6D? Follow focus is working? Does follow focus or standalone focus stacking work any better with main builds, with wait enabled?

The above script probably won't work with mainline builds (not tested). I just wanted to make sure focus functionality isn't broken in the lua_fix builds, as it's queued for merging.

pulsar124

It's Canon 6D. I will do the testing later today. The focus stacking + FRSP issue I had was on the mainstream ML builds - first on 1 year old build, but also on the up-to-date one. I haven't tested this on the lua_fix build.

pulsar124

 Good news - with the Lua script for half-shutter and a recent nightly lua_fix build of ML, the FRSP + focus stacking works without issues even when Wait=ON in Focus settings. (That was critical, as with Wait=off focus stacking was all wrong). My 3D scanning (photogrammetry) gadget now works for small objects requiring focus stacking for each angle of the object. I wrote a bash script which on a Windows PC (under Cygwin) automatically performs focus stacking for all groups of N shots using Zerene stacking software.

One remaining issue is the extra duplicate shot at the beginning of focus stacking sequences. Unfortunately it doesn't happen all the time - probably ~50% of the time, so I cannot write script which would get rid of the duplicates automatically. Time codes are of no use - duplicates are spaced in time with the same interval as normal shots.  It is not a huge issue, as I can find and delete the duplicates visually.

I just shot ~1000 shots of a small object (~4cm high), resulted in 105 shots after focus stacking (which worked perfectly). I am now trying to make a 3d model out of it...