Skip to content

Instantly share code, notes, and snippets.

@hanslovsky
Last active February 1, 2017 17:00
Show Gist options
  • Save hanslovsky/3e36f44b4a6ccdbbd88133ad8a816316 to your computer and use it in GitHub Desktop.
Save hanslovsky/3e36f44b4a6ccdbbd88133ad8a816316 to your computer and use it in GitHub Desktop.
Z-Position Correction Matrix Wipeoiut
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": []
},
{
"name": "stderr",
"output_type": "stream",
"text": []
}
],
"source": [
"@file:DependsOn(\"net.imagej:ij:1.51g\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": []
},
{
"name": "stderr",
"output_type": "stream",
"text": []
}
],
"source": [
"@file:DependsOn(\"net.imglib2:imglib2:3.2.1\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": []
},
{
"name": "stderr",
"output_type": "stream",
"text": []
}
],
"source": [
"@file:DependsOn(\"sc.fiji:z_spacing:1.0.3-SNAPSHOT\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": []
},
{
"name": "stderr",
"output_type": "stream",
"text": []
}
],
"source": [
"@file:DependsOn(\"de.hanslovsky:z_spacing-experiments:0.0.1-SNAPSHOT\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": []
},
{
"name": "stderr",
"output_type": "stream",
"text": []
}
],
"source": [
"@file:DependsOn(\"net.imglib2:imglib2-ij:2.0.0-beta-35\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": []
},
{
"name": "stderr",
"output_type": "stream",
"text": []
}
],
"source": [
"@file:DependsOn(\"net.imglib2:imglib2-realtransform:2.0.0-beta-34\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": []
},
{
"name": "stderr",
"output_type": "stream",
"text": []
}
],
"source": [
"import java.io.File;\n",
"import java.util.stream.IntStream;\n",
"\n",
"import org.janelia.thickness.inference.InferFromMatrix;\n",
"import org.janelia.thickness.inference.Options;\n",
"import org.janelia.thickness.inference.fits.GlobalCorrelationFitAverage;\n",
"import org.janelia.thickness.lut.LUTRealTransform;\n",
"\n",
"import ij.ImageJ;\n",
"import ij.ImagePlus;\n",
"import ij.io.FileSaver;\n",
"import ij.process.FloatProcessor;\n",
"\n",
"import net.imglib2.RandomAccessibleInterval;\n",
"import net.imglib2.converter.Converter;\n",
"import net.imglib2.converter.Converters;\n",
"import net.imglib2.converter.RealARGBConverter;\n",
"import net.imglib2.img.array.ArrayImg;\n",
"import net.imglib2.img.array.ArrayImgs;\n",
"import net.imglib2.img.basictypeaccess.array.FloatArray;\n",
"import net.imglib2.img.display.imagej.ImageJFunctions;\n",
"import net.imglib2.interpolation.randomaccess.NLinearInterpolatorFactory;\n",
"import net.imglib2.realtransform.RealViews;\n",
"import net.imglib2.type.numeric.ARGBType;\n",
"import net.imglib2.type.numeric.real.FloatType;\n",
"import net.imglib2.view.IntervalView;\n",
"import net.imglib2.view.Views;\n",
"\n",
"import de.hanslovsky.zspacing.experiments.wipeout.WipeMatrix;"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": []
},
{
"name": "stderr",
"output_type": "stream",
"text": []
}
],
"source": [
"val o = Options.generateDefaultOptions();\n",
"o.comparisonRange = 50;\n",
"o.nIterations = 100;\n",
"o.withReorder = false;\n",
"o.shiftProportion = 0.6;\n",
"o.minimumSectionThickness = 1e-9;\n",
"o.regularizationType = InferFromMatrix.RegularizationType.BORDER;\n",
"o.scalingFactorRegularizerWeight = 0.1;\n",
"o.coordinateUpdateRegularizerWeight = 0.0;\n",
"o.scalingFactorEstimationIterations = 10;\n",
"o.forceMonotonicity = true; // very important here!\n",
"\n",
"val matrixPath = \"https://github.com/hanslovsky/z-spacing-experiments/raw/master/resources/matrix.tif\";\n",
"val wipeStart = 200;\n",
"val wipeStop = 300;"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"net.imglib2.converter.read.ConvertedRandomAccessibleInterval\n",
"(r=255,g=255,b=255,a=255)\n",
"1.0\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": []
},
{
"data": {
"text/plain": [
"true"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"val fp1 = ImagePlus( matrixPath ).getProcessor().convertToFloatProcessor();\n",
"val fp2 = ImagePlus( matrixPath ).getProcessor().convertToFloatProcessor();\n",
"val w = fp1.getWidth();\n",
"val h = fp1.getHeight();\n",
"\n",
"WipeMatrix.wipe( fp2, o.comparisonRange, wipeStart, wipeStop );\n",
"\n",
"val m1 = ArrayImgs.floats( fp1.getPixels() as FloatArray, w.toLong(), h.toLong() );\n",
"val m2 = ArrayImgs.floats( fp2.getPixels() as FloatArray, w.toLong(), h.toLong() );\n",
"\n",
"val conv = RealARGBConverter<FloatType>();\n",
"\n",
"val c1 = Converters.convert( m1 as RandomAccessibleInterval<FloatType>, conv, ARGBType() );\n",
"val c2 = Converters.convert( m2 as RandomAccessibleInterval<FloatType>, conv, ARGBType() );\n",
"println(c1.javaClass.name);\n",
"println(c1.randomAccess().get());\n",
"println(m1.randomAccess().get());\n",
"\n",
"// val p1 = File.createTempFile(\"z-spacing\", \".png\", File(\".\")).getPath();\n",
"// val p2 = File.createTempFile(\"z-spacing\", \".png\", File(\".\")).getPath();\n",
"// How can I access p1, p2 from markdown cell? For now, just save as z-spacing-matrix-1/2.png\n",
"\n",
"FileSaver( ImageJFunctions.wrap( c1, \"\" ) ).saveAsPng( \"z-spacing-matrix-1.png\" );\n",
"FileSaver( ImageJFunctions.wrap( c2, \"\" ) ).saveAsPng( \"z-spacing-matrix-2.png\" );\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<tr>\n",
"<td><img src=\"https://gist.github.com/hanslovsky/3e36f44b4a6ccdbbd88133ad8a816316/raw/7fd57f4caa598021ef472b74c212cb19eeadf379/z-spacing-matrix-1.png\" title=\"Matrix\" alt=\"matrix\" style=\"width: 600px;\"/></td>\n",
"<td><img src=\"https://gist.github.com/hanslovsky/3e36f44b4a6ccdbbd88133ad8a816316/raw/7fd57f4caa598021ef472b74c212cb19eeadf379/z-spacing-matrix-2.png\" title=\"Wipeout Matrix\" alt=\"wipeout matrix\" style=\"width: 600px;\"/></td>\n",
"</tr>"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Running inference...\tDone\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": []
}
],
"source": [
"val inf = InferFromMatrix( GlobalCorrelationFitAverage() );\n",
"\n",
"val startingCoordinates = IntStream.range( 0, w ).mapToDouble( { i -> i.toDouble() } ).toArray();\n",
"\n",
"print(\"Running inference...\\t\");\n",
"val t1 = inf.estimateZCoordinates( m1, startingCoordinates.clone(), o );\n",
"val t2 = inf.estimateZCoordinates( m2, startingCoordinates.clone(), o );\n",
"println(\"Done\")"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": []
},
{
"name": "stderr",
"output_type": "stream",
"text": []
},
{
"data": {
"text/plain": [
"true"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"val tf1 = LUTRealTransform( t1, 2, 2 );\n",
"val tf2 = LUTRealTransform( t2, 2, 2 );\n",
"\n",
"val fac = NLinearInterpolatorFactory<FloatType>();\n",
"val oob = FloatType( Float.NaN );\n",
"\n",
"val o1 = Views.interval( Views.raster( RealViews.transform( Views.interpolate( Views.extendValue( m1, oob ), fac ), tf1 ) ), m1 );\n",
"val o2 = Views.interval( Views.raster( RealViews.transform( Views.interpolate( Views.extendValue( m1, oob ), fac ), tf2 ) ), m2 );\n",
"val o3 = Views.interval( Views.raster( RealViews.transform( Views.interpolate( Views.extendValue( m2, oob ), fac ), tf2 ) ), m2 );\n",
"\n",
"val cc1 = Converters.convert( o1 as RandomAccessibleInterval<FloatType>, conv, ARGBType() );\n",
"val cc2 = Converters.convert( o2 as RandomAccessibleInterval<FloatType>, conv, ARGBType() );\n",
"val cc3 = Converters.convert( o3 as RandomAccessibleInterval<FloatType>, conv, ARGBType() );\n",
"\n",
"FileSaver( ImageJFunctions.wrap( cc1, \"\" ) ).saveAsPng( \"z-spacing-matrix-out-1.png\" );\n",
"FileSaver( ImageJFunctions.wrap( cc2, \"\" ) ).saveAsPng( \"z-spacing-matrix-out-2.png\" );\n",
"FileSaver( ImageJFunctions.wrap( cc3, \"\" ) ).saveAsPng( \"z-spacing-matrix-out-3.png\" );"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<tr>\n",
"<td><img src=\"https://gist.github.com/hanslovsky/3e36f44b4a6ccdbbd88133ad8a816316/raw/7fd57f4caa598021ef472b74c212cb19eeadf379/z-spacing-matrix-out-1.png\" alt=\"tf matrix\" title=\"Transformed matrix\" style=\"width: 400px;\"/></td>\n",
"<td><img src=\"https://gist.github.com/hanslovsky/3e36f44b4a6ccdbbd88133ad8a816316/raw/7fd57f4caa598021ef472b74c212cb19eeadf379/z-spacing-matrix-out-2.png\" alt=\"tf matrix wo tf\" title=\"Transformed matrix with wipeout transform\" style=\"width: 400px;\"/></td>\n",
"<td><img src=\"https://gist.github.com/hanslovsky/3e36f44b4a6ccdbbd88133ad8a816316/raw/7fd57f4caa598021ef472b74c212cb19eeadf379/z-spacing-matrix-out-3.png\" alt=\"tf wo matrix\" title=\"Transformed wipeout matrix\" style=\"width: 400px;\"/></td>\n",
"</tr>"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Kotlin",
"language": "kotlin",
"name": "kotlin"
},
"language_info": {
"file_extension": "kt",
"name": "kotlin"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment