NOTE: Practice notebook adapted from the following:
%classpath config resolver maven.scijava.org https://maven.scijava.org/content/groups/public
%classpath config resolver mvnLocal
Added new repo: maven.scijava.org Added new repo: mvnLocal
%%classpath add mvn
net.imglib2 imglib2 5.7.0
net.imglib2 imklib 0.1.1
net.imagej imagej 2.0.0-rc-71
import net.imglib2.RandomAccessibleInterval
import net.imglib2.img.array.ArrayImgs
import net.imglib2.type.numeric.real.DoubleType
import net.imglib2.type.numeric.RealType
import net.imglib2.type.numeric.integer.UnsignedByteType
import net.imglib2.view.Views
import net.imglib2.util.Intervals
import kotlin.math.sqrt
import kotlin.random.Random
import net.imglib2.imklib.*
import net.imglib2.imklib.extensions.*
null
// sanity check
val floats = ArrayImgs.floats(1, 2)
floats.forEach { it.setOne() }
println("${floats[0, 0]} ${floats[0, 1]}")
floats.iterable().map {it.realDouble}
1.0 1.0
[1.0, 1.0]
Warning: This is a convenience method and should be used only for sparse voxel access.
val row = ArrayImgs.doubles(
doubleArrayOf(
0.0, 1.0, 2.0),
3,1) as RandomAccessibleInterval<DoubleType>
println("${row.iterable().map { it.realDouble }}")
println(row[0, 0])
println(row[1, 0])
println(row[2, 0])
row //3 columns, 1 row
[0.0, 1.0, 2.0] 0.0 1.0 2.0
ArrayImg [3x1]
val col = ArrayImgs.doubles(
doubleArrayOf(
0.0,
1.0,
2.0),
1,3) as RandomAccessibleInterval<DoubleType>
println("${col.iterable().map { it.realDouble }}")
println(col[0, 0])
println(col[0, 1])
println(col[0, 2])
col //1 column, 3 rows
[0.0, 1.0, 2.0] 0.0 1.0 2.0
ArrayImg [1x3]
val sq = ArrayImgs.doubles(
doubleArrayOf(
0.0, 1.0, 2.0,
3.0, 4.0, 5.0,
6.0, 7.0, 8.0),
3,3) as RandomAccessibleInterval<DoubleType>
println("${sq.iterable().map { it.realDouble }}")
println("First column")
println(sq[0, 0])
println(sq[0, 1])
println(sq[0, 2])
println()
println("Second column")
println(sq[1, 0])
println(sq[1, 1])
println(sq[1, 2])
println()
println("Third column")
println(sq[2, 0])
println(sq[2, 1])
println(sq[2, 2])
sq //3 columns, 3 rows
[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0] First column 0.0 3.0 6.0 Second column 1.0 4.0 7.0 Third column 2.0 5.0 8.0
ArrayImg [3x3]
val rect = ArrayImgs.doubles(
doubleArrayOf(
0.0, 1.0, 2.0,
3.0, 4.0, 5.0),
3,2) as RandomAccessibleInterval<DoubleType>
println("${rect.iterable().map { it.realDouble }}")
println("First column")
println(rect[0, 0])
println(rect[0, 1])
println()
println("Second column")
println(rect[1, 0])
println(rect[1, 1])
println()
println("Third column")
println(rect[2, 0])
println(rect[2, 1])
rect //3 columns, 2 rows
[0.0, 1.0, 2.0, 3.0, 4.0, 5.0] First column 0.0 3.0 Second column 1.0 4.0 Third column 2.0 5.0
ArrayImg [3x2]
val img1 = ArrayImgs.doubles(
doubleArrayOf(
1.0, 2.0, 3.0, 4.0,
5.0, 6.0, 7.0, 8.0,
9.0, 10.0, 11.0, 12.0),
4, 3) as RandomAccessibleInterval<DoubleType>
val img2 = img1 + 1.0
println("${img2.iterable().map { it.realDouble }}")
val img2_1 = img1 * 3.0
println("${img2_1.iterable().map { it.realDouble }}")
[2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0] [3.0, 6.0, 9.0, 12.0, 15.0, 18.0, 21.0, 24.0, 27.0, 30.0, 33.0, 36.0]
null
(0 until img1.dimension(1)).forEach {
val img = img1[AX, it]
println("dim 1 at $it: $img ${img.iterable().map { it.realDouble }}")
}
dim 1 at 0: IntervalView [(0) -- (3) = 4] [1.0, 2.0, 3.0, 4.0] dim 1 at 1: IntervalView [(0) -- (3) = 4] [5.0, 6.0, 7.0, 8.0] dim 1 at 2: IntervalView [(0) -- (3) = 4] [9.0, 10.0, 11.0, 12.0]
null
(0 until img1.dimension(0)).forEach {
val img = img1[AX(it, it), AX]
println("dim 0 at $it: $img ${img.iterable().map { it.realDouble }}")
}
dim 0 at 0: IntervalView [(0, 0) -- (0, 2) = 1x3] [1.0, 5.0, 9.0] dim 0 at 1: IntervalView [(1, 0) -- (1, 2) = 1x3] [2.0, 6.0, 10.0] dim 0 at 2: IntervalView [(2, 0) -- (2, 2) = 1x3] [3.0, 7.0, 11.0] dim 0 at 3: IntervalView [(3, 0) -- (3, 2) = 1x3] [4.0, 8.0, 12.0]
null
val img3 = img1[AX..2]
println("${Intervals.minAsLongArray(img3).map { it }} ${Intervals.maxAsLongArray(img3).map { it }} ${img3.iterable().map { it.realDouble }}")
[0, 0] [1, 2] [1.0, 3.0, 5.0, 7.0, 9.0, 11.0]
null
// element-wise division
val img4 = img1 / img2
println("${img4.iterable().map { it.realDouble }}")
// elemnt-wise division by scalar followed by exponantiation
val img5 = (img1 / 10.0).exp()
println("${img5.iterable().map { it.realDouble }}")
// add random gaussian noise to each element
val rng = java.util.Random(100)
val img6 = img5.apply({it + 0.1*rng.nextGaussian()}, DoubleType())
[0.5, 0.6666666666666666, 0.75, 0.8, 0.8333333333333334, 0.8571428571428571, 0.875, 0.8888888888888888, 0.9, 0.9090909090909091, 0.9166666666666666, 0.9230769230769231] [1.1051709180756477, 1.2214027581601699, 1.3498588075760032, 1.4918246976412703, 1.6487212707001282, 1.8221188003905089, 2.0137527074704766, 2.225540928492468, 2.45960311115695, 2.718281828459045, 3.0041660239464334, 3.3201169227365472]
null
//Kotlin lists: x and y
/*
val x = mutableListOf(0,1,2,3,4,5,6,7,8,9)
val y = mutableListOf(0,3,9,25,75,250,750,2500,7500,25000)
Defining x and y as above throws type mismatch error:
error: Java type mismatch expected (Mutable)List<Any!>! but found MutableList<Int>. Use explicit cast
line.setX(x)
^
error: Java type mismatch expected (Mutable)List<Number!>! but found MutableList<Int>. Use explicit cast
line.setY(y)
*/
val x: MutableList<Any> = mutableListOf(0,1,2,3,4,5,6,7,8,9)
val y: MutableList<Number> = mutableListOf(0,3,9,25,75,250,750,2500,7500,25000)
println(x)
print(y)
val line = Line()
line.setX(x)
line.setY(y)
val plot = Plot()
plot.setTitle("Interactive plot generated with BeakerX's native plotting library!")
plot.setXLabel("X-label")
plot.setYLabel("Y-label")
plot.add(line)
plot
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [0, 3, 9, 25, 75, 250, 750, 2500, 7500, 25000]
val plot = Plot()
plot.setTitle("Exponential")
val line = Line()
line.setY(img5.iterable().map {it.realDouble})
line.setX(img4.iterable().map {it.realDouble})
plot.add(line)
val line2 = Line()
line2.setY(img6.iterable().map {it.realDouble})
line2.setX(img4.iterable().map {it.realDouble})
plot.add(line2)
val plot = Plot()
plot.setTitle("x/x+1")
val line = Line()
line.setY(img4.iterable().map {it.realDouble})
line.setX(img1.iterable().map {it.realDouble})
plot.add(line)
import net.imglib2.imklib.extensions.*
import net.imglib2.type.numeric.real.DoubleType
val ij = net.imagej.ImageJ()
val url = "data/butterfly_small.jpg"
val dataset = ij.io().open(url) as net.imagej.Dataset
val img = dataset.imgPlus.img
img
Dec 24, 2020 1:56:32 PM java.util.prefs.WindowsPreferences <init> WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs at root 0x80000002. Windows RegCreateKeyEx(...) returned error code 5.
[INFO] Populating metadata [INFO] Populating metadata
val asDouble = img.convertTo(DoubleType()) {s, t -> t.setReal(s.getRealDouble())}
asDouble.rotate(angle=30.0)
from https://github.com/saalfeldlab/imklib2/blob/master/notebooks/examples/00_basic-usage.ipynb