Merge pull request #1827 from stefanv/daisy_aa_draw

Daisy aa draw
This commit is contained in:
Juan Nunez-Iglesias
2016-02-01 15:41:24 +11:00
7 changed files with 74 additions and 33 deletions
+2 -2
View File
@@ -1,7 +1,7 @@
Build Requirements
------------------
* `Python >= 2.6 <http://python.org>`__
* `Numpy >= 1.6.1 <http://numpy.scipy.org/>`__
* `Numpy >= 1.7.2 <http://numpy.scipy.org/>`__
* `Cython >= 0.21 <http://www.cython.org/>`__
* `Six >=1.4 <https://pypi.python.org/pypi/six>`__
* `SciPy >=0.9 <http://scipy.org>`__
@@ -9,7 +9,7 @@ Build Requirements
Runtime requirements
--------------------
* `Python >= 2.6 <http://python.org>`__
* `Numpy >= 1.6.1 <http://numpy.scipy.org/>`__
* `Numpy >= 1.7.2 <http://numpy.scipy.org/>`__
* `SciPy >= 0.9 <http://scipy.org>`__
* `Matplotlib >= 1.1.0 <http://matplotlib.sf.net>`__
* `NetworkX >= 1.8 <https://networkx.github.io>`__
+1 -1
View File
@@ -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
+7 -6
View File
@@ -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):
+30 -6
View File
@@ -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
+19 -2
View File
@@ -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)
+13 -13
View File
@@ -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
+2 -3
View File
@@ -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):