Created
January 30, 2020 20:23
-
-
Save hanslovsky/e26c5da895482f74987a83e8e434f6ea to your computer and use it in GitHub Desktop.
kscript example for how to create ImgLib2 cached cell images and show them in BDV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env kscript | |
@file:MavenRepository("scijava.public", "https://maven.scijava.org/content/groups/public") | |
@file:DependsOn("sc.fiji:bigdataviewer-vistools:1.0.0-beta-11") | |
import kotlin.math.pow | |
import bdv.util.BdvFunctions | |
import bdv.util.volatiles.VolatileViews | |
import net.imglib2.Localizable | |
import net.imglib2.cache.img.CachedCellImg | |
import net.imglib2.cache.img.RandomAccessibleCacheLoader | |
import net.imglib2.cache.ref.SoftRefLoaderCache | |
import net.imglib2.img.basictypeaccess.AccessFlags | |
import net.imglib2.img.basictypeaccess.array.DoubleArray | |
import net.imglib2.img.basictypeaccess.volatiles.array.VolatileDoubleArray | |
import net.imglib2.img.cell.Cell | |
import net.imglib2.img.cell.CellGrid | |
import net.imglib2.position.FunctionRandomAccessible | |
import net.imglib2.type.numeric.RealType | |
import net.imglib2.type.numeric.real.DoubleType | |
import java.util.function.BiConsumer | |
import java.util.function.Supplier | |
// choose some values | |
fun RealType<*>.pow(power: Double) = setReal(getRealDouble().pow(power)) | |
fun Localizable.prod(target: RealType<*>, power: Double, factor: Double = 500.0) { | |
target.setOne() | |
for (d in 0 until this.numDimensions()) | |
target.mul(factor * this.getDoublePosition(d)) | |
target.pow(power) | |
} | |
val nDim = 3 | |
val power = 1.0 / nDim.toDouble() | |
val image = FunctionRandomAccessible( | |
nDim, | |
Supplier { BiConsumer { l: Localizable, t: DoubleType -> l.prod(t, power) } }, | |
Supplier { DoubleType() }) | |
val grid = CellGrid(longArrayOf(175, 225, 200), intArrayOf(16, 16, 16)) | |
val loader = RandomAccessibleCacheLoader.get<DoubleType, DoubleArray, VolatileDoubleArray>( | |
grid, | |
image, | |
AccessFlags.setOf(AccessFlags.VOLATILE)) | |
val cachedImage = CachedCellImg( | |
grid, | |
DoubleType(), | |
SoftRefLoaderCache<Long, Cell<VolatileDoubleArray>>().withLoader(loader), | |
VolatileDoubleArray(1, true)) | |
BdvFunctions.show(VolatileViews.wrapAsVolatile(cachedImage), "image") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment