fractal-analytics-platform / fractal-tasks-core

Main tasks for the Fractal analytics platform
https://fractal-analytics-platform.github.io/fractal-tasks-core/
BSD 3-Clause "New" or "Revised" License
14 stars 6 forks source link

Improve pyramid downsampling #42

Closed jluethi closed 10 months ago

jluethi commented 2 years ago

Currently, we are using dask.coarsen to downsample, using the min of the pixels to be combined (e.g. in 2x downsampling, the min of the 4 pixels (2x2 in xy) for the lower resolution pyramid level.

Our relevant code: ``` zyx_new = da.coarsen( np.min, pyramid[level - 1][ind_chl], {1: coarsening_xy, 2: coarsening_xy}, trim_excess=True, ) ```

This has 2 downsides: 1) The pyramid gets much darker, the further we zoom out. At a plate level for 23 wells (e.g. about level 8), the wells are barely visible using this min approach.

Using the same rescaling settings, the zoomed out view of the pyramid looks like this (barely visible): ![Screenshot 2022-06-28 at 13 12 21](https://user-images.githubusercontent.com/18033446/176166041-88d5e7aa-3d31-4a75-9470-650b13207720.png)
While the zoom in looks like this: ![Screenshot 2022-06-28 at 13 12 38](https://user-images.githubusercontent.com/18033446/176166049-cbb6378a-3556-46d9-9f4d-61e7ea6c5e4b.png)

2) Just using information of the pixels to be combined can lead to some artefacts, aliasing etc. See a good discussion here: https://github.com/dask/dask-image/issues/136 We would probably want something like a gaussian pyramid or a nearest neighbor interpolation method. I have not compared the different approaches. Ome-Zarr-py implements many of them and defaults to the nearest methods using cvs2.INTER_NEAREST.

Conclusions: 1) Is there a good reason we are using np.min? Have we tried np.mean to at least avoid the dimming the further we zoom out? Are there important issue with a mean interpolation? 2) I'll do some more research to figure out what interpolation would actually be optimal and whether anyone has implemented it nicely for Dask / for Zarr files (e.g. Volker Hilsenstein)

jluethi commented 2 years ago

Using np.mean works well, solves the issue of dimming pyramids when we zoom out. Quality of the pyramids looks about like before, just with more constant intensities.

I pushed this change with ad5868a0b913e6aa29cf3d68db7694eec7ca8984.

I will research part 2 regarding better downsampling functions later, this makes for a much better demo already :)

jluethi commented 10 months ago

Closing as not relevant for now. Potentially interesting to explore in the future

jluethi commented 7 months ago

For future reference, consider mode downsampling, see: https://forum.image.sc/t/efficient-binning-of-a-labeling/93316/3?u=tibuch