From 998d64e64e74ebc73f4994dc1027a78125a4ddb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Mon, 14 Oct 2013 17:55:32 +0200 Subject: [PATCH 1/3] Remove duplicate set_color function --- skimage/draw/_draw.pyx | 28 ---------------------------- skimage/draw/draw.py | 29 ++++------------------------- 2 files changed, 4 insertions(+), 53 deletions(-) diff --git a/skimage/draw/_draw.pyx b/skimage/draw/_draw.pyx index c22600a4..db63d343 100644 --- a/skimage/draw/_draw.pyx +++ b/skimage/draw/_draw.pyx @@ -834,31 +834,3 @@ def bezier_curve(Py_ssize_t y0, Py_ssize_t x0, px.extend(rr) py.extend(cc) return np.array(px, dtype=np.intp), np.array(py, dtype=np.intp) - - -def set_color(img, coords, color): - """Set pixel color in the image at the given coordinates. - - Coordinates that exceed the shape of the image will be ignored. - - Parameters - ---------- - img : (M, N, D) ndarray - Image - coords : ((P,) ndarray, (P,) ndarray) - Coordinates of pixels to be colored. - color : (D,) ndarray - Color to be assigned to coordinates in the image. - - Returns - ------- - img : (M, N, D) ndarray - The updated image. - - """ - - rr, cc = coords - rr_inside = np.logical_and(rr >= 0, rr < img.shape[0]) - cc_inside = np.logical_and(cc >= 0, cc < img.shape[1]) - inside = np.logical_and(rr_inside, cc_inside) - img[rr[inside], cc[inside]] = color diff --git a/skimage/draw/draw.py b/skimage/draw/draw.py index cbf3ced2..c823b73e 100644 --- a/skimage/draw/draw.py +++ b/skimage/draw/draw.py @@ -2,11 +2,6 @@ import numpy as np -def _coords_inside_image(rr, cc, shape): - mask = (rr >= 0) & (rr < shape[0]) & (cc >= 0) & (cc < shape[1]) - return rr[mask], cc[mask] - - def ellipse(cy, cx, yradius, xradius, shape=None): """Generate coordinates of pixels within ellipse. @@ -131,26 +126,10 @@ def set_color(img, coords, color): img : (M, N, D) ndarray The updated image. - Examples - -------- - >>> from skimage.draw import line, set_color - >>> img = np.zeros((10, 10), dtype=np.uint8) - >>> rr, cc = line(1, 1, 20, 20) - >>> set_color(img, (rr, cc), 1) - >>> img - array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 1, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 1, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 1, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 1, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 1, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 1, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 1, 0], - [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 + rr_inside = np.logical_and(rr >= 0, rr < img.shape[0]) + cc_inside = np.logical_and(cc >= 0, cc < img.shape[1]) + inside = np.logical_and(rr_inside, cc_inside) + img[rr[inside], cc[inside]] = color From ffbe37ad9d3b33ff4750cefbf252b471c9f5d2b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Mon, 14 Oct 2013 18:01:03 +0200 Subject: [PATCH 2/3] Add test case for set_color function --- skimage/draw/tests/test_draw.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/skimage/draw/tests/test_draw.py b/skimage/draw/tests/test_draw.py index d3cab811..2d739f0f 100644 --- a/skimage/draw/tests/test_draw.py +++ b/skimage/draw/tests/test_draw.py @@ -1,11 +1,22 @@ from numpy.testing import assert_array_equal, assert_equal import numpy as np -from skimage.draw import (line, line_aa, polygon, +from skimage.draw import (set_color, line, line_aa, polygon, circle, circle_perimeter, circle_perimeter_aa, ellipse, ellipse_perimeter, - _bezier_segment, bezier_curve, - ) + _bezier_segment, bezier_curve) + + +def test_set_color(): + img = np.zeros((10, 10)) + + rr, cc = line(0, 0, 0, 30) + set_color(img, (rr, cc), 1) + + img_ = np.zeros((10, 10)) + img_[0, :] = 1 + + assert_array_equal(img, img_) def test_line_horizontal(): From a9e9012d1a754d44a5199c5a9b974288db12a9d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Mon, 14 Oct 2013 18:24:09 +0200 Subject: [PATCH 3/3] Re-add missing parts --- skimage/draw/draw.py | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/skimage/draw/draw.py b/skimage/draw/draw.py index c823b73e..cbf3ced2 100644 --- a/skimage/draw/draw.py +++ b/skimage/draw/draw.py @@ -2,6 +2,11 @@ import numpy as np +def _coords_inside_image(rr, cc, shape): + mask = (rr >= 0) & (rr < shape[0]) & (cc >= 0) & (cc < shape[1]) + return rr[mask], cc[mask] + + def ellipse(cy, cx, yradius, xradius, shape=None): """Generate coordinates of pixels within ellipse. @@ -126,10 +131,26 @@ def set_color(img, coords, color): img : (M, N, D) ndarray The updated image. + Examples + -------- + >>> from skimage.draw import line, set_color + >>> img = np.zeros((10, 10), dtype=np.uint8) + >>> rr, cc = line(1, 1, 20, 20) + >>> set_color(img, (rr, cc), 1) + >>> img + array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 1, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 1, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 1, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 1, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 1, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 1, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 1, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 1]], dtype=uint8) + """ rr, cc = coords - rr_inside = np.logical_and(rr >= 0, rr < img.shape[0]) - cc_inside = np.logical_and(cc >= 0, cc < img.shape[1]) - inside = np.logical_and(rr_inside, cc_inside) - img[rr[inside], cc[inside]] = color + rr, cc = _coords_inside_image(rr, cc, img.shape) + img[rr, cc] = color