mviereck / microscopy-tools

Tools for microscopic captures and focus stackshots (beta)
GNU General Public License v2.0
26 stars 1 forks source link

imfuse: finding good parameters for focus stacking #4

Closed holgerd001 closed 1 year ago

holgerd001 commented 3 years ago

This issue is opened to test, discuss, improve, ... the results of imfuse. Many parameters are possible, many parameters can have arguments and some parameters can be combined reasonable and other not. Some hints can be found here: https://github.com/mviereck/microscopy-tools/tree/master/fusesets

Suggestion for a workflow:

  1. take photos
  2. align them (e.g. align_image_stack)
  3. fuse them (imfuse)
  4. convert tif to jpg (magick) (optional)
  5. transfer exif data (exiftool) (if exif data is in the aligned images, imfuse do that for you)
holgerd001 commented 3 years ago

In the past I have used enfuse and got more or less good results with enfuse -o result.tif --exposure-weight=0 --saturation-weight=0 --contrast-weight=1 --hard-mask --gray-projector=l-star input_????.tif

In the hope imfuse will be faster and/or provide better results I started working with imfuse.

Until now I haven't found a parameter set for imfuse that produces a result as good as enfuse do. Most results have halos. So I started this issue and continue experimenting with different parameters. Within the next days I'll send some results.

Any suggestion witch parameters should be combined or witch not?

Greetings Holger

mviereck commented 3 years ago

Most results have halos. Any suggestion witch parameters should be combined or witch not?

Your moth example has a black background. Likely the halos appear there? To avoid halos in black background, it helps to add a weighted saturation mask, automatically with --saturation=w or manually with e.g. --saturation=w15. Example:

imfuse --log --saturation=w15  IMAGES

Further examples with --saturation in https://github.com/mviereck/microscopy-tools/blob/master/fusesets/macro-soft-less-sat . These more complex examples also add options less,close,soft.

In the hope imfuse will be faster and/or provide better results I started working with imfuse.

Likely imfuse will forever be slower than enfuse. sigh enfuse already gives pretty results. imfuse wants to improve these points:

In areas with strong contrast imfuse gives pretty nice results with most provided mask options. Currently I work on further solutions for low contrast areas. Current solutions are described above; I hope to find even better (and easier) solutions.

mviereck commented 3 years ago

An attempt to write a guidance that might result in a wiki article:

Remove artefacts in low contrast areas

Currently there are basically two ways to handle low contrast areas with artefacts like halo and noise:

Strenghten weak foreground objects

Some objects in foreground like thin hairs might have less contrast significance than the main object behind. In that case they are not completely drawn and might look transparent, broken, or disappear at all.

General recommendations:

holgerd001 commented 3 years ago

Combining e.g. --log with --saturation=w15 will apply the saturation mask only in areas where the contrast strength of --log is below 15%.

* Just writing `--saturation=w` will set a default weight value fitting the first specified mask option.

To get an overview about --log and --saturation here is what I have done:

  1. Taken photos

  2. align_image_stack -v -C -c80 -m --use-given-order -a aligned_c80_C_morder

  3. enfuse -o xD2040938_aligned_c80_C_m_order_grayl-star.tif --exposure-weight=0 --saturation-weight=0 --contrast-weight=1 --hard-mask --gray-projector=l-star Now I have an image "xD2040938_aligned_c80_C_m_order_grayl-star.tif" to compare with.

  4. resize the aligned images to be faster for file in aligned_c80_C_m_order_00??.tif; do convert $file -resize 20% pc20-$file; done

  5. using imfuse several times with variations of --log and ----saturation e.g. imfuse -o imfu_log_satw65_pc20.tif --log --saturation=w65 pc20-aligned_c80_C_m_order_00??.tif

  6. save bandwith and mem: mogrify -format jpg imfu_*.tif

imfu_log_pc20.tif imfu_log_pc20

imfu_log_satw5_pc20.tif imfu_log_satw5_pc20

imfu_log_satw10_pc20_c80_C_m.tif imfu_log_satw10_pc20_c80_C_m

imfu_log_satw15_pc20_c80_C_m.tif imfu_log_satw15_pc20_c80_C_m

imfu_log_satw25_pc20.tif imfu_log_satw25_pc20

