diff --git a/DEPENDS.txt b/DEPENDS.txt index 2c2aeb7d..fc6d6679 100644 --- a/DEPENDS.txt +++ b/DEPENDS.txt @@ -1,7 +1,7 @@ Build Requirements ------------------ * `Python >= 2.6 `__ -* `Numpy >= 1.6.1 `__ +* `Numpy >= 1.7.2 `__ * `Cython >= 0.21 `__ * `Six >=1.4 `__ * `SciPy >=0.9 `__ @@ -9,7 +9,7 @@ Build Requirements Runtime requirements -------------------- * `Python >= 2.6 `__ -* `Numpy >= 1.6.1 `__ +* `Numpy >= 1.7.2 `__ * `SciPy >= 0.9 `__ * `Matplotlib >= 1.1.0 `__ * `NetworkX >= 1.8 `__ diff --git a/requirements.txt b/requirements.txt index 19c541f5..98c1b49a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ matplotlib>=1.1.0 -numpy>=1.6.1 +numpy>=1.7.2 scipy>=0.9.0 six>=1.4 networkx>=1.8 diff --git a/skimage/draw/_draw.pyx b/skimage/draw/_draw.pyx index 3a0d3612..0d384a38 100644 --- a/skimage/draw/_draw.pyx +++ b/skimage/draw/_draw.pyx @@ -21,20 +21,21 @@ def _coords_inside_image(rr, cc, shape, val=None): shape : tuple Image shape which is used to determine the maximum extent of output pixel coordinates. - val : ndarray of float, optional - Values of pixels at coordinates [rr, cc]. + val : (N, D) ndarray of float, optional + Values of pixels at coordinates ``[rr, cc]``. Returns ------- - rr, cc : (N,) array of int + rr, cc : (M,) array of int Row and column indices of valid pixels (i.e. those inside `shape`). - val : (N,) array of float, optional + val : (M, D) array of float, optional Values at `rr, cc`. Returned only if `val` is given as input. """ mask = (rr >= 0) & (rr < shape[0]) & (cc >= 0) & (cc < shape[1]) - if val is not None: + if val is None: + return rr[mask], cc[mask] + else: return rr[mask], cc[mask], val[mask] - return rr[mask], cc[mask] def line(Py_ssize_t y0, Py_ssize_t x0, Py_ssize_t y1, Py_ssize_t x1): diff --git a/skimage/draw/draw.py b/skimage/draw/draw.py index c91aca2c..1624b115 100644 --- a/skimage/draw/draw.py +++ b/skimage/draw/draw.py @@ -125,7 +125,7 @@ def circle(r, c, radius, shape=None): return ellipse(r, c, radius, radius, shape) -def set_color(img, coords, color): +def set_color(img, coords, color, alpha=1): """Set pixel color in the image at the given coordinates. Coordinates that exceed the shape of the image will be ignored. @@ -134,10 +134,13 @@ def set_color(img, coords, color): ---------- img : (M, N, D) ndarray Image - coords : ((P,) ndarray, (P,) ndarray) - Coordinates of pixels to be colored. + coords : tuple of ((P,) ndarray, (P,) ndarray) + Row and column coordinates of pixels to be colored. color : (D,) ndarray Color to be assigned to coordinates in the image. + alpha : scalar or (N,) ndarray + Alpha values used to blend color with image. 0 is transparent, + 1 is opaque. Returns ------- @@ -163,7 +166,28 @@ def set_color(img, coords, color): [0, 0, 0, 0, 0, 0, 0, 0, 0, 1]], dtype=uint8) """ - rr, cc = coords - rr, cc = _coords_inside_image(rr, cc, img.shape) - img[rr, cc] = color + + if img.ndim == 2: + img = img[..., np.newaxis] + + color = np.array(color, ndmin=1, copy=False) + + if img.shape[-1] != color.shape[-1]: + raise ValueError('Color shape ({}) must match last ' + 'image dimension ({}).'.format(color.shape[0], + img.shape[-1])) + + if np.isscalar(alpha): + # Can be replaced by ``full_like`` when numpy 1.8 becomes + # minimum dependency + alpha = np.ones_like(rr) * alpha + + rr, cc, alpha = _coords_inside_image(rr, cc, img.shape, val=alpha) + + alpha = alpha[..., np.newaxis] + + color = color * alpha + vals = img[rr, cc] * (1 - alpha) + + img[rr, cc] = vals + color diff --git a/skimage/draw/tests/test_draw.py b/skimage/draw/tests/test_draw.py index a07ca631..214f09b0 100644 --- a/skimage/draw/tests/test_draw.py +++ b/skimage/draw/tests/test_draw.py @@ -1,4 +1,4 @@ -from numpy.testing import assert_array_equal, assert_equal +from numpy.testing import assert_array_equal, assert_equal, assert_raises import numpy as np from skimage._shared.testing import test_parallel @@ -20,6 +20,21 @@ def test_set_color(): assert_array_equal(img, img_) +def test_set_color_with_alpha(): + img = np.zeros((10, 10)) + + rr, cc, alpha = line_aa(0, 0, 0, 30) + set_color(img, (rr, cc), 1, alpha=alpha) + + # Wrong dimensionality color + assert_raises(ValueError, set_color, img, (rr, cc), (255, 0, 0), alpha=alpha) + + img = np.zeros((10, 10, 3)) + + rr, cc, alpha = line_aa(0, 0, 0, 30) + set_color(img, (rr, cc), (1, 0, 0), alpha=alpha) + + @test_parallel() def test_line_horizontal(): img = np.zeros((10, 10)) @@ -72,7 +87,7 @@ def test_line_aa_horizontal(): img = np.zeros((10, 10)) rr, cc, val = line_aa(0, 0, 0, 9) - img[rr, cc] = val + set_color(img, (rr, cc), 1, alpha=val) img_ = np.zeros((10, 10)) img_[0, :] = 1 @@ -329,12 +344,14 @@ def test_circle_perimeter_aa_shape(): img = np.zeros((15, 20), 'uint8') rr, cc, val = circle_perimeter_aa(7, 10, 9, shape=(15, 20)) img[rr, cc] = val * 255 + shift = 5 img_ = np.zeros((15 + 2 * shift, 20), 'uint8') rr, cc, val = circle_perimeter_aa(7 + shift, 10, 9, shape=None) img_[rr, cc] = val * 255 assert_array_equal(img, img_[shift:-shift, :]) + def test_ellipse_trivial(): img = np.zeros((2, 2), 'uint8') rr, cc = ellipse(0.5, 0.5, 0.5, 0.5) diff --git a/skimage/feature/_daisy.py b/skimage/feature/_daisy.py index 6dca0c59..df5cf4f4 100644 --- a/skimage/feature/_daisy.py +++ b/skimage/feature/_daisy.py @@ -181,20 +181,20 @@ def daisy(img, step=4, radius=15, rings=3, histograms=8, orientations=8, for i in range(descs.shape[0]): for j in range(descs.shape[1]): # Draw center histogram sigma - color = (1, 0, 0) + color = [1, 0, 0] desc_y = i * step + radius desc_x = j * step + radius - coords = draw.circle_perimeter(desc_y, desc_x, int(sigmas[0])) - draw.set_color(descs_img, coords, color) + rows, cols, val = draw.circle_perimeter_aa(desc_y, desc_x, int(sigmas[0])) + draw.set_color(descs_img, (rows, cols), color, alpha=val) max_bin = np.max(descs[i, j, :]) for o_num, o in enumerate(orientation_angles): # Draw center histogram bins bin_size = descs[i, j, o_num] / max_bin dy = sigmas[0] * bin_size * sin(o) dx = sigmas[0] * bin_size * cos(o) - coords = draw.line(desc_y, desc_x, int(desc_y + dy), - int(desc_x + dx)) - draw.set_color(descs_img, coords, color) + rows, cols, val = draw.line_aa(desc_y, desc_x, int(desc_y + dy), + int(desc_x + dx)) + draw.set_color(descs_img, (rows, cols), color, alpha=val) for r_num, r in enumerate(ring_radii): color_offset = float(1 + r_num) / rings color = (1 - color_offset, 1, color_offset) @@ -202,9 +202,9 @@ def daisy(img, step=4, radius=15, rings=3, histograms=8, orientations=8, # Draw ring histogram sigmas hist_y = desc_y + int(round(r * sin(t))) hist_x = desc_x + int(round(r * cos(t))) - coords = draw.circle_perimeter(hist_y, hist_x, - int(sigmas[r_num + 1])) - draw.set_color(descs_img, coords, color) + rows, cols, val = draw.circle_perimeter_aa(hist_y, hist_x, + int(sigmas[r_num + 1])) + draw.set_color(descs_img, (rows, cols), color, alpha=val) for o_num, o in enumerate(orientation_angles): # Draw histogram bins bin_size = descs[i, j, orientations + r_num * @@ -213,10 +213,10 @@ def daisy(img, step=4, radius=15, rings=3, histograms=8, orientations=8, bin_size /= max_bin dy = sigmas[r_num + 1] * bin_size * sin(o) dx = sigmas[r_num + 1] * bin_size * cos(o) - coords = draw.line(hist_y, hist_x, - int(hist_y + dy), - int(hist_x + dx)) - draw.set_color(descs_img, coords, color) + rows, cols, val = draw.line_aa(hist_y, hist_x, + int(hist_y + dy), + int(hist_x + dx)) + draw.set_color(descs_img, (rows, cols), color, alpha=val) return descs, descs_img else: return descs diff --git a/skimage/util/dtype.py b/skimage/util/dtype.py index 077b14be..a0ff637e 100644 --- a/skimage/util/dtype.py +++ b/skimage/util/dtype.py @@ -25,9 +25,8 @@ _supported_types = (np.bool_, np.bool8, np.int8, np.int16, np.int32, np.int64, np.float32, np.float64) -if np.__version__ >= "1.6.0": - dtype_range[np.float16] = (-1, 1) - _supported_types += (np.float16, ) +dtype_range[np.float16] = (-1, 1) +_supported_types += (np.float16, ) def dtype_limits(image, clip_negative=True):