{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Slice Op"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Added new repo: scijava.public\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "",
"version_major": 2,
"version_minor": 0
},
"method": "display_data"
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "05a6fea8-2b61-4c7a-988a-0aae24d0cb8f",
"version_major": 2,
"version_minor": 0
},
"method": "display_data"
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"net.imagej.ImageJ@551883b6"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"//load ImageJ\n",
"%classpath config resolver scijava.public https://maven.scijava.org/content/groups/public\n",
"%classpath add mvn net.imagej imagej 2.0.0-rc-71\n",
"\n",
"//create ImageJ object\n",
"ij = new net.imagej.ImageJ()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This op slices 3+ dimensional inputs and outputs up into sections, allowing for independent processing for each section before rejoining the sections at the end. Let's see how the `Op` is called:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Available operations:\n",
"\t(RandomAccessibleInterval out) =\n",
"\tnet.imagej.ops.slice.SliceRAI2RAI(\n",
"\t\tRandomAccessibleInterval out,\n",
"\t\tRandomAccessibleInterval in,\n",
"\t\tUnaryComputerOp op,\n",
"\t\tint[] axisIndices,\n",
"\t\tboolean dropSingleDimensions?)\n",
"\t(ImgPlus out) =\n",
"\tnet.imagej.ops.transform.crop.CropImgPlus(\n",
"\t\tImgPlus in1,\n",
"\t\tInterval in2,\n",
"\t\tboolean dropSingleDimensions?)\n",
"\t(RandomAccessibleInterval out) =\n",
"\tnet.imagej.ops.transform.crop.CropRAI(\n",
"\t\tRandomAccessibleInterval in1,\n",
"\t\tInterval in2,\n",
"\t\tboolean dropSingleDimensions?)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ij.op().help('slice')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's take a quick look at the parameters:\n",
"\n",
"* `RandomAccessibleInterval out`: the output\n",
"* `RandomAccessibleInterval in`: the input\n",
"* `UnaryComputerOp op`: an `Op` instance that describes how the slices are going to be processed.\n",
"* `int[] axidIndices`: the axes that define the plane, cube, or hypercube that we want our slices to look like. The axes not present in this list will be iterated through.\n",
"* `boolean dropSingleDimensions`: if true, discard all dimensions of size one present in the slices. This parameters is **not** required.\n",
"\n",
"Let's see an example of where `slice()` can be useful:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"input = ij.scifio().datasetIO().open(\"http://imagej.net/images/clown.png\")\n",
"ij.notebook().display(input)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's say that we want to do a Gaussian on this image:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[320, 200, 3]\n"
]
},
{
"data": {
"text/html": [
"
Original | Gaussed |
---|
| |
"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sigma = 1\n",
"\n",
"output = ij.op().create().img(input) \n",
" \n",
"ij.op().run(\"gauss\", output, input, sigma)\n",
"\n",
"dims = new long[output.numDimensions()]\n",
"output.dimensions(dims)\n",
"println(dims)\n",
"\n",
"ij.notebook().display([[\"Original\" : input, \"Gaussed\" : output]])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As you can see, calling a plain old `gauss()` on this image washes out the channels, leaving a bit to be desired. If we use `slice()` to perform the `gauss()` we can keep the color intensity while still blurring the features. Note that `axes` contains the dimensions that we **want** to make slices of, **not** the dimensions that we want to iterate through. "
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"Original | Gauss Features |
---|
| |
"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import net.imagej.ops.special.computer.Computers\n",
"import net.imagej.ops.Ops\n",
"\n",
"gaussOp = Computers.unary(ij.op(), Ops.Filter.Gauss.class, output, input, sigma)\n",
"\n",
"axes = [0, 1] as int[]\n",
"\n",
"ij.op().run(\"slice\", output, input, gaussOp, axes)\n",
"\n",
"ij.notebook().display([[\"Original\" : input, \"Gauss Features\" : output]])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Ah, nice and colorful. If we wanted to instead call `gauss()` on the color channels, keeping the image details nice and sharp, we could replace `axes` with `[2]`:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"Original | Gauss Channels |
---|
| |
"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"newAxes = [2] as int[]\n",
"\n",
"ij.op().run(\"slice\", output, input, gaussOp, newAxes)\n",
"\n",
"ij.notebook().display([[\"Original\" : input, \"Gauss Channels\" : output]])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Groovy",
"language": "groovy",
"name": "groovy"
},
"language_info": {
"codemirror_mode": "groovy",
"file_extension": ".groovy",
"mimetype": "",
"name": "Groovy",
"nbconverter_exporter": "",
"version": "2.4.3"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": false,
"sideBar": false,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": false,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}