{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Mixed World: Using ImageJ 1.x\n",
"Familiar with [ImageJ 1.x](https://imagej.net/ImageJ1)? Want to mix and match? Here's how."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Enabling ImageJ 1.x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In order to make use of [ImageJ 1.x](https://imagej.net/ImageJ1) functionality, we need to ensure the [ImageJ Legacy](https://imagej.net/ImageJ_Legacy) component is present on the runtime classpath."
]
},
{
"cell_type": "code",
"execution_count": 1,
"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": "ca32687d-47f0-48ca-b3d3-0e0b98b246d9",
"version_major": 2,
"version_minor": 0
},
"method": "display_data"
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%classpath config resolver scijava.public https://maven.scijava.org/content/groups/public\n",
"%%classpath add mvn\n",
"net.imagej imagej-legacy 0.35.0\n",
"net.imagej imagej 2.0.0-rc-71"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"ImageJ v2.0.0-rc-71/1.52i is ready to go."
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ij = new net.imagej.ImageJ()\n",
"\"ImageJ v${ij.getVersion()} is ready to go.\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ImageJ2 patches ImageJ 1.x so that it can run [headless](https://imagej.net/Headless). However, depending on your environment, the BeakerX kernels may not launch the JVM in headless mode by default.\n",
"\n",
"Let's check whether we are running headless now:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "307b2f59-98c7-4fa7-80c8-e2022a6342f4",
"version_major": 2,
"version_minor": 0
},
"method": "display_data"
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"[\"System property\": Boolean.getBoolean(\"java.awt.headless\"),\n",
"\"UIService\": ij.ui().isHeadless(),\n",
"\"GraphicsEnvironment\": java.awt.GraphicsEnvironment.isHeadless()]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Calling ImageJ 1.x directly"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For the most part, you can call the ImageJ1 API directly as desired:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"// From: https://commons.wikimedia.org/wiki/File:Julia_set_for_f(z)%3D_z%5E14-z.png\n",
"juliaIJ1 = ij.IJ.openImage(\"https://upload.wikimedia.org/wikipedia/commons/thumb/e/e6/Julia_set_for_f%28z%29%3D_z%5E14-z.png/120px-Julia_set_for_f%28z%29%3D_z%5E14-z.png\")\n",
"juliaIJ1.getImage()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Converting images"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The SciJava `ConvertService` can be used to convert an ImageJ1 `ImagePlus` to an ImageJ2 `Dataset`:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"juliaIJ2 = ij.convert().convert(juliaIJ1, net.imagej.Dataset.class)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It is also possible to convert ImageJ2 `Dataset` objects to ImageJ1 `ImagePlus`:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"// From: https://commons.wikimedia.org/wiki/File:Mandelbrot_Grayscale_Contours.png\n",
"fractalIJ2 = ij.io().open(\"https://upload.wikimedia.org/wikipedia/commons/thumb/b/b8/Mandelbrot_Grayscale_Contours.png/120px-Mandelbrot_Grayscale_Contours.png\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"stats[count=11520, mean=59.86666666666667, min=9.0, max=212.0]"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fractalIJ1 = ij.convert().convert(fractalIJ2, ij.ImagePlus.class)\n",
"fractalIJ1.getAllStatistics()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "741757c0-d62a-44bc-970a-7f7794a96c9c",
"version_major": 2,
"version_minor": 0
},
"method": "display_data"
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"[\"Count\": ij.op().stats().size(fractalIJ2).getRealDouble(),\n",
" \"Mean\": ij.op().stats().mean(fractalIJ2).getRealDouble(),\n",
" \"Min\": ij.op().stats().min(fractalIJ2).getRealDouble(),\n",
" \"Max\": ij.op().stats().max(fractalIJ2).getRealDouble()]"
]
}
],
"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": true,
"sideBar": true,
"skip_h1_title": true,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {
"height": "calc(100% - 180px)",
"left": "10px",
"top": "150px",
"width": "307px"
},
"toc_section_display": true,
"toc_window_display": true
}
},
"nbformat": 4,
"nbformat_minor": 2
}