scikit-image
218 строк · 9.4 Кб
1import numpy as np2
3from ._find_contours_cy import _get_contour_segments4
5from collections import deque6
7_param_options = ('high', 'low')8
9
10def find_contours(11image, level=None, fully_connected='low', positive_orientation='low', *, mask=None12):13"""Find iso-valued contours in a 2D array for a given level value.14
15Uses the "marching squares" method to compute the iso-valued contours of
16the input 2D array for a particular level value. Array values are linearly
17interpolated to provide better precision for the output contours.
18
19Parameters
20----------
21image : (M, N) ndarray of double
22Input image in which to find contours.
23level : float, optional
24Value along which to find contours in the array. By default, the level
25is set to (max(image) + min(image)) / 2
26
27.. versionchanged:: 0.18
28This parameter is now optional.
29fully_connected : str, {'low', 'high'}
30Indicates whether array elements below the given level value are to be
31considered fully-connected (and hence elements above the value will
32only be face connected), or vice-versa. (See notes below for details.)
33positive_orientation : str, {'low', 'high'}
34Indicates whether the output contours will produce positively-oriented
35polygons around islands of low- or high-valued elements. If 'low' then
36contours will wind counter-clockwise around elements below the
37iso-value. Alternately, this means that low-valued elements are always
38on the left of the contour. (See below for details.)
39mask : (M, N) ndarray of bool or None
40A boolean mask, True where we want to draw contours.
41Note that NaN values are always excluded from the considered region
42(``mask`` is set to ``False`` wherever ``array`` is ``NaN``).
43
44Returns
45-------
46contours : list of (K, 2) ndarrays
47Each contour is a ndarray of ``(row, column)`` coordinates along the contour.
48
49See Also
50--------
51skimage.measure.marching_cubes
52
53Notes
54-----
55The marching squares algorithm is a special case of the marching cubes
56algorithm [1]_. A simple explanation is available here:
57
58https://users.polytech.unice.fr/~lingrand/MarchingCubes/algo.html
59
60There is a single ambiguous case in the marching squares algorithm: when
61a given ``2 x 2``-element square has two high-valued and two low-valued
62elements, each pair diagonally adjacent. (Where high- and low-valued is
63with respect to the contour value sought.) In this case, either the
64high-valued elements can be 'connected together' via a thin isthmus that
65separates the low-valued elements, or vice-versa. When elements are
66connected together across a diagonal, they are considered 'fully
67connected' (also known as 'face+vertex-connected' or '8-connected'). Only
68high-valued or low-valued elements can be fully-connected, the other set
69will be considered as 'face-connected' or '4-connected'. By default,
70low-valued elements are considered fully-connected; this can be altered
71with the 'fully_connected' parameter.
72
73Output contours are not guaranteed to be closed: contours which intersect
74the array edge or a masked-off region (either where mask is False or where
75array is NaN) will be left open. All other contours will be closed. (The
76closed-ness of a contours can be tested by checking whether the beginning
77point is the same as the end point.)
78
79Contours are oriented. By default, array values lower than the contour
80value are to the left of the contour and values greater than the contour
81value are to the right. This means that contours will wind
82counter-clockwise (i.e. in 'positive orientation') around islands of
83low-valued pixels. This behavior can be altered with the
84'positive_orientation' parameter.
85
86The order of the contours in the output list is determined by the position
87of the smallest ``x,y`` (in lexicographical order) coordinate in the
88contour. This is a side effect of how the input array is traversed, but
89can be relied upon.
90
91.. warning::
92
93Array coordinates/values are assumed to refer to the *center* of the
94array element. Take a simple example input: ``[0, 1]``. The interpolated
95position of 0.5 in this array is midway between the 0-element (at
96``x=0``) and the 1-element (at ``x=1``), and thus would fall at
97``x=0.5``.
98
99This means that to find reasonable contours, it is best to find contours
100midway between the expected "light" and "dark" values. In particular,
101given a binarized array, *do not* choose to find contours at the low or
102high value of the array. This will often yield degenerate contours,
103especially around structures that are a single array element wide. Instead,
104choose a middle value, as above.
105
106References
107----------
108.. [1] Lorensen, William and Harvey E. Cline. Marching Cubes: A High
109Resolution 3D Surface Construction Algorithm. Computer Graphics
110(SIGGRAPH 87 Proceedings) 21(4) July 1987, p. 163-170).
111:DOI:`10.1145/37401.37422`
112
113Examples
114--------
115>>> a = np.zeros((3, 3))
116>>> a[0, 0] = 1
117>>> a
118array([[1., 0., 0.],
119[0., 0., 0.],
120[0., 0., 0.]])
121>>> find_contours(a, 0.5)
122[array([[0. , 0.5],
123[0.5, 0. ]])]
124"""
125if fully_connected not in _param_options:126raise ValueError(127'Parameters "fully_connected" must be either ' '"high" or "low".'128)129if positive_orientation not in _param_options:130raise ValueError(131'Parameters "positive_orientation" must be either ' '"high" or "low".'132)133if image.shape[0] < 2 or image.shape[1] < 2:134raise ValueError("Input array must be at least 2x2.")135if image.ndim != 2:136raise ValueError('Only 2D arrays are supported.')137if mask is not None:138if mask.shape != image.shape:139raise ValueError('Parameters "array" and "mask"' ' must have same shape.')140if not np.can_cast(mask.dtype, bool, casting='safe'):141raise TypeError('Parameter "mask" must be a binary array.')142mask = mask.astype(np.uint8, copy=False)143if level is None:144level = (np.nanmin(image) + np.nanmax(image)) / 2.0145
146segments = _get_contour_segments(147image.astype(np.float64), float(level), fully_connected == 'high', mask=mask148)149contours = _assemble_contours(segments)150if positive_orientation == 'high':151contours = [c[::-1] for c in contours]152return contours153
154
155def _assemble_contours(segments):156current_index = 0157contours = {}158starts = {}159ends = {}160for from_point, to_point in segments:161# Ignore degenerate segments.162# This happens when (and only when) one vertex of the square is163# exactly the contour level, and the rest are above or below.164# This degenerate vertex will be picked up later by neighboring165# squares.166if from_point == to_point:167continue168
169tail, tail_num = starts.pop(to_point, (None, None))170head, head_num = ends.pop(from_point, (None, None))171
172if tail is not None and head is not None:173# We need to connect these two contours.174if tail is head:175# We need to closed a contour: add the end point176head.append(to_point)177else: # tail is not head178# We need to join two distinct contours.179# We want to keep the first contour segment created, so that180# the final contours are ordered left->right, top->bottom.181if tail_num > head_num:182# tail was created second. Append tail to head.183head.extend(tail)184# Remove tail from the detected contours185contours.pop(tail_num, None)186# Update starts and ends187starts[head[0]] = (head, head_num)188ends[head[-1]] = (head, head_num)189else: # tail_num <= head_num190# head was created second. Prepend head to tail.191tail.extendleft(reversed(head))192# Remove head from the detected contours193starts.pop(head[0], None) # head[0] can be == to_point!194contours.pop(head_num, None)195# Update starts and ends196starts[tail[0]] = (tail, tail_num)197ends[tail[-1]] = (tail, tail_num)198elif tail is None and head is None:199# We need to add a new contour200new_contour = deque((from_point, to_point))201contours[current_index] = new_contour202starts[from_point] = (new_contour, current_index)203ends[to_point] = (new_contour, current_index)204current_index += 1205elif head is None: # tail is not None206# tail first element is to_point: the new segment should be207# prepended.208tail.appendleft(from_point)209# Update starts210starts[from_point] = (tail, tail_num)211else: # tail is None and head is not None:212# head last element is from_point: the new segment should be213# appended214head.append(to_point)215# Update ends216ends[to_point] = (head, head_num)217
218return [np.array(contour) for _, contour in sorted(contours.items())]219