From 51d643c52719ba3042ac1b17884ff34852ecd45c Mon Sep 17 00:00:00 2001 From: Zach Pincus Date: Sat, 26 Nov 2011 00:23:03 -0500 Subject: [PATCH 01/12] Add contour-finding Bring some old code for "marching-squares" contour-finding into skimage. Only one crummy test and c code may not be up to style standards... --- skimage/find_contours/__init__.py | 171 ++++++++++++++++++ skimage/find_contours/setup.py | 23 +++ .../find_contours/tests/test_find_contours.py | 54 ++++++ 3 files changed, 248 insertions(+) create mode 100755 skimage/find_contours/__init__.py create mode 100644 skimage/find_contours/setup.py create mode 100644 skimage/find_contours/tests/test_find_contours.py diff --git a/skimage/find_contours/__init__.py b/skimage/find_contours/__init__.py new file mode 100755 index 00000000..ebfd187f --- /dev/null +++ b/skimage/find_contours/__init__.py @@ -0,0 +1,171 @@ +import numpy as np +import _find_contours + +from collections import deque + +_param_options = ('high', 'low') + +def find_contours(array, level, fully_connected='low', positive_orientation='low'): + '''Find iso-valued contours in a 2D array for a given level value. + + Uses the "marching squares" method to compute a the iso-valued contours of + the input 2D array for a particular level value. Array values are linearly + interpolated to provide better precision for the output contours. + + Parameters + ---------- + array : convertible to a 2D ndarray object + Input data in which to find isocontours. + level : float + Value along which to find contours in the array. + fully_connected : either 'low' or 'high' + Indicates whether array elements below the given level value are to + be considered fully- connected (and hence elements above the value + will only be face connected), or vice-versa. (See below for details.) + positive_orientation : either 'low' or 'high' + Indicates whether the output contours will produce + positively-oriented polygons around islands of low- or high-valued + elements. If 'low' then contours will wind counter- clockwise around + elements below the iso-value. Alternately, this means that low-valued + elements are always on the left of the contour. (See below for + details.) + + Returns + ------- + A list of contours, where each contour is an ndarray of shape (n, 2) + consisting of n (x,y) coordinates along the contour. + + The marching squares algorithm is a special case of the marching cubes + algorithm (Lorensen, William and Harvey E. Cline. Marching Cubes: A High + Resolution 3D Surface Construction Algorithm. Computer Graphics (SIGGRAPH + 87 Proceedings) 21(4) July 1987, p. 163-170). A simple explanation is + available here: http://www.essi.fr/~lingrand/MarchingCubes/algo.html + + There is a single ambiguous case in the marching squares algorithm: when + a given 2x2-element square has two high-valued and two low-valued + elements, each pair diagonally adjacent. (Where high- and low-valued is + with respect to the contour value sought.) In this case, either the + high-valued elements can be 'connected together' via a thin isthmus that + separates the low-valued elements, or vice-versa. When elements are + connected together across a diagonal, they are considered 'fully + connected' (also known as 'face+vertex-connected' or '8-connected'). Only + high-valued or low-valued elements can be fully-connected, the other set + will be considred as 'face-connected' or '4-connected'. By default, + low-valued elements are considered fully-connected; this can be altered + with the 'fully_connected' parameter. + + Output contours are not guaranteed to be closed: contours which intersect + the array edge will be left open. All other contours will be closed. (The + closed-ness of a contours can be tested by checking whether the beginning + point is the same as the end point.) + + Contours are oriented. By default, array values lower than the contour + value are to the left of the contour and values greater than the contour + value are to the right. This means that contours will wind + counter-clockwise (i.e. in 'positive orientation') around islands of + low-valued pixels. This behavior can be altered with the + 'positive_orientation' parameter. + + The order of the contours in the output list is determined by the position + of the smallest x,y (in lexicographical order) coordinate in the contour. + This is a side-effect of how the input array is traversed, but can be + relied upon.''' + + array = np.asarray(array) + if array.ndim != 2: + raise RuntimeError('Only 2D arrays are supported.') + level = float(level) + if fully_connected not in _param_options or + positive_orientation not in _param_options: + raise ValueError('Parameters "fully_connected" and' + ' "positive_orientation" must be either "high" or "low".') + point_list = _find_contours.iterate_and_store(array, level, + fully_connected == 'high') + contours = _assemble_contours(_take_2(point_list)) + if positive_orientation == 'high': + contours = [c[::-1] for c in contours] + return contours + +def _take_2(seq): + iterator = iter(seq) + while(True): + n1 = iterator.next() + n2 = iterator.next() + yield (n1, n2) + +def _assemble_contours(points_iterator): + current_index = 0 + contours = {} + starts = {} + ends = {} + for from_point, to_point in points_iterator: + # Ignore degenerate segments. + # This happens when (and only when) one vertex of the square is + # exactly the contour level, and the rest are above or below. + # This degnerate vertex will be picked up later by neighboring squares. + if from_point == to_point: continue + + tail_data = starts.get(to_point) + head_data = ends.get(from_point) + + if tail_data is not None and head_data is not None: + tail, tail_num = tail_data + head, head_num = head_data + # We need to connect these two contours. + if tail is head: + # We need to closed a contour. + # Add the end point, and remove the contour from the + # 'starts' and 'ends' dicts. + head.append(to_point) + del starts[to_point] + del ends[from_point] + else: # tail is not head + # We need to join two distinct contours. + # We want to keep the first contour segment created, so that + # the final contours are ordered left->right, top->bottom. + if tail_num > head_num: + # tail was created second. Append tail to head. + head.extend(tail) + # remove all traces of tail: + del starts[to_point] + del ends[tail[-1]] + del contours[tail_num] + # remove the old end of head and add the new end. + del ends[from_point] + ends[head[-1]] = (head, head_num) + else: # tail_num <= head_num + # head was created second. Prepend head to tail. + tail.extendleft(reversed(head)) + # remove all traces of head: + del starts[head[0]] + del ends[from_point] + del contours[head_num] + # remove the old start of tail and add the new start. + del starts[to_point] + starts[tail[0]] = (tail, tail_num) + elif tail_data is None and head_data is None: + # we need to add a new contour + current_index += 1 + new_num = current_index + new_contour = deque((from_point, to_point)) + contours[new_num] = new_contour + starts[from_point] = (new_contour, new_num) + ends[to_point] = (new_contour, new_num) + elif tail_data is not None and head_data is None: + tail, tail_num = tail_data + # We've found a single contour to which the new segment should be + # prepended. + tail.appendleft(from_point) + del starts[to_point] + starts[from_point] = (tail, tail_num) + elif tail_data is None and head_data is not None: + head, head_num = head_data + # We've found a single contour to which the new segment should be + # appended + head.append(to_point) + del ends[from_point] + ends[to_point] = (head, head_num) + # end iteration over from_ and to_ points + + return [np.array(contour) for (num, contour) in sorted(contours.items())] + \ No newline at end of file diff --git a/skimage/find_contours/setup.py b/skimage/find_contours/setup.py new file mode 100644 index 00000000..10c5d0cf --- /dev/null +++ b/skimage/find_contours/setup.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python + +def configuration(parent_package='', top_path=None): + from numpy.distutils.misc_util import Configuration, get_numpy_include_dirs + + config = Configuration('find_contours', parent_package, top_path) + config.add_data_dir('tests') + + + config.add_extension('_find_contours', sources=['_find_contours.c'], + include_dirs=[get_numpy_include_dirs()]) + + return config + +if __name__ == '__main__': + from numpy.distutils.core import setup + setup(maintainer = 'scikits.image Developers', + maintainer_email = 'scikits-image@googlegroups.com', + description = 'Graph-based Image-processing Algorithms', + url = 'https://github.com/scikits-image/scikits.image', + license = 'Modified BSD', + **(configuration(top_path='').todict()) + ) diff --git a/skimage/find_contours/tests/test_find_contours.py b/skimage/find_contours/tests/test_find_contours.py new file mode 100644 index 00000000..7a16dded --- /dev/null +++ b/skimage/find_contours/tests/test_find_contours.py @@ -0,0 +1,54 @@ +import numpy as np +from numpy.testing import * + +from skimage.find_contours import find_contours + +a = np.ones((8,8), dtype=np.float32) +a[1:-1, 1] = 0 +a[1, 1:-1] = 0 + +## array([[ 1., 1., 1., 1., 1., 1., 1., 1.], +## [ 1., 0., 0., 0., 0., 0., 0., 1.], +## [ 1., 0., 1., 1., 1., 1., 1., 1.], +## [ 1., 0., 1., 1., 1., 1., 1., 1.], +## [ 1., 0., 1., 1., 1., 1., 1., 1.], +## [ 1., 0., 1., 1., 1., 1., 1., 1.], +## [ 1., 0., 1., 1., 1., 1., 1., 1.], +## [ 1., 1., 1., 1., 1., 1., 1., 1.]], dtype=float32) + + +def test_find_contours(): + contours = find_contours(a, 0.5) + assert len(contours) == 1 + assert_array_equal(contours[0], + [[ 6. , 1.5], + [ 5. , 1.5], + [ 4. , 1.5], + [ 3. , 1.5], + [ 2. , 1.5], + [ 1.5, 2. ], + [ 1.5, 3. ], + [ 1.5, 4. ], + [ 1.5, 5. ], + [ 1.5, 6. ], + [ 1. , 6.5], + [ 0.5, 6. ], + [ 0.5, 5. ], + [ 0.5, 4. ], + [ 0.5, 3. ], + [ 0.5, 2. ], + [ 0.5, 1. ], + [ 1. , 0.5], + [ 2. , 0.5], + [ 3. , 0.5], + [ 4. , 0.5], + [ 5. , 0.5], + [ 6. , 0.5], + [ 6.5, 1. ], + [ 6. , 1.5]]) + + + +if __name__ == '__main__': + from numpy.testing import run_module_suite + run_module_suite() From 3e9945cf938e01541117792d28b1c3462d250e9d Mon Sep 17 00:00:00 2001 From: Zach Pincus Date: Sat, 26 Nov 2011 00:36:36 -0500 Subject: [PATCH 02/12] Make it work bug fixes to make it install --- skimage/find_contours/__init__.py | 4 ++-- skimage/setup.py | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/skimage/find_contours/__init__.py b/skimage/find_contours/__init__.py index ebfd187f..b807cd57 100755 --- a/skimage/find_contours/__init__.py +++ b/skimage/find_contours/__init__.py @@ -75,8 +75,8 @@ def find_contours(array, level, fully_connected='low', positive_orientation='low if array.ndim != 2: raise RuntimeError('Only 2D arrays are supported.') level = float(level) - if fully_connected not in _param_options or - positive_orientation not in _param_options: + if (fully_connected not in _param_options or + positive_orientation not in _param_options): raise ValueError('Parameters "fully_connected" and' ' "positive_orientation" must be either "high" or "low".') point_list = _find_contours.iterate_and_store(array, level, diff --git a/skimage/setup.py b/skimage/setup.py index 35a04f6b..690d8816 100644 --- a/skimage/setup.py +++ b/skimage/setup.py @@ -15,6 +15,7 @@ def configuration(parent_package='', top_path=None): config.add_subpackage('color') config.add_subpackage('draw') config.add_subpackage('feature') + config.add_subpackage('find_contours') def add_test_directories(arg, dirname, fnames): if dirname.split(os.path.sep)[-1] == 'tests': From 077a08038967d8b17f78e1d014b6145e023ba95f Mon Sep 17 00:00:00 2001 From: Zach Pincus Date: Sun, 27 Nov 2011 17:05:27 -0500 Subject: [PATCH 03/12] Move to measure submodule Code moved to new "measure" submodule and another test and better documentation added. --- skimage/measure/__init__.py | 1 + .../__init__.py => measure/find_contours.py} | 19 ++++++++++++++++--- skimage/{find_contours => measure}/setup.py | 2 +- .../tests/test_find_contours.py | 14 +++++++++++++- skimage/setup.py | 2 +- 5 files changed, 32 insertions(+), 6 deletions(-) create mode 100755 skimage/measure/__init__.py rename skimage/{find_contours/__init__.py => measure/find_contours.py} (89%) rename skimage/{find_contours => measure}/setup.py (91%) rename skimage/{find_contours => measure}/tests/test_find_contours.py (82%) diff --git a/skimage/measure/__init__.py b/skimage/measure/__init__.py new file mode 100755 index 00000000..c7182c20 --- /dev/null +++ b/skimage/measure/__init__.py @@ -0,0 +1 @@ +from find_contours import find_contours \ No newline at end of file diff --git a/skimage/find_contours/__init__.py b/skimage/measure/find_contours.py similarity index 89% rename from skimage/find_contours/__init__.py rename to skimage/measure/find_contours.py index b807cd57..05d97fd8 100755 --- a/skimage/find_contours/__init__.py +++ b/skimage/measure/find_contours.py @@ -41,7 +41,7 @@ def find_contours(array, level, fully_connected='low', positive_orientation='low 87 Proceedings) 21(4) July 1987, p. 163-170). A simple explanation is available here: http://www.essi.fr/~lingrand/MarchingCubes/algo.html - There is a single ambiguous case in the marching squares algorithm: when + There is a single ambiguous case in the marching squares algorithm: when a given 2x2-element square has two high-valued and two low-valued elements, each pair diagonally adjacent. (Where high- and low-valued is with respect to the contour value sought.) In this case, either the @@ -54,7 +54,7 @@ def find_contours(array, level, fully_connected='low', positive_orientation='low low-valued elements are considered fully-connected; this can be altered with the 'fully_connected' parameter. - Output contours are not guaranteed to be closed: contours which intersect + Output contours are not guaranteed to be closed: contours which intersect the array edge will be left open. All other contours will be closed. (The closed-ness of a contours can be tested by checking whether the beginning point is the same as the end point.) @@ -69,7 +69,20 @@ def find_contours(array, level, fully_connected='low', positive_orientation='low The order of the contours in the output list is determined by the position of the smallest x,y (in lexicographical order) coordinate in the contour. This is a side-effect of how the input array is traversed, but can be - relied upon.''' + relied upon. + + IMPORTANT NOTE ON COORDINATES AND VALUES: + Array coordinates/values are assumed to refer to the _center_ of the + array element. Take a simple example: [0, 1]. The interpolated position of + 0.5 in this array is midway between the 0-element (at x=0) and the + 1-element (at x=1), and thus would fall at x=0.5. + + This means that to find reasonable contours, it is best to find contours + midway between the expected "light" and "dark" values. In particular, + given a binarized array, DO NOT choose to find contours at the low or high + value of the array. This will often yield degenerate contours, especially + around structures that are a single array element wide. Instead choose + a middle value, as above.''' array = np.asarray(array) if array.ndim != 2: diff --git a/skimage/find_contours/setup.py b/skimage/measure/setup.py similarity index 91% rename from skimage/find_contours/setup.py rename to skimage/measure/setup.py index 10c5d0cf..dc4d1768 100644 --- a/skimage/find_contours/setup.py +++ b/skimage/measure/setup.py @@ -3,7 +3,7 @@ def configuration(parent_package='', top_path=None): from numpy.distutils.misc_util import Configuration, get_numpy_include_dirs - config = Configuration('find_contours', parent_package, top_path) + config = Configuration('measure', parent_package, top_path) config.add_data_dir('tests') diff --git a/skimage/find_contours/tests/test_find_contours.py b/skimage/measure/tests/test_find_contours.py similarity index 82% rename from skimage/find_contours/tests/test_find_contours.py rename to skimage/measure/tests/test_find_contours.py index 7a16dded..408a679a 100644 --- a/skimage/find_contours/tests/test_find_contours.py +++ b/skimage/measure/tests/test_find_contours.py @@ -16,8 +16,10 @@ a[1, 1:-1] = 0 ## [ 1., 0., 1., 1., 1., 1., 1., 1.], ## [ 1., 1., 1., 1., 1., 1., 1., 1.]], dtype=float32) +x,y = np.mgrid[-1:1:5j,-1:1:5j] +r = np.sqrt(x**2 + y**2) -def test_find_contours(): +def test_binary(): contours = find_contours(a, 0.5) assert len(contours) == 1 assert_array_equal(contours[0], @@ -47,6 +49,16 @@ def test_find_contours(): [ 6.5, 1. ], [ 6. , 1.5]]) +def test_float(): + contours = find_contours(r, 0.5) + assert len(contours) == 1 + assert_array_equal(contours[0], + [[ 2., 3.], + [ 1., 2.], + [ 2., 1.], + [ 3., 2.], + [ 2., 3.]]) + if __name__ == '__main__': diff --git a/skimage/setup.py b/skimage/setup.py index 690d8816..f4b09a56 100644 --- a/skimage/setup.py +++ b/skimage/setup.py @@ -15,7 +15,7 @@ def configuration(parent_package='', top_path=None): config.add_subpackage('color') config.add_subpackage('draw') config.add_subpackage('feature') - config.add_subpackage('find_contours') + config.add_subpackage('measure') def add_test_directories(arg, dirname, fnames): if dirname.split(os.path.sep)[-1] == 'tests': From 1cb10a5bd9bd6f0731b537f5c24f4fdea0878869 Mon Sep 17 00:00:00 2001 From: Zach Pincus Date: Wed, 30 Nov 2011 13:16:40 -0500 Subject: [PATCH 04/12] add _find_contours.c --- skimage/measure/_find_contours.c | 262 +++++++++++++++++++++++++++++++ 1 file changed, 262 insertions(+) create mode 100644 skimage/measure/_find_contours.c diff --git a/skimage/measure/_find_contours.c b/skimage/measure/_find_contours.c new file mode 100644 index 00000000..da4b865d --- /dev/null +++ b/skimage/measure/_find_contours.c @@ -0,0 +1,262 @@ +#include +#include "numpy/arrayobject.h" + +static char _find_contours_doc[] = +"This module defines C helper functions for find_contours"; + + +static char iterate_and_store_doc[] = +"iterate_and_store(array, level, vertex_connect_high)\n\ +\n\ +Iterate across the given array in a marching-squares fashion, looking for\n\ +segments that cross 'level'. If such a segment is found, its coordinates are\n\ +added to a growing list of segments, which is returned by the function.\n\ +if vertex_connect_high is nonzero, high-values pixels are considered to be\n\ +face+vertex connected into objects; otherwise low-valued pixels are."; + + +// Nasty macros to inline the inner loop of interpolating the position of +// the contour and add an appropriate tuple to the output list. +// These macros define blocks of code that can only be used within the inner +// loop of 'iterate_and_store' because they use variables therefrom. + +#define GET_FRACTION(from_value, to_value) \ + ((level - from_value) / (to_value - from_value)) + +#define TOP { \ + output0 = coords[0]; \ + output1 = coords[1] + GET_FRACTION(*ul_ptr, *ur_ptr); \ +} + +#define BOTTOM { \ + output0 = coords[0] + 1; \ + output1 = coords[1] + GET_FRACTION(*ll_ptr, *lr_ptr); \ +} + +#define LEFT { \ + output0 = coords[0] + GET_FRACTION(*ul_ptr, *ll_ptr); \ + output1 = coords[1]; \ +} + +#define RIGHT { \ + output0 = coords[0] + GET_FRACTION(*ur_ptr, *lr_ptr); \ + output1 = coords[1] + 1; \ +} + +#define ADD_TUPLE { \ + PyObject* tuple = Py_BuildValue("(dd)", output0, output1); \ + if (!tuple) { \ + Py_DECREF(double_array); \ + Py_DECREF(arc_list); \ + return NULL; \ + } \ + char res = PyList_Append(arc_list, tuple); \ + Py_DECREF(tuple); \ + if (res < 0) { \ + Py_DECREF(double_array); \ + Py_DECREF(arc_list); \ + return NULL; \ + } \ +} + +#define ADD_SEGMENT(START, END) { \ + double output0, output1; \ + START \ + ADD_TUPLE \ + END \ + ADD_TUPLE \ +} + +static PyObject* +iterate_and_store(PyObject *self, PyObject *args) +{ + PyObject* array; + double level; + int vertex_connect_high; + if (!PyArg_ParseTuple(args, "Odi:iterate_and_store", &array, &level, + &vertex_connect_high)) { + return NULL; + } + + PyObject* double_array = PyArray_FromAny(array, + PyArray_DescrFromType(NPY_DOUBLE), 2, 2, NPY_CONTIGUOUS | NPY_ALIGNED, + NULL); + if (!double_array) { + return NULL; + } + + npy_intp *dims = PyArray_DIMS(double_array); + if (dims[0] < 2 || dims[1] < 2) { + Py_DECREF(double_array); + PyErr_SetString(PyExc_ValueError, "Input array must be at least 2x2."); + return NULL; + } + + // The plan is to iterate a 2x2 square across the input array. This means + // that the upper-left corner of the square needs to iterate across a + // sub-array that's one-less-large in each direction (so that the square + // never steps out of bounds). The square is represented by four pointers: + // ul, ur, ll, and lr (for 'upper left', etc.). We also maintain the current + // 2D coordinates for the position of the upper-left pointer. Note that we + // ensured that the array is of type 'double' and is C-contiguous (last + // index varies the fastest). + + // Current coords start at 0,0. + npy_intp coords[2] = {0,0}; + // Precompute the size of the array minus 2 in each direction, so we'll know + // when to update the coordinates and double-increment the square pointers + // so that the upper-left pointer never visits the last column. + npy_intp dims_m2[2]; + dims_m2[0] = dims[0] - 2; + dims_m2[1] = dims[1] - 2; + // Calculate the number of iterations we'll need + npy_intp num_square_steps = (dims[0] - 1) * (dims[1] - 1); + // and set up the square pointers. + double* ul_ptr = PyArray_DATA(double_array); + double* ur_ptr = ul_ptr + 1; + double* ll_ptr = ul_ptr + dims[1]; + double* lr_ptr = ll_ptr + 1; + + // make a list to hold the returned coordinates + PyObject* arc_list = PyList_New(0); + if (!arc_list) { + Py_DECREF(double_array); + return NULL; + } + while(num_square_steps--) { + // There are sixteen different possible square types, diagramed below. + // A + indicates that the vertex is above the contour value, and a - + // indicates that the vertex is below or equal to the contour value. + // The vertices of each square are: + // ul ur + // ll lr + // and can be treated as a binary value with the bits in that order. Thus + // each square case can be numbered: + // 0-- 1+- 2-+ 3++ 4-- 5+- 6-+ 7++ + // -- -- -- -- +- +- +- +- + // + // 8-- 9+- 10-+ 11++ 12-- 13+- 14-+ 15++ + // -+ -+ -+ -+ ++ ++ ++ ++ + // + // The position of the line segment that cuts through (or doesn't, in case + // 0 and 15) each square is clear, except in cases 6 and 9. In this case, + // where the segments are placed is determined by vertex_connect_high. + // If vertex_connect_high is false, then lines like \\ are drawn + // through square 6, and lines like // are drawn through square 9. + // Otherwise, the situation is reversed. + // Finally, recall that we draw the lines so that (moving from tail to + // head) the lower-valued pixels are on the left of the line. So, for + // example, case 1 entails a line slanting from the middle of the top of + // the square to the middle of the left side of the square. + + unsigned char square_case = 0; + if ((*ul_ptr) > level) square_case += 1; + if ((*ur_ptr) > level) square_case += 2; + if ((*ll_ptr) > level) square_case += 4; + if ((*lr_ptr) > level) square_case += 8; + + switch(square_case) + { + case 0: // no line + break; + case 1: // top to left + ADD_SEGMENT(TOP, LEFT); + break; + case 2: // right to top + ADD_SEGMENT(RIGHT, TOP); + break; + case 3: // right to left + ADD_SEGMENT(RIGHT, LEFT); + break; + case 4: // left to bottom + ADD_SEGMENT(LEFT, BOTTOM); + break; + case 5: // top to bottom + ADD_SEGMENT(TOP, BOTTOM); + break; + case 6: + if (vertex_connect_high) + { + // left to top + ADD_SEGMENT(LEFT, TOP); + // right to bottom + ADD_SEGMENT(RIGHT, BOTTOM); + } + else + { + // right to top + ADD_SEGMENT(RIGHT, TOP); + // left to bottom + ADD_SEGMENT(LEFT, BOTTOM); + } + break; + case 7: // right to bottom + ADD_SEGMENT(RIGHT, BOTTOM); + break; + case 8: // bottom to right + ADD_SEGMENT(BOTTOM, RIGHT); + break; + case 9: + if (vertex_connect_high) + { + // top to right + ADD_SEGMENT(TOP, RIGHT); + // bottom to left + ADD_SEGMENT(BOTTOM, LEFT); + } + else + { + // top to left + ADD_SEGMENT(TOP, LEFT); + // bottom to right + ADD_SEGMENT(BOTTOM, RIGHT); + } + break; + case 10: // bottom to top + ADD_SEGMENT(BOTTOM, TOP); + break; + case 11: // bottom to left + ADD_SEGMENT(BOTTOM, LEFT); + break; + case 12: // left to right + ADD_SEGMENT(LEFT, RIGHT); + break; + case 13: // top to right + ADD_SEGMENT(TOP, RIGHT); + break; + case 14: // left to top + ADD_SEGMENT(LEFT, TOP); + break; + case 15: // no line + break; + } // switch square_case + + if (coords[1] < dims_m2[1]) { + coords[1]++; + } else { + coords[1] = 0; + coords[0]++; + // Double-increment pointers to advance them to the next row, since + // we're skipping the last column, as far as ul_ptr is concerned. + ul_ptr++; ur_ptr++; ll_ptr++; lr_ptr++; + } + ul_ptr++; ur_ptr++; ll_ptr++; lr_ptr++; + } // iteration + + // get rid of the double array reference that we own + Py_DECREF(double_array); + return arc_list; +} + + +static PyMethodDef _find_contours_methods[] = { + {"iterate_and_store", iterate_and_store, METH_VARARGS, iterate_and_store_doc}, + {NULL, NULL, 0, NULL} +}; + +PyMODINIT_FUNC +init_find_contours(void) +{ + Py_InitModule3("_find_contours", _find_contours_methods, _find_contours_doc); + import_array(); +} From bea1b751d2a162f9a9565ea03d21eec7bbd4ca6e Mon Sep 17 00:00:00 2001 From: Stefan van der Walt Date: Wed, 30 Nov 2011 10:55:40 -0800 Subject: [PATCH 05/12] BUG: Import find_contours from correct location. --- skimage/measure/__init__.py | 2 +- skimage/measure/tests/test_find_contours.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/skimage/measure/__init__.py b/skimage/measure/__init__.py index c7182c20..a9eb1526 100755 --- a/skimage/measure/__init__.py +++ b/skimage/measure/__init__.py @@ -1 +1 @@ -from find_contours import find_contours \ No newline at end of file +from find_contours import find_contours diff --git a/skimage/measure/tests/test_find_contours.py b/skimage/measure/tests/test_find_contours.py index 408a679a..8d705878 100644 --- a/skimage/measure/tests/test_find_contours.py +++ b/skimage/measure/tests/test_find_contours.py @@ -1,7 +1,7 @@ import numpy as np from numpy.testing import * -from skimage.find_contours import find_contours +from skimage.measure import find_contours a = np.ones((8,8), dtype=np.float32) a[1:-1, 1] = 0 From 053b27e623272b0c313d70a1fa4fcfd04af4055a Mon Sep 17 00:00:00 2001 From: Stefan van der Walt Date: Wed, 30 Nov 2011 10:55:57 -0800 Subject: [PATCH 06/12] ENH: Whitespace cleanup. --- skimage/measure/_find_contours.c | 64 ++++++++++++++++---------------- skimage/measure/find_contours.py | 49 ++++++++++++------------ 2 files changed, 56 insertions(+), 57 deletions(-) diff --git a/skimage/measure/_find_contours.c b/skimage/measure/_find_contours.c index da4b865d..e9371692 100644 --- a/skimage/measure/_find_contours.c +++ b/skimage/measure/_find_contours.c @@ -1,11 +1,11 @@ #include #include "numpy/arrayobject.h" -static char _find_contours_doc[] = +static char _find_contours_doc[] = "This module defines C helper functions for find_contours"; -static char iterate_and_store_doc[] = +static char iterate_and_store_doc[] = "iterate_and_store(array, level, vertex_connect_high)\n\ \n\ Iterate across the given array in a marching-squares fashion, looking for\n\ @@ -51,32 +51,32 @@ face+vertex connected into objects; otherwise low-valued pixels are."; return NULL; \ } \ char res = PyList_Append(arc_list, tuple); \ - Py_DECREF(tuple); \ - if (res < 0) { \ + Py_DECREF(tuple); \ + if (res < 0) { \ Py_DECREF(double_array); \ Py_DECREF(arc_list); \ - return NULL; \ - } \ + return NULL; \ + } \ } #define ADD_SEGMENT(START, END) { \ double output0, output1; \ START \ ADD_TUPLE \ - END \ - ADD_TUPLE \ + END \ + ADD_TUPLE \ } static PyObject* iterate_and_store(PyObject *self, PyObject *args) { - PyObject* array; - double level; - int vertex_connect_high; - if (!PyArg_ParseTuple(args, "Odi:iterate_and_store", &array, &level, - &vertex_connect_high)) { - return NULL; - } + PyObject* array; + double level; + int vertex_connect_high; + if (!PyArg_ParseTuple(args, "Odi:iterate_and_store", &array, &level, + &vertex_connect_high)) { + return NULL; + } PyObject* double_array = PyArray_FromAny(array, PyArray_DescrFromType(NPY_DOUBLE), 2, 2, NPY_CONTIGUOUS | NPY_ALIGNED, @@ -84,14 +84,14 @@ iterate_and_store(PyObject *self, PyObject *args) if (!double_array) { return NULL; } - + npy_intp *dims = PyArray_DIMS(double_array); if (dims[0] < 2 || dims[1] < 2) { Py_DECREF(double_array); PyErr_SetString(PyExc_ValueError, "Input array must be at least 2x2."); return NULL; - } - + } + // The plan is to iterate a 2x2 square across the input array. This means // that the upper-left corner of the square needs to iterate across a // sub-array that's one-less-large in each direction (so that the square @@ -100,7 +100,7 @@ iterate_and_store(PyObject *self, PyObject *args) // 2D coordinates for the position of the upper-left pointer. Note that we // ensured that the array is of type 'double' and is C-contiguous (last // index varies the fastest). - + // Current coords start at 0,0. npy_intp coords[2] = {0,0}; // Precompute the size of the array minus 2 in each direction, so we'll know @@ -116,7 +116,7 @@ iterate_and_store(PyObject *self, PyObject *args) double* ur_ptr = ul_ptr + 1; double* ll_ptr = ul_ptr + dims[1]; double* lr_ptr = ll_ptr + 1; - + // make a list to hold the returned coordinates PyObject* arc_list = PyList_New(0); if (!arc_list) { @@ -139,22 +139,22 @@ iterate_and_store(PyObject *self, PyObject *args) // -+ -+ -+ -+ ++ ++ ++ ++ // // The position of the line segment that cuts through (or doesn't, in case - // 0 and 15) each square is clear, except in cases 6 and 9. In this case, + // 0 and 15) each square is clear, except in cases 6 and 9. In this case, // where the segments are placed is determined by vertex_connect_high. - // If vertex_connect_high is false, then lines like \\ are drawn + // If vertex_connect_high is false, then lines like \\ are drawn // through square 6, and lines like // are drawn through square 9. // Otherwise, the situation is reversed. // Finally, recall that we draw the lines so that (moving from tail to // head) the lower-valued pixels are on the left of the line. So, for // example, case 1 entails a line slanting from the middle of the top of // the square to the middle of the left side of the square. - + unsigned char square_case = 0; if ((*ul_ptr) > level) square_case += 1; if ((*ur_ptr) > level) square_case += 2; if ((*ll_ptr) > level) square_case += 4; if ((*lr_ptr) > level) square_case += 8; - + switch(square_case) { case 0: // no line @@ -210,7 +210,7 @@ iterate_and_store(PyObject *self, PyObject *args) ADD_SEGMENT(TOP, LEFT); // bottom to right ADD_SEGMENT(BOTTOM, RIGHT); - } + } break; case 10: // bottom to top ADD_SEGMENT(BOTTOM, TOP); @@ -230,7 +230,7 @@ iterate_and_store(PyObject *self, PyObject *args) case 15: // no line break; } // switch square_case - + if (coords[1] < dims_m2[1]) { coords[1]++; } else { @@ -242,21 +242,21 @@ iterate_and_store(PyObject *self, PyObject *args) } ul_ptr++; ur_ptr++; ll_ptr++; lr_ptr++; } // iteration - + // get rid of the double array reference that we own Py_DECREF(double_array); - return arc_list; + return arc_list; } static PyMethodDef _find_contours_methods[] = { - {"iterate_and_store", iterate_and_store, METH_VARARGS, iterate_and_store_doc}, - {NULL, NULL, 0, NULL} + {"iterate_and_store", iterate_and_store, METH_VARARGS, iterate_and_store_doc}, + {NULL, NULL, 0, NULL} }; PyMODINIT_FUNC init_find_contours(void) { - Py_InitModule3("_find_contours", _find_contours_methods, _find_contours_doc); - import_array(); + Py_InitModule3("_find_contours", _find_contours_methods, _find_contours_doc); + import_array(); } diff --git a/skimage/measure/find_contours.py b/skimage/measure/find_contours.py index 05d97fd8..9ab67a06 100755 --- a/skimage/measure/find_contours.py +++ b/skimage/measure/find_contours.py @@ -7,16 +7,16 @@ _param_options = ('high', 'low') def find_contours(array, level, fully_connected='low', positive_orientation='low'): '''Find iso-valued contours in a 2D array for a given level value. - + Uses the "marching squares" method to compute a the iso-valued contours of the input 2D array for a particular level value. Array values are linearly interpolated to provide better precision for the output contours. - + Parameters - ---------- + ---------- array : convertible to a 2D ndarray object Input data in which to find isocontours. - level : float + level : float Value along which to find contours in the array. fully_connected : either 'low' or 'high' Indicates whether array elements below the given level value are to @@ -29,18 +29,18 @@ def find_contours(array, level, fully_connected='low', positive_orientation='low elements below the iso-value. Alternately, this means that low-valued elements are always on the left of the contour. (See below for details.) - + Returns - ------- + ------- A list of contours, where each contour is an ndarray of shape (n, 2) consisting of n (x,y) coordinates along the contour. - + The marching squares algorithm is a special case of the marching cubes algorithm (Lorensen, William and Harvey E. Cline. Marching Cubes: A High Resolution 3D Surface Construction Algorithm. Computer Graphics (SIGGRAPH 87 Proceedings) 21(4) July 1987, p. 163-170). A simple explanation is available here: http://www.essi.fr/~lingrand/MarchingCubes/algo.html - + There is a single ambiguous case in the marching squares algorithm: when a given 2x2-element square has two high-valued and two low-valued elements, each pair diagonally adjacent. (Where high- and low-valued is @@ -51,44 +51,44 @@ def find_contours(array, level, fully_connected='low', positive_orientation='low connected' (also known as 'face+vertex-connected' or '8-connected'). Only high-valued or low-valued elements can be fully-connected, the other set will be considred as 'face-connected' or '4-connected'. By default, - low-valued elements are considered fully-connected; this can be altered + low-valued elements are considered fully-connected; this can be altered with the 'fully_connected' parameter. - + Output contours are not guaranteed to be closed: contours which intersect the array edge will be left open. All other contours will be closed. (The closed-ness of a contours can be tested by checking whether the beginning point is the same as the end point.) - + Contours are oriented. By default, array values lower than the contour value are to the left of the contour and values greater than the contour value are to the right. This means that contours will wind counter-clockwise (i.e. in 'positive orientation') around islands of low-valued pixels. This behavior can be altered with the 'positive_orientation' parameter. - + The order of the contours in the output list is determined by the position of the smallest x,y (in lexicographical order) coordinate in the contour. This is a side-effect of how the input array is traversed, but can be relied upon. - + IMPORTANT NOTE ON COORDINATES AND VALUES: Array coordinates/values are assumed to refer to the _center_ of the array element. Take a simple example: [0, 1]. The interpolated position of 0.5 in this array is midway between the 0-element (at x=0) and the 1-element (at x=1), and thus would fall at x=0.5. - + This means that to find reasonable contours, it is best to find contours midway between the expected "light" and "dark" values. In particular, given a binarized array, DO NOT choose to find contours at the low or high value of the array. This will often yield degenerate contours, especially around structures that are a single array element wide. Instead choose a middle value, as above.''' - + array = np.asarray(array) if array.ndim != 2: raise RuntimeError('Only 2D arrays are supported.') level = float(level) - if (fully_connected not in _param_options or + if (fully_connected not in _param_options or positive_orientation not in _param_options): raise ValueError('Parameters "fully_connected" and' ' "positive_orientation" must be either "high" or "low".') @@ -98,7 +98,7 @@ def find_contours(array, level, fully_connected='low', positive_orientation='low if positive_orientation == 'high': contours = [c[::-1] for c in contours] return contours - + def _take_2(seq): iterator = iter(seq) while(True): @@ -117,24 +117,24 @@ def _assemble_contours(points_iterator): # exactly the contour level, and the rest are above or below. # This degnerate vertex will be picked up later by neighboring squares. if from_point == to_point: continue - + tail_data = starts.get(to_point) head_data = ends.get(from_point) - + if tail_data is not None and head_data is not None: tail, tail_num = tail_data head, head_num = head_data - # We need to connect these two contours. + # We need to connect these two contours. if tail is head: # We need to closed a contour. - # Add the end point, and remove the contour from the + # Add the end point, and remove the contour from the # 'starts' and 'ends' dicts. head.append(to_point) del starts[to_point] del ends[from_point] else: # tail is not head # We need to join two distinct contours. - # We want to keep the first contour segment created, so that + # We want to keep the first contour segment created, so that # the final contours are ordered left->right, top->bottom. if tail_num > head_num: # tail was created second. Append tail to head. @@ -166,7 +166,7 @@ def _assemble_contours(points_iterator): ends[to_point] = (new_contour, new_num) elif tail_data is not None and head_data is None: tail, tail_num = tail_data - # We've found a single contour to which the new segment should be + # We've found a single contour to which the new segment should be # prepended. tail.appendleft(from_point) del starts[to_point] @@ -179,6 +179,5 @@ def _assemble_contours(points_iterator): del ends[from_point] ends[to_point] = (head, head_num) # end iteration over from_ and to_ points - + return [np.array(contour) for (num, contour) in sorted(contours.items())] - \ No newline at end of file From c0b36010562000b6fd9f0c095fe6828d6d4a2cd6 Mon Sep 17 00:00:00 2001 From: Stefan van der Walt Date: Wed, 30 Nov 2011 12:23:32 -0800 Subject: [PATCH 07/12] REF: Convert find_contours to Cython. --- skimage/measure/_find_contours.c | 262 ----------------------------- skimage/measure/_find_contours.pyx | 185 ++++++++++++++++++++ skimage/measure/find_contours.py | 2 +- skimage/measure/setup.py | 6 + 4 files changed, 192 insertions(+), 263 deletions(-) delete mode 100644 skimage/measure/_find_contours.c create mode 100644 skimage/measure/_find_contours.pyx diff --git a/skimage/measure/_find_contours.c b/skimage/measure/_find_contours.c deleted file mode 100644 index e9371692..00000000 --- a/skimage/measure/_find_contours.c +++ /dev/null @@ -1,262 +0,0 @@ -#include -#include "numpy/arrayobject.h" - -static char _find_contours_doc[] = -"This module defines C helper functions for find_contours"; - - -static char iterate_and_store_doc[] = -"iterate_and_store(array, level, vertex_connect_high)\n\ -\n\ -Iterate across the given array in a marching-squares fashion, looking for\n\ -segments that cross 'level'. If such a segment is found, its coordinates are\n\ -added to a growing list of segments, which is returned by the function.\n\ -if vertex_connect_high is nonzero, high-values pixels are considered to be\n\ -face+vertex connected into objects; otherwise low-valued pixels are."; - - -// Nasty macros to inline the inner loop of interpolating the position of -// the contour and add an appropriate tuple to the output list. -// These macros define blocks of code that can only be used within the inner -// loop of 'iterate_and_store' because they use variables therefrom. - -#define GET_FRACTION(from_value, to_value) \ - ((level - from_value) / (to_value - from_value)) - -#define TOP { \ - output0 = coords[0]; \ - output1 = coords[1] + GET_FRACTION(*ul_ptr, *ur_ptr); \ -} - -#define BOTTOM { \ - output0 = coords[0] + 1; \ - output1 = coords[1] + GET_FRACTION(*ll_ptr, *lr_ptr); \ -} - -#define LEFT { \ - output0 = coords[0] + GET_FRACTION(*ul_ptr, *ll_ptr); \ - output1 = coords[1]; \ -} - -#define RIGHT { \ - output0 = coords[0] + GET_FRACTION(*ur_ptr, *lr_ptr); \ - output1 = coords[1] + 1; \ -} - -#define ADD_TUPLE { \ - PyObject* tuple = Py_BuildValue("(dd)", output0, output1); \ - if (!tuple) { \ - Py_DECREF(double_array); \ - Py_DECREF(arc_list); \ - return NULL; \ - } \ - char res = PyList_Append(arc_list, tuple); \ - Py_DECREF(tuple); \ - if (res < 0) { \ - Py_DECREF(double_array); \ - Py_DECREF(arc_list); \ - return NULL; \ - } \ -} - -#define ADD_SEGMENT(START, END) { \ - double output0, output1; \ - START \ - ADD_TUPLE \ - END \ - ADD_TUPLE \ -} - -static PyObject* -iterate_and_store(PyObject *self, PyObject *args) -{ - PyObject* array; - double level; - int vertex_connect_high; - if (!PyArg_ParseTuple(args, "Odi:iterate_and_store", &array, &level, - &vertex_connect_high)) { - return NULL; - } - - PyObject* double_array = PyArray_FromAny(array, - PyArray_DescrFromType(NPY_DOUBLE), 2, 2, NPY_CONTIGUOUS | NPY_ALIGNED, - NULL); - if (!double_array) { - return NULL; - } - - npy_intp *dims = PyArray_DIMS(double_array); - if (dims[0] < 2 || dims[1] < 2) { - Py_DECREF(double_array); - PyErr_SetString(PyExc_ValueError, "Input array must be at least 2x2."); - return NULL; - } - - // The plan is to iterate a 2x2 square across the input array. This means - // that the upper-left corner of the square needs to iterate across a - // sub-array that's one-less-large in each direction (so that the square - // never steps out of bounds). The square is represented by four pointers: - // ul, ur, ll, and lr (for 'upper left', etc.). We also maintain the current - // 2D coordinates for the position of the upper-left pointer. Note that we - // ensured that the array is of type 'double' and is C-contiguous (last - // index varies the fastest). - - // Current coords start at 0,0. - npy_intp coords[2] = {0,0}; - // Precompute the size of the array minus 2 in each direction, so we'll know - // when to update the coordinates and double-increment the square pointers - // so that the upper-left pointer never visits the last column. - npy_intp dims_m2[2]; - dims_m2[0] = dims[0] - 2; - dims_m2[1] = dims[1] - 2; - // Calculate the number of iterations we'll need - npy_intp num_square_steps = (dims[0] - 1) * (dims[1] - 1); - // and set up the square pointers. - double* ul_ptr = PyArray_DATA(double_array); - double* ur_ptr = ul_ptr + 1; - double* ll_ptr = ul_ptr + dims[1]; - double* lr_ptr = ll_ptr + 1; - - // make a list to hold the returned coordinates - PyObject* arc_list = PyList_New(0); - if (!arc_list) { - Py_DECREF(double_array); - return NULL; - } - while(num_square_steps--) { - // There are sixteen different possible square types, diagramed below. - // A + indicates that the vertex is above the contour value, and a - - // indicates that the vertex is below or equal to the contour value. - // The vertices of each square are: - // ul ur - // ll lr - // and can be treated as a binary value with the bits in that order. Thus - // each square case can be numbered: - // 0-- 1+- 2-+ 3++ 4-- 5+- 6-+ 7++ - // -- -- -- -- +- +- +- +- - // - // 8-- 9+- 10-+ 11++ 12-- 13+- 14-+ 15++ - // -+ -+ -+ -+ ++ ++ ++ ++ - // - // The position of the line segment that cuts through (or doesn't, in case - // 0 and 15) each square is clear, except in cases 6 and 9. In this case, - // where the segments are placed is determined by vertex_connect_high. - // If vertex_connect_high is false, then lines like \\ are drawn - // through square 6, and lines like // are drawn through square 9. - // Otherwise, the situation is reversed. - // Finally, recall that we draw the lines so that (moving from tail to - // head) the lower-valued pixels are on the left of the line. So, for - // example, case 1 entails a line slanting from the middle of the top of - // the square to the middle of the left side of the square. - - unsigned char square_case = 0; - if ((*ul_ptr) > level) square_case += 1; - if ((*ur_ptr) > level) square_case += 2; - if ((*ll_ptr) > level) square_case += 4; - if ((*lr_ptr) > level) square_case += 8; - - switch(square_case) - { - case 0: // no line - break; - case 1: // top to left - ADD_SEGMENT(TOP, LEFT); - break; - case 2: // right to top - ADD_SEGMENT(RIGHT, TOP); - break; - case 3: // right to left - ADD_SEGMENT(RIGHT, LEFT); - break; - case 4: // left to bottom - ADD_SEGMENT(LEFT, BOTTOM); - break; - case 5: // top to bottom - ADD_SEGMENT(TOP, BOTTOM); - break; - case 6: - if (vertex_connect_high) - { - // left to top - ADD_SEGMENT(LEFT, TOP); - // right to bottom - ADD_SEGMENT(RIGHT, BOTTOM); - } - else - { - // right to top - ADD_SEGMENT(RIGHT, TOP); - // left to bottom - ADD_SEGMENT(LEFT, BOTTOM); - } - break; - case 7: // right to bottom - ADD_SEGMENT(RIGHT, BOTTOM); - break; - case 8: // bottom to right - ADD_SEGMENT(BOTTOM, RIGHT); - break; - case 9: - if (vertex_connect_high) - { - // top to right - ADD_SEGMENT(TOP, RIGHT); - // bottom to left - ADD_SEGMENT(BOTTOM, LEFT); - } - else - { - // top to left - ADD_SEGMENT(TOP, LEFT); - // bottom to right - ADD_SEGMENT(BOTTOM, RIGHT); - } - break; - case 10: // bottom to top - ADD_SEGMENT(BOTTOM, TOP); - break; - case 11: // bottom to left - ADD_SEGMENT(BOTTOM, LEFT); - break; - case 12: // left to right - ADD_SEGMENT(LEFT, RIGHT); - break; - case 13: // top to right - ADD_SEGMENT(TOP, RIGHT); - break; - case 14: // left to top - ADD_SEGMENT(LEFT, TOP); - break; - case 15: // no line - break; - } // switch square_case - - if (coords[1] < dims_m2[1]) { - coords[1]++; - } else { - coords[1] = 0; - coords[0]++; - // Double-increment pointers to advance them to the next row, since - // we're skipping the last column, as far as ul_ptr is concerned. - ul_ptr++; ur_ptr++; ll_ptr++; lr_ptr++; - } - ul_ptr++; ur_ptr++; ll_ptr++; lr_ptr++; - } // iteration - - // get rid of the double array reference that we own - Py_DECREF(double_array); - return arc_list; -} - - -static PyMethodDef _find_contours_methods[] = { - {"iterate_and_store", iterate_and_store, METH_VARARGS, iterate_and_store_doc}, - {NULL, NULL, 0, NULL} -}; - -PyMODINIT_FUNC -init_find_contours(void) -{ - Py_InitModule3("_find_contours", _find_contours_methods, _find_contours_doc); - import_array(); -} diff --git a/skimage/measure/_find_contours.pyx b/skimage/measure/_find_contours.pyx new file mode 100644 index 00000000..f3a26f74 --- /dev/null +++ b/skimage/measure/_find_contours.pyx @@ -0,0 +1,185 @@ +# -*- python -*- +# cython: cdivision=True + +import numpy as np +cimport numpy as np + +np.import_array() + +cdef double _get_fraction(double from_value, double to_value, double level): + if (to_value == from_value): + return 0 + return ((level - from_value) / (to_value - from_value)) + + +def iterate_and_store(np.ndarray[double, ndim=2, mode='c'] array, + double level, int vertex_connect_high): + """Iterate across the given array in a marching-squares fashion, + looking for segments that cross 'level'. If such a segment is + found, its coordinates are added to a growing list of segments, + which is returned by the function. if vertex_connect_high is + nonzero, high-values pixels are considered to be face+vertex + connected into objects; otherwise low-valued pixels are. + + """ + if array.shape[0] < 2 or array.shape[1] < 2: + raise ValueError("Input array must be at least 2x2.") + + cdef list arc_list = [] + cdef int n + + # The plan is to iterate a 2x2 square across the input array. This means + # that the upper-left corner of the square needs to iterate across a + # sub-array that's one-less-large in each direction (so that the square + # never steps out of bounds). The square is represented by four pointers: + # ul, ur, ll, and lr (for 'upper left', etc.). We also maintain the current + # 2D coordinates for the position of the upper-left pointer. Note that we + # ensured that the array is of type 'double' and is C-contiguous (last + # index varies the fastest). + + # Current coords start at 0,0. + cdef int[2] coords + coords[0] = 0 + coords[1] = 0 + + # Calculate the number of iterations we'll need + cdef int num_square_steps = (array.shape[0] - 1) * (array.shape[1] - 1) + + cdef unsigned char square_case = 0 + cdef tuple top, bottom, left, right + cdef double ul, ur, ll, lr + cdef int r0, r1, c0, c1 + + for n in range(num_square_steps): + # There are sixteen different possible square types, diagramed below. + # A + indicates that the vertex is above the contour value, and a - + # indicates that the vertex is below or equal to the contour value. + # The vertices of each square are: + # ul ur + # ll lr + # and can be treated as a binary value with the bits in that order. Thus + # each square case can be numbered: + # 0-- 1+- 2-+ 3++ 4-- 5+- 6-+ 7++ + # -- -- -- -- +- +- +- +- + # + # 8-- 9+- 10-+ 11++ 12-- 13+- 14-+ 15++ + # -+ -+ -+ -+ ++ ++ ++ ++ + # + # The position of the line segment that cuts through (or + # doesn't, in case 0 and 15) each square is clear, except in + # cases 6 and 9. In this case, where the segments are placed + # is determined by vertex_connect_high. If + # vertex_connect_high is false, then lines like \\ are drawn + # through square 6, and lines like # are drawn through square + # 9. Otherwise, the situation is reversed. + # Finally, recall that we draw the lines so that (moving from tail to + # head) the lower-valued pixels are on the left of the line. So, for + # example, case 1 entails a line slanting from the middle of the top of + # the square to the middle of the left side of the square. + + r0, c0 = coords[0], coords[1] + r1, c1 = r0 + 1, c0 + 1 + + ul = array[r0, c0] + ur = array[r0, c0 + 1] + ll = array[r0 + 1, c0] + lr = array[r0 + 1, c0 + 1] + + square_case = 0 + if (ul > level): square_case += 1 + if (ur > level): square_case += 2 + if (ll > level): square_case += 4 + if (lr > level): square_case += 8 + + top = coords[0], coords[1] + _get_fraction(ul, ur, level) + bottom = coords[0] + 1, coords[1] + _get_fraction(ll, lr, level) + left = coords[0] + _get_fraction(ul, ll, level), coords[1] + right = coords[0] + _get_fraction(ur, lr, level), coords[1] + 1 + + if (square_case == 0): + # no line + pass + elif (square_case == 1): + # top to left + arc_list.append(top) + arc_list.append(left) + elif (square_case == 2): + # right to top + arc_list.append(right) + arc_list.append(top) + elif (square_case == 3): + # right to left + arc_list.append(right) + arc_list.append(left) + elif (square_case == 4): + # left to bottom + arc_list.append(left) + arc_list.append(bottom) + elif (square_case == 5): + # top to bottom + arc_list.append(top) + arc_list.append(bottom) + elif (square_case == 6): + if vertex_connect_high: + arc_list.append(left) + arc_list.append(top) + + arc_list.append(right) + arc_list.append(bottom) + else: + arc_list.append(right) + arc_list.append(top) + arc_list.append(left) + arc_list.append(bottom) + elif (square_case == 7): + # right to bottom + arc_list.append(right) + arc_list.append(bottom) + elif (square_case == 8): + # bottom to right + arc_list.append(bottom) + arc_list.append(right) + elif (square_case == 9): + if vertex_connect_high: + arc_list.append(top) + arc_list.append(right) + + arc_list.append(bottom) + arc_list.append(left) + else: + arc_list.append(top) + arc_list.append(left) + + arc_list.append(bottom) + arc_list.append(right) + elif (square_case == 10): + # bottom to top + arc_list.append(bottom) + arc_list.append(top) + elif (square_case == 11): + # bottom to left + arc_list.append(bottom) + arc_list.append(left) + elif (square_case == 12): + # lef to right + arc_list.append(left) + arc_list.append(right) + elif (square_case == 13): + # top to right + arc_list.append(top) + arc_list.append(right) + elif (square_case == 14): + # left to top + arc_list.append(left) + arc_list.append(top) + elif (square_case == 15): + # no line + pass + + if coords[1] < array.shape[1] - 2: + coords[1] += 1 + else: + coords[0] += 1 + coords[1] = 0 + + return arc_list diff --git a/skimage/measure/find_contours.py b/skimage/measure/find_contours.py index 9ab67a06..f46d4e77 100755 --- a/skimage/measure/find_contours.py +++ b/skimage/measure/find_contours.py @@ -84,7 +84,7 @@ def find_contours(array, level, fully_connected='low', positive_orientation='low around structures that are a single array element wide. Instead choose a middle value, as above.''' - array = np.asarray(array) + array = np.asarray(array, dtype=np.double) if array.ndim != 2: raise RuntimeError('Only 2D arrays are supported.') level = float(level) diff --git a/skimage/measure/setup.py b/skimage/measure/setup.py index dc4d1768..9fc5ca2d 100644 --- a/skimage/measure/setup.py +++ b/skimage/measure/setup.py @@ -1,11 +1,17 @@ #!/usr/bin/env python +from skimage._build import cython + +import os +base_path = os.path.abspath(os.path.dirname(__file__)) + def configuration(parent_package='', top_path=None): from numpy.distutils.misc_util import Configuration, get_numpy_include_dirs config = Configuration('measure', parent_package, top_path) config.add_data_dir('tests') + cython(['_find_contours.pyx'], working_path=base_path) config.add_extension('_find_contours', sources=['_find_contours.c'], include_dirs=[get_numpy_include_dirs()]) From 2d4abbaa8655bea8ccdfb8151e634ac685dacac8 Mon Sep 17 00:00:00 2001 From: Stefan van der Walt Date: Wed, 30 Nov 2011 12:28:04 -0800 Subject: [PATCH 08/12] BUG: Import imread from renamed _io module. --- skimage/io/collection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skimage/io/collection.py b/skimage/io/collection.py index 7de3c9cc..317298d1 100644 --- a/skimage/io/collection.py +++ b/skimage/io/collection.py @@ -8,7 +8,7 @@ from glob import glob import os.path import numpy as np -from io import imread +from ._io import imread class MultiImage(object): From ff119bb68cf308b8bc7f602e4b10d285024f817b Mon Sep 17 00:00:00 2001 From: Stefan van der Walt Date: Wed, 30 Nov 2011 13:38:26 -0800 Subject: [PATCH 09/12] DOC: Update contributors. --- CONTRIBUTORS.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index bff86ccc..72880521 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -28,7 +28,7 @@ Code to generate skimage logo. - Zachary Pincus - Tracing of low cost paths, FreeImage I/O plugin + Tracing of low cost paths, FreeImage I/O plugin, iso-contours - Almar Klein Binary heap class for graph algorithms From 7691fdc3c62ec99c2721d9bfe6e0fb3a9a953018 Mon Sep 17 00:00:00 2001 From: Stefan van der Walt Date: Wed, 30 Nov 2011 13:38:46 -0800 Subject: [PATCH 10/12] DOC: Add contour finding example. --- doc/examples/plot_contours.py | 44 +++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 doc/examples/plot_contours.py diff --git a/doc/examples/plot_contours.py b/doc/examples/plot_contours.py new file mode 100644 index 00000000..8f707d52 --- /dev/null +++ b/doc/examples/plot_contours.py @@ -0,0 +1,44 @@ +""" +=============== +Contour finding +=============== + +``skimage.measure.find_contours`` uses a marching squares method to +find constant valued contours in an image. Array values are linearly +interpolated to provide better precision of the output contours. +Contours which intersect the image edge are open; all others +are closed. + +The `marching squares algorithm +`__ is a special +case of the marching cubes algorithm (Lorensen, William and Harvey +E. Cline. Marching Cubes: A High Resolution 3D Surface Construction +Algorithm. Computer Graphics (SIGGRAPH 87 Proceedings) 21(4) July +1987, p. 163-170). + +""" + +from skimage import data +from skimage import measure + +import numpy as np +import matplotlib.pyplot as plt + +# Construct some test data +x, y = np.ogrid[-np.pi:np.pi:100j, -np.pi:np.pi:100j] +r = np.sin(np.exp((np.sin(x)**3 + np.cos(y)**2))) + +# Find contours at a constant value of 0.8 +contours = measure.find_contours(r, 0.8) + +# Display the image and plot all contours found +plt.imshow(r, interpolation='nearest') + +for n, contour in enumerate(contours): + plt.plot(contour[:, 1], contour[:, 0], linewidth=2) + +plt.axis('image') +plt.xticks([]) +plt.yticks([]) +plt.show() + From 8579ef0183a45b213ed934fdc0880f4ecca40a5b Mon Sep 17 00:00:00 2001 From: Stefan van der Walt Date: Wed, 30 Nov 2011 15:56:21 -0800 Subject: [PATCH 11/12] STY: Clean up docstrings. --- doc/examples/plot_contours.py | 18 ++++---- skimage/measure/find_contours.py | 75 ++++++++++++++++++-------------- 2 files changed, 51 insertions(+), 42 deletions(-) diff --git a/doc/examples/plot_contours.py b/doc/examples/plot_contours.py index 8f707d52..020d3dfc 100644 --- a/doc/examples/plot_contours.py +++ b/doc/examples/plot_contours.py @@ -3,18 +3,16 @@ Contour finding =============== -``skimage.measure.find_contours`` uses a marching squares method to -find constant valued contours in an image. Array values are linearly -interpolated to provide better precision of the output contours. -Contours which intersect the image edge are open; all others -are closed. +``skimage.measure.find_contours`` uses a marching squares method to find +constant valued contours in an image. Array values are linearly interpolated +to provide better precision of the output contours. Contours which intersect +the image edge are open; all others are closed. The `marching squares algorithm -`__ is a special -case of the marching cubes algorithm (Lorensen, William and Harvey -E. Cline. Marching Cubes: A High Resolution 3D Surface Construction -Algorithm. Computer Graphics (SIGGRAPH 87 Proceedings) 21(4) July -1987, p. 163-170). +`__ is a special case of +the marching cubes algorithm (Lorensen, William and Harvey E. Cline. Marching +Cubes: A High Resolution 3D Surface Construction Algorithm. Computer Graphics +(SIGGRAPH 87 Proceedings) 21(4) July 1987, p. 163-170). """ diff --git a/skimage/measure/find_contours.py b/skimage/measure/find_contours.py index f46d4e77..582c1b52 100755 --- a/skimage/measure/find_contours.py +++ b/skimage/measure/find_contours.py @@ -5,8 +5,9 @@ from collections import deque _param_options = ('high', 'low') -def find_contours(array, level, fully_connected='low', positive_orientation='low'): - '''Find iso-valued contours in a 2D array for a given level value. +def find_contours(array, level, + fully_connected='low', positive_orientation='low'): + """Find iso-valued contours in a 2D array for a given level value. Uses the "marching squares" method to compute a the iso-valued contours of the input 2D array for a particular level value. Array values are linearly @@ -14,35 +15,36 @@ def find_contours(array, level, fully_connected='low', positive_orientation='low Parameters ---------- - array : convertible to a 2D ndarray object - Input data in which to find isocontours. + array : 2D ndarray of double + Input data in which to find contours. level : float Value along which to find contours in the array. - fully_connected : either 'low' or 'high' - Indicates whether array elements below the given level value are to - be considered fully- connected (and hence elements above the value - will only be face connected), or vice-versa. (See below for details.) + fully_connected : str, {'low', 'high'} + Indicates whether array elements below the given level value are to be + considered fully-connected (and hence elements above the value will + only be face connected), or vice-versa. (See notes below for details.) positive_orientation : either 'low' or 'high' - Indicates whether the output contours will produce - positively-oriented polygons around islands of low- or high-valued - elements. If 'low' then contours will wind counter- clockwise around - elements below the iso-value. Alternately, this means that low-valued - elements are always on the left of the contour. (See below for - details.) + Indicates whether the output contours will produce positively-oriented + polygons around islands of low- or high-valued elements. If 'low' then + contours will wind counter- clockwise around elements below the + iso-value. Alternately, this means that low-valued elements are always + on the left of the contour. (See below for details.) Returns ------- - A list of contours, where each contour is an ndarray of shape (n, 2) - consisting of n (x,y) coordinates along the contour. + contours : list of (n,2)-ndarrays + Each contour is an ndarray of shape ``(n, 2)``, + consisting of n ``(x, y)`` coordinates along the contour. + Notes + ----- The marching squares algorithm is a special case of the marching cubes - algorithm (Lorensen, William and Harvey E. Cline. Marching Cubes: A High - Resolution 3D Surface Construction Algorithm. Computer Graphics (SIGGRAPH - 87 Proceedings) 21(4) July 1987, p. 163-170). A simple explanation is - available here: http://www.essi.fr/~lingrand/MarchingCubes/algo.html + algorithm [1]_. A simple explanation is available here:: + + http://www.essi.fr/~lingrand/MarchingCubes/algo.html There is a single ambiguous case in the marching squares algorithm: when - a given 2x2-element square has two high-valued and two low-valued + a given ``2 x 2``-element square has two high-valued and two low-valued elements, each pair diagonally adjacent. (Where high- and low-valued is with respect to the contour value sought.) In this case, either the high-valued elements can be 'connected together' via a thin isthmus that @@ -50,7 +52,7 @@ def find_contours(array, level, fully_connected='low', positive_orientation='low connected together across a diagonal, they are considered 'fully connected' (also known as 'face+vertex-connected' or '8-connected'). Only high-valued or low-valued elements can be fully-connected, the other set - will be considred as 'face-connected' or '4-connected'. By default, + will be considered as 'face-connected' or '4-connected'. By default, low-valued elements are considered fully-connected; this can be altered with the 'fully_connected' parameter. @@ -67,23 +69,32 @@ def find_contours(array, level, fully_connected='low', positive_orientation='low 'positive_orientation' parameter. The order of the contours in the output list is determined by the position - of the smallest x,y (in lexicographical order) coordinate in the contour. - This is a side-effect of how the input array is traversed, but can be - relied upon. + of the smallest ``x,y`` (in lexicographical order) coordinate in the + contour. This is a side-effect of how the input array is traversed, but + can be relied upon. - IMPORTANT NOTE ON COORDINATES AND VALUES: - Array coordinates/values are assumed to refer to the _center_ of the - array element. Take a simple example: [0, 1]. The interpolated position of - 0.5 in this array is midway between the 0-element (at x=0) and the - 1-element (at x=1), and thus would fall at x=0.5. + .. warning:: + + Array coordinates/values are assumed to refer to the *center* of the + array element. Take a simple example input: ``[0, 1]``. The interpolated + position of 0.5 in this array is midway between the 0-element (at + ``x=0``) and the 1-element (at ``x=1``), and thus would fall at + ``x=0.5``. This means that to find reasonable contours, it is best to find contours midway between the expected "light" and "dark" values. In particular, - given a binarized array, DO NOT choose to find contours at the low or high + given a binarized array, *do not* choose to find contours at the low or high value of the array. This will often yield degenerate contours, especially around structures that are a single array element wide. Instead choose - a middle value, as above.''' + a middle value, as above. + References + ---------- + .. [1] Lorensen, William and Harvey E. Cline. Marching Cubes: A High + Resolution 3D Surface Construction Algorithm. Computer Graphics + (SIGGRAPH 87 Proceedings) 21(4) July 1987, p. 163-170). + + """ array = np.asarray(array, dtype=np.double) if array.ndim != 2: raise RuntimeError('Only 2D arrays are supported.') From fbb07734fca7d0fb86c094743ac794def49baa62 Mon Sep 17 00:00:00 2001 From: Zach Pincus Date: Thu, 1 Dec 2011 12:34:08 -0500 Subject: [PATCH 12/12] Tighten up _find_contours.pyx Remove some redundant operations in _find_contours.pyx -- will speed things especially in common case where most pixels DON'T have a contour nearby. --- skimage/measure/_find_contours.pyx | 171 +++++++++++++++-------------- 1 file changed, 86 insertions(+), 85 deletions(-) diff --git a/skimage/measure/_find_contours.pyx b/skimage/measure/_find_contours.pyx index f3a26f74..17702f95 100644 --- a/skimage/measure/_find_contours.pyx +++ b/skimage/measure/_find_contours.pyx @@ -6,7 +6,8 @@ cimport numpy as np np.import_array() -cdef double _get_fraction(double from_value, double to_value, double level): +cdef inline double _get_fraction(double from_value, double to_value, + double level): if (to_value == from_value): return 0 return ((level - from_value) / (to_value - from_value)) @@ -70,7 +71,7 @@ def iterate_and_store(np.ndarray[double, ndim=2, mode='c'] array, # cases 6 and 9. In this case, where the segments are placed # is determined by vertex_connect_high. If # vertex_connect_high is false, then lines like \\ are drawn - # through square 6, and lines like # are drawn through square + # through square 6, and lines like // are drawn through square # 9. Otherwise, the situation is reversed. # Finally, recall that we draw the lines so that (moving from tail to # head) the lower-valued pixels are on the left of the line. So, for @@ -81,9 +82,17 @@ def iterate_and_store(np.ndarray[double, ndim=2, mode='c'] array, r1, c1 = r0 + 1, c0 + 1 ul = array[r0, c0] - ur = array[r0, c0 + 1] - ll = array[r0 + 1, c0] - lr = array[r0 + 1, c0 + 1] + ur = array[r0, c1] + ll = array[r1, c0] + lr = array[r1, c1] + + # now in advance the coords indices + if coords[1] < array.shape[1] - 2: + coords[1] += 1 + else: + coords[0] += 1 + coords[1] = 0 + square_case = 0 if (ul > level): square_case += 1 @@ -91,95 +100,87 @@ def iterate_and_store(np.ndarray[double, ndim=2, mode='c'] array, if (ll > level): square_case += 4 if (lr > level): square_case += 8 - top = coords[0], coords[1] + _get_fraction(ul, ur, level) - bottom = coords[0] + 1, coords[1] + _get_fraction(ll, lr, level) - left = coords[0] + _get_fraction(ul, ll, level), coords[1] - right = coords[0] + _get_fraction(ur, lr, level), coords[1] + 1 + if (square_case != 0 and square_case != 15): + # only do anything if there's a line passing through the + # square. Cases 0 and 15 are entirely below/above the contour. + + top = r0, c0 + _get_fraction(ul, ur, level) + bottom = r1, c0 + _get_fraction(ll, lr, level) + left = r0 + _get_fraction(ul, ll, level), c0 + right = r0 + _get_fraction(ur, lr, level), c1 - if (square_case == 0): - # no line - pass - elif (square_case == 1): - # top to left - arc_list.append(top) - arc_list.append(left) - elif (square_case == 2): - # right to top - arc_list.append(right) - arc_list.append(top) - elif (square_case == 3): - # right to left - arc_list.append(right) - arc_list.append(left) - elif (square_case == 4): - # left to bottom - arc_list.append(left) - arc_list.append(bottom) - elif (square_case == 5): - # top to bottom - arc_list.append(top) - arc_list.append(bottom) - elif (square_case == 6): - if vertex_connect_high: - arc_list.append(left) - arc_list.append(top) - - arc_list.append(right) - arc_list.append(bottom) - else: - arc_list.append(right) + if (square_case == 1): + # top to left arc_list.append(top) arc_list.append(left) - arc_list.append(bottom) - elif (square_case == 7): - # right to bottom - arc_list.append(right) - arc_list.append(bottom) - elif (square_case == 8): - # bottom to right - arc_list.append(bottom) - arc_list.append(right) - elif (square_case == 9): - if vertex_connect_high: - arc_list.append(top) + elif (square_case == 2): + # right to top arc_list.append(right) - - arc_list.append(bottom) - arc_list.append(left) - else: arc_list.append(top) + elif (square_case == 3): + # right to left + arc_list.append(right) arc_list.append(left) + elif (square_case == 4): + # left to bottom + arc_list.append(left) + arc_list.append(bottom) + elif (square_case == 5): + # top to bottom + arc_list.append(top) + arc_list.append(bottom) + elif (square_case == 6): + if vertex_connect_high: + arc_list.append(left) + arc_list.append(top) + arc_list.append(right) + arc_list.append(bottom) + else: + arc_list.append(right) + arc_list.append(top) + arc_list.append(left) + arc_list.append(bottom) + elif (square_case == 7): + # right to bottom + arc_list.append(right) + arc_list.append(bottom) + elif (square_case == 8): + # bottom to right arc_list.append(bottom) arc_list.append(right) - elif (square_case == 10): - # bottom to top - arc_list.append(bottom) - arc_list.append(top) - elif (square_case == 11): - # bottom to left - arc_list.append(bottom) - arc_list.append(left) - elif (square_case == 12): - # lef to right - arc_list.append(left) - arc_list.append(right) - elif (square_case == 13): - # top to right - arc_list.append(top) - arc_list.append(right) - elif (square_case == 14): - # left to top - arc_list.append(left) - arc_list.append(top) - elif (square_case == 15): - # no line - pass + elif (square_case == 9): + if vertex_connect_high: + arc_list.append(top) + arc_list.append(right) - if coords[1] < array.shape[1] - 2: - coords[1] += 1 - else: - coords[0] += 1 - coords[1] = 0 + arc_list.append(bottom) + arc_list.append(left) + else: + arc_list.append(top) + arc_list.append(left) + + arc_list.append(bottom) + arc_list.append(right) + elif (square_case == 10): + # bottom to top + arc_list.append(bottom) + arc_list.append(top) + elif (square_case == 11): + # bottom to left + arc_list.append(bottom) + arc_list.append(left) + elif (square_case == 12): + # lef to right + arc_list.append(left) + arc_list.append(right) + elif (square_case == 13): + # top to right + arc_list.append(top) + arc_list.append(right) + elif (square_case == 14): + # left to top + arc_list.append(left) + arc_list.append(top) return arc_list