imfu_log_satw35_pc20.tif imfu_log_satw35_pc20

imfu_log_satw45_pc20.tif imfu_log_satw45_pc20

imfu_log_satw55_pc20.tif imfu_log_satw55_pc20

imfu_log_satw65_pc20.tif imfu_log_satw65_pc20

imfu_sat_pc20.tif imfu_sat_pc20

Made by emfuse (xD2040938_aligned_c80_C_m_order_grayl-star_pc20.jpg): xD2040938_aligned_c80_C_m_order_grayl-star_pc20

Most of the images generated by imfuse have black pixels in the light part right and left from the eyes of the fly. If I use the non resized aligned images as input the number of these black pixels decrease (imfu_log_satw15_aligned_c80_C_m.jpg). imfu_log_satw15_aligned_c80_C_m

Nevertheless the result of emfuse looks more 'natural'.

Next I use less, close and soft.

holgerd001 commented 3 years ago
1. take photos

A short explanation about the way I take photos.

Inspired by 'traumflieger' I build up a lens to get more than a magnification of 1:1. It's made of a camera + adapter (mft-Minolta) + bellows + adapter (Minolta-M42) + adapter (M42-RMS) + microscope lens (Kern Plan 4/0.10 160/0.17)

D2061246_25

D2061245_25

mviereck commented 3 years ago

Thank you for showing your camera setup! Quite interesting. I have an unused microscope, maybe I'll build something similar, too, with one of the microscop objectives. Do you manually adjust the steps with the wheels?

Thank you for sharing the imfuse results. They are quite unsatisfying, --saturation=w does not help as expected. With --soft you should get much better results.

What does the shortcut c80_C_m mean? Btw, if you don't specify an output name, imfuse creates a name that includes all chosen options and automatically set default values.

holgerd001 commented 3 years ago

Do you manually adjust the steps with the wheels?

Yes. And this can be a problem, because it's not very sensitive. A hundredth rotation of the adjusting knob can change a wide range of depth of field (DOF). Actual I need a macro slide or something like that. Perhaps a positioning slide like turning lathes have.

What does the shortcut c80_C_m mean?

That are the parameters align_image_stack has created the output files with. -c80 -C -m -m Optimize field of view for all images, except for first. Useful for aligning focus stacks with slightly different magnification. -C Auto crop the image to the area covered by all images -c num Number of control points (per grid) to create between adjacent images (default: 8)

Btw, if you don't specify an output name, imfuse creates a name that includes all chosen options and automatically set default values.

That's what I do too.

I have an unused microscope, maybe I'll build something similar, too, with one of the microscop objectives. Here are some hints:

  • have a look at traumflieger.de
  • 4x magnification is enough (at first)
  • it should be a lens for a limited microscope tube. Here 160mm. Afaik you need an additional lens, if you take an infinity lens (objective).
  • you can save one adapter, if you take a M42 ballows
  • you can buy a M42-RMS or M42 M26F adapter at traumflieger.de. If you need a different adapter or just simply want to test if it works, you can make an adapter by yourself.

Take a small part of plywood. Saw a circle that is a little bit to big to fit into the M42. Drill a hole that is a little bit to small for your thread of the lens. Use abrasive paper for the acuteness. The thread of the lens and of the M42 part is much harder then the wood. So the thread is self-cutting. The internal side should be black. It's not a high precision adapter, but it works. D2071248_20 D2071249_20

mviereck commented 3 years ago

Thank you the hints!

And this can be a problem, because it's not very sensitive. A hundredth rotation of the adjusting knob can change a wide range of depth of field (DOF).

It might help if you add a gear wheel and rotate it with a gear spiral. (Not sure if I translated right: Zahnrad und Schnecke, beides von fischertechnik). Here a picture of my microscope; the spiral is driven by a stepper motor, the gear wheel is glued to the microscope fine tuning wheel. The glue is Heißkleber, strong enough but easy to remove. Turning the spiral by hand instead with the stepper motor also allows very fine rotation of the gear.

Or just one big wheel glued on the small knob of your slide would already allow finer rotation by hand, without using a spiral.

Actual I need a macro slide or something like that.

