{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Integral Sum" ] }, { "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": "7394a39f-123a-4593-8120-f7125010864e", "version_major": 2, "version_minor": 0 }, "method": "display_data" }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "net.imagej.ImageJ@3baf5137" ] }, "execution_count": 1, "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-67\n", "\n", "//create ImageJ object\n", "ij = new net.imagej.ImageJ()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This `Op` calculates the sum of a subregion of an `Img` using a [Summed-area table](https://en.wikipedia.org/wiki/Summed-area_table), a procedure that . This notebook will differ from the flow of most of the other `stats` `Op`s since this `Op` is a little more complicated; let's call `help()` to see how it is called:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Available operations:\n", "\t(DoubleType out?) =\n", "\tnet.imagej.ops.stats.IntegralSum(\n", "\t\tDoubleType out?,\n", "\t\tRectangleNeighborhood in)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ij.op().help('integralSum')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As you can see the `Op` takes a [`RectangleNeighborhood`] (INSERT LINK) as input. This is because the `Op` calculated the sum over a given subregion of the `Img`, something that a `RectangleNeighborhood` is designed to provide. Let's start by creating an `Img`:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgA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uWdavPmzZhSKZV3qi1btmBKpVTeqbZu3YoplVJ5p9q2bRumVErlnWr79u2YUimVd6odO3ZgSqVU3ql27tyJKZVSeafatWsXplRK5Z1q9+7dmFIplXeqPXv2YEqlVN6p9u7diymVUnmn2rdvH6ZUSuWdav/+/ZhSKZV3qgMHDmBKpVTeqQ4ePIgplVJ5pzp06BCmVErlnerw4cOYUimVd6ojR45gSqVU3qmOHj2KKZVSeac6duwYplRK5Z3q+PHjmFIplXeqEydOYEqlVN6pTp48iSmVUnmnOnXqFKZUSuWd6vTp05hSKZV3qjNnzmBKpVTeqc6ePYsplVJ5pzp37hymVErlner8+fOYUimVd6oLFy5gSqVU3qkuXryIKZVSeae6dOkSplRK5Z3q8uXLmFIplXeqK1euYEqlVN6prl69iimVUnmnunbtGqZUSuWd6vr165hSKZV3qhs3bmBKpVTeqW7evIkplVJ5p7p16xamVErlner27duYUimVd6o7d+5gSqVU3qnu3r2LKZVSeae6d+8eplRK5Z3q/v37mFIplXeqBw8eYEqlVN6pHj58iCmVUnmnevToEaZUSuWd6vHjx5hSKZV3qidPnmBKpVTeqZ4+fYoplVJ5p3r27BmmVErlner58+eYUimVd6oXL15gSqVU3qlevnyJKZVSead69eoVplRK5Z3q9evXmFIplXeqN2/eYEqlVN6p3r59iymVUnmnevfuHaZUSuWd6v3795hSKZV3qg8fPmBKpVTeqT5+/IgplVJ5p/r06ROmVErlnerz58+YUimVd6ovX75gSqVU3qm+fv2KKZVSeaf69u0bplRK5Z3q+/fvmFIplXeqHz9+YEqlVN6pfv78iSmVUnmn+vXrF6ZUSuWd6vfv35hSKZV3qj9//mBKpVTeqf7+/YsplVJ5p/r37x+mVErlnep//wEMg/TRCqFHUAAAAABJRU5ErkJggg==" }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ramp = ij.op().run(\"create.img\", [150, 100])\n", "formula = \"p[0] + p[1]\"\n", "ij.op().image().equation(ramp, formula)\n", "\n", "ij.notebook().display(ramp)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now that we have an `Img` we can create a `RectangleNeighborhood` (using a `RectangleNeighborhoodFactory`) to pass through to the `Op`:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "net.imglib2.algorithm.neighborhood.RectangleNeighborhood@13a113d5" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import net.imglib2.algorithm.neighborhood.RectangleNeighborhood\n", "\n", "//our current position in the neighborhood. This is not really important in this use case \n", "//since all we care about is the region as a whole (since we are summing each pixel), \n", "//however in other applications it can be useful.\n", "position = [5, 5] as long[]\n", "\n", "//The top left corner of the neighborhood\n", "min = [0, 0] as long[]\n", "\n", "//The bottom right corner of the neighborhood\n", "max = [10, 10] as long[]\n", "\n", "//An Interval object that defines the span of the image. This is, again, \n", "//not really important for our use case, ut we can pass through our ramp image since\n", "//all Imgs are Intervals\n", "span = ramp\n", "\n", "//A RandomAccess that allows us to access all of the data values of our Img\n", "randomAccess = ramp.randomAccess()\n", "\n", "neighborhood = RectangleNeighborhood.factory().create(position, min, max, span, randomAccess)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now that we have our `RectangleNeighborhood` we can pass it through to `integralSum()`:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.0" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import net.imglib2.type.numeric.real.DoubleType\n", "\n", "output = new DoubleType()\n", "\n", "ij.op().stats().integralSum(output, neighborhood)\n", "\n", "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 }