PetteriAimonen / focus-stack

Fast and easy focus stacking
MIT License
249 stars 37 forks source link

RFE: Option for alignment only? #9

Closed mviereck closed 3 years ago

mviereck commented 3 years ago

Hello,

I am occasionally working on stackshot fusing tools, too. (https://github.com/mviereck/microscopy-tools) It uses external tools for alignment, currently either vidstab (ffmpeg) or align-image-stack. Could you provide an option to align images only, so I could use your focus-stack? I would be happy with either ready aligned images or just a text file providing distortion values.

PetteriAimonen commented 3 years ago

Sounds reasonable.

Probably easiest to add --align-only so that it works like --save-steps except saving only the alignment result images and stopping after that. The transformation matrix is a bit of internal implementation detail so I'd prefer not to have any external applications depend on it, as that would break compatibility with future improvements (e.g. if non-linear lens distortion correction would be added).

mviereck commented 3 years ago

Thank you that you consider it! I did a test run with --save-steps and got proper aligned results. But I would need to know where to clip the resulting aligned images. Currently they contain wrong content in 'lost' areas. Maybe the common area of all aligned images could be printed on stdout or stored in a log file?

The transformation matrix is a bit of internal implementation detail so I'd prefer not to have any external applications depend on it, as that would break compatibility with future improvements (e.g. if non-linear lens distortion correction would be added).

ok, I see. Describing lens distorting changes could become difficult.

would break compatibility with future improvements

It depends on the implementation. An extensible syntax could be defined. Example for a single line:

file='filename.jpg' distortion.x=5 distortion.y=11 rotate=17 ... [further future parameters declared as stable or unstable]
PetteriAimonen commented 3 years ago

I think for --align-only it would make sense to crop the saved images to the common area by default.

PetteriAimonen commented 3 years ago

I added the option now, the cropped results are save as aligned_ORIGNAME, in the same format as the input images are.

The transformation matrix & color matching info are available from --verbose output:

aligned_frame-0014.jpg transform: [1.002 -0.001 -1.271; -0.000 1.007 -4.797]
aligned_frame-0014.jpg contrast map: C:1.004, X:0.031, X2:-0.187, Y:0.068, Y2:-0.105
aligned_frame-0014.jpg whitebalance: R:x1.004-0.1, G:x0.984+0.7, B:x0.961+0.8

The format for transform is standard 2x3 affine transform matrix. The contrast match has average contrast and x, x², y, y² terms. White balance has contrast and offset for each color channel.

I don't currently foresee changes to this format, but if e.g. lens distortion correction would be added it would probably cause an alignment based on only the transformation matrix to be sub-optimal, as generally all the parameters have some correlation and interdependence.

mviereck commented 3 years ago

I added the option now, the cropped results are save as aligned_ORIGNAME, in the same format as the input images are.

Thank you very much!

The format for transform is standard 2x3 affine transform matrix. The contrast match has average contrast and x, x², y, y² terms. White balance has contrast and offset for each color channel.

Thanks for the insight, I'll look into this. Long time since I've calculated a matrix in school ...

are save as aligned_ORIGNAME

There is one issue that occurs with --align-only but not with --save-steps. If using a relative path to the source images an invalid file name is created:

$ focus-stack ../../test4/magick/* --verbose --align-only
[...]
 0.109 [ 12/432] T2 Starting task: Save aligned_../../test4/magick/preview_0054.jpg

No error is printed although storing the file fails, focus-stack continues. Using --save-steps instead stores the files in pwd as expected.

PetteriAimonen commented 3 years ago

Thanks for noticing that, the relative path bug should be fixed now. One can also use --output=..../foo_ to set the path and prefix for output with --align_only.

mviereck commented 3 years ago

One can also use --output=..../foo_ to set the path and prefix for output with --align_only

Great, that is useful!

Somehow the autocrop does not work anymore, cloned from git some minutes ago. It worked with a previous version some hours ago.

PetteriAimonen commented 3 years ago

The automatic cropping currently crops relative to the reference frame, which is by default the middle one but can be set with --reference. This does leave a bit of the extrapolated edge on some of the images.

Cropping to the smallest frame instead could help, that may be a part of #2 also.

mviereck commented 3 years ago

Cropping to the smallest frame instead could help

I'd say that is the correct behaviour to avoid artefacts. However, currently no crop at all is applied. The resulting images always have the same size as their source.

It worked with a previous version some hours ago.

I am not sure about this anymore; maybe I've used images already cropped by other tools.

mviereck commented 1 year ago

One can also use --output=..../foo_ to set the path and prefix for output with --align_only.

Is there a way to set the output file format, too? Say, setting the suffix to .tif?

PetteriAimonen commented 1 year ago

Is there a way to set the output file format, too? Say, setting the suffix to .tif?

Currently no separate way, but it uses the same format as the input image.

mviereck commented 1 year ago

Currently no separate way, but it uses the same format as the input image.

Yes, I see this. I have several sets of jpg source images, but would like to store them as tif to avoid loss of image quality. (Even quality 100% for jpg is said to loose a bit.) Of course I can convert the images to tif myself before aligning them. However, it would be nice if focus-stack could do this itself, so I would save same CPU power / time.

PetteriAimonen commented 1 year ago

Yeah, currently the "focus-stack as alignment tool" is just quickly bolted on feature, not really very much thought of. I don't have time to work on it myself.

mviereck commented 1 year ago

Yeah, currently the "focus-stack as alignment tool" is just quickly bolted on feature, not really very much thought of.

It is a really great feature! I quite appreciate that you have introduced it. Thank you very much!

Those details like "specify image output format" are just nitpicking where I suggest a possible improvement, but can live well with it "as is".