You could also use a cheap old microscope as macro slide. If the microscope moves its table, you can remove the microscope tube and use your current setup. Alternatively you could somehow mount the camera onto the tube. Will be tricky to find the right distance to get a sharp projection onto the chip.

holgerd001 commented 3 years ago

It might help if you add a gear wheel and rotate it with a gear spiral. ...

Thanks for this hints. I think about something similar. Mounting two runners (Schubladenschienen) parallel on a wooden board. An other board where the camera gets mounted on) becomes mounted on the runners. Than a threaded rod beside to move top board. But in the moment it's to cold to work in the workshop.

You could also use a cheap old microscope as macro slide.

That's an interesting idea. The microscope I have needed to be cut (by saw). And the microscope stage is to small to mount a camera on. The insects I take photos from are needled. I use a 'third hand' to keep it in position. It's difficult to mount the 'third hand' on the microscope stage. But I'll think about.

mviereck commented 3 years ago

I've added two new options that reduce noise: --cutblur and --finalblur. I am still trying out their effects.

Your stack above might give a good result with:

imfuse --dog=s0.3 --soft=25 --less=6 --cutblur=0.5
imfuse --dog=s0.3 --finalblur=w10,s1 --less=6 --cutblur=0.5
imfuse --log=s0.3  --finalblur=w10,s1
mviereck commented 3 years ago

I've made a change in the mask generation that impacts --soft, --less and --finalblur. They need much higher values than before, but are now the same across all mask options. I am still checking out, but as a start I recommend --soft=100, --less=60, --finalbur=w75,s1. (Or, as mask arguments, like --log=soft100,less60.)

mviereck commented 3 years ago

I've made several changes the last few days, and meanwhile some commits had new introduced bugs; I should have checked better before uploading. I hope you didn't had issues because of that. Now all seems to work well again. For your image above, I assume this setup will do a good job:

imfuse --wavelet --soft --less --close 

With current default values, this is the same as:

imfuse --wavelet=p10 --soft=100 --less=60 --close=2x2 --background=enfuse
holgerd001 commented 3 years ago

imfuse --wavelet --soft --less --close

Yes, impressiv. As I compare it with the result from enfuse --gray-projector=l-star I see:

And I noticed, that I have to improve the lighting. At the moment I need two small LED lamps with curtain in front of to make the light softer. But I assume it's not soft enough.

mviereck commented 3 years ago

Yes, impressiv.

I am happy to hear this. :-)

a little bit pixelated in the light area left of the left eye and left of the mouth

Likely that can be fixed. --cutblur should help, but makes the image softer overall. (With --cutblur you can omit --close). --finalblur is worth a try, but less effective along with --soft. Higher values for --soft might help, too. (Yet I am looking at the --soft code again, found likely an issue that can cause some false results or detail loss. So the value range might change again.)

Edit: pixelated areas can also be a result of .jpg quality loss, e.g. while resizing the image. I always convert to .tif first before doing any other image processing.

And I noticed, that I have to improve the lighting. At the moment I need two small LED lamps with curtain in front of to make the light softer. But I assume it's not soft enough.

You might get slightly better light distribution with option --fakehdr that makes dark areas brighter and bright areas darker. Of course, no computer technique can achieve or outweight the quality of a good capture.

Some setups can be found in www.focusstackingforum . They often use something like a semitransparent white tube around the object with light sources outsight of the tube. That results in diffuse light inside.

holgerd001 commented 3 years ago

Higher values for --soft might help, too.

It seems --soft is 100 by default. I got imfuse.wavelet=p10_soft100_less60_close2x2_bg=enfuse.fc1e5b.tif using the defaults (imfuse --wavelet --soft --less --close).

mviereck commented 3 years ago

It seems --soft is 100 by default.

You can set values above 100, too.

holgerd001 commented 3 years ago

You can set values above 100, too.

Ok, I thought that means percentage and 100 means 'all'. I tried 150 but the differential is marginal.

mviereck commented 3 years ago

Ok, I thought that means percentage and 100 means 'all'.

Percent value is right, but here allowing values above 100% and below 0%

I've changed the argument handling of --soft yet. It is hard to explain in words, so I've opened a new ticket #9 that shows an example table.

mviereck commented 1 year ago

I am working on tutorials and examples in the wiki: https://github.com/mviereck/microscopy-tools/wiki