imglib / imglib2-algorithm

Image processing algorithms for ImgLib2
http://imglib2.net/
Other
22 stars 20 forks source link

Block algorithms: type conversion and downsampling #97

Closed tpietzsch closed 1 year ago

tpietzsch commented 1 year ago

This PR provides

1) A framework for implementing block-processing algorithms that build off the PrimitiveBlocks utility introduced in imglib2 6.2.0. Package net.imglib2.algorithm.block contains helpers to define and chain block operators. In particular, the BlockProcessor<I,O> interface specifies an algorithm that computes values in a flattened primitive output array (type O) from values in a flattened primitive input array (type I). To avoid mistakes in unchecked casting of primitive array types, BlockProcessor should be typically wrapped in UnaryBlockOperator<S,T> where S and T are source and target ImgLib2 RealType corresponding to the respective I and O array types. UnaryBlockOperator can be chained using UnaryBlockOperator.andThen(...).

Block algorithms are intended to run single-threaded. Multi-threading is achieved on a coarser level by using them in CellLoaders for CahcedCellImg and creating multiple cells in parallel.

2) An operator to convert between common RealTypes (signed and unsigned, with and without clamping). This code is partially auto-generated using the bin/generate.groovy script lifted from scijava-ops.

3) An operator to downsample, by factor 2, blocks of common RealType. It can be specified whether intermediate calculations should happen in FLOAT or DOUBLE precision (or AUTOmatically determined). There are versions for downsampling 2x2x2... blocks with half-pixel offset and 3x3x3... blocks on pixel centers.

Together with PrimitiveBlocks from imglib2 core, the downsampling algorithm achieves speedup of factor ~4 over the downsampling used in bigdataviewer-core, and speedup of factor ~10 to ~30 over the downsampling used in BigStitcher (depending on Java version, block size, etc). (This is on ArrayImg inputs. Speedups are probably even larger for CellImg or PlanarImg inputs, where PrimitveBlocks has a larger impact.)

I will make PRs to BigStitcher and bigdataviewer-core when this is merged and released.