From 2a46cc57de176a9f4e3820adc209c14257149000 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Sun, 13 Jan 2013 11:44:09 +0100 Subject: [PATCH 01/10] Use char type for circle_perimeter method for speedup --- skimage/draw/_draw.pyx | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/skimage/draw/_draw.pyx b/skimage/draw/_draw.pyx index 66b41d2c..8c920933 100644 --- a/skimage/draw/_draw.pyx +++ b/skimage/draw/_draw.pyx @@ -231,10 +231,13 @@ def circle_perimeter(int cy, int cx, int radius, method='bresenham'): cdef int x = 0 cdef int y = radius cdef int d = 0 + cdef char cmethod if method == 'bresenham': d = 3 - 2 * radius + cmethod = 'b' elif method == 'andres': d = radius - 1 + cmethod = 'a' else: raise ValueError('Wrong method') @@ -242,14 +245,14 @@ def circle_perimeter(int cy, int cx, int radius, method='bresenham'): rr.extend([y, -y, y, -y, x, -x, x, -x]) cc.extend([x, x, -x, -x, y, y, -y, -y]) - if method == 'bresenham': + if cmethod == 'b': if d < 0: d += 4 * x + 6 else: d += 4 * (x - y) + 10 y -= 1 x += 1 - elif method == 'andres': + elif cmethod == 'a': if d >= 2 * (x - 1): d = d - 2 * x x = x + 1 From cc014a14db2055070b682dc8f13c748b54de7a0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Sun, 13 Jan 2013 11:46:42 +0100 Subject: [PATCH 02/10] Use same cython directives for whole source file --- skimage/draw/_draw.pyx | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/skimage/draw/_draw.pyx b/skimage/draw/_draw.pyx index 8c920933..bc07808d 100644 --- a/skimage/draw/_draw.pyx +++ b/skimage/draw/_draw.pyx @@ -1,3 +1,7 @@ +#cython: cdivision=True +#cython: boundscheck=False +#cython: nonecheck=False +#cython: wraparound=False import numpy as np import math from libc.math cimport sqrt @@ -6,8 +10,6 @@ cimport cython from skimage._shared.geometry cimport point_in_polygon -@cython.boundscheck(False) -@cython.wraparound(False) def line(int y, int x, int y2, int x2): """Generate line pixel coordinates. @@ -66,9 +68,6 @@ def line(int y, int x, int y2, int x2): return rr, cc -@cython.boundscheck(False) -@cython.wraparound(False) -@cython.nonecheck(False) def polygon(y, x, shape=None): """Generate coordinates of pixels within polygon. @@ -123,10 +122,6 @@ def polygon(y, x, shape=None): return np.array(rr), np.array(cc) -@cython.boundscheck(False) -@cython.wraparound(False) -@cython.nonecheck(False) -@cython.cdivision(True) def ellipse(double cy, double cx, double b, double a, shape=None): """Generate coordinates of pixels within ellipse. @@ -267,8 +262,6 @@ def circle_perimeter(int cy, int cx, int radius, method='bresenham'): return np.array(rr) + cy, np.array(cc) + cx -@cython.boundscheck(False) -@cython.wraparound(False) 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. From 55ac092e2fe3034dc77fbc9d77df18662a3ba0f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Sun, 13 Jan 2013 11:48:37 +0100 Subject: [PATCH 03/10] Fix PEP8 issues --- skimage/draw/_draw.pyx | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/skimage/draw/_draw.pyx b/skimage/draw/_draw.pyx index bc07808d..b3b24655 100644 --- a/skimage/draw/_draw.pyx +++ b/skimage/draw/_draw.pyx @@ -35,15 +35,19 @@ def line(int y, int x, int y2, int x2): cdef int dy = abs(y2 - y) cdef int sx, sy, d, i - if (x2 - x) > 0: sx = 1 - else: sx = -1 - if (y2 - y) > 0: sy = 1 - else: sy = -1 + if (x2 - x) > 0: + sx = 1 + else: + sx = -1 + if (y2 - y) > 0: + sy = 1 + else: + sy = -1 if dy > dx: steep = 1 - x,y = y,x - dx,dy = dy,dx - sx,sy = sy,sx + x, y = y, x + dx, dy = dy, dx + sx, sy = sy, sx d = (2 * dy) - dx rr = np.zeros(int(dx) + 1, dtype=np.int32) @@ -97,8 +101,8 @@ def polygon(y, x, shape=None): # make sure output coordinates do not exceed image size if shape is not None: - maxr = min(shape[0]-1, maxr) - maxc = min(shape[1]-1, maxc) + maxr = min(shape[0] - 1, maxr) + maxc = min(shape[1] - 1, maxc) cdef int r, c @@ -139,15 +143,15 @@ def ellipse(double cy, double cx, double b, double a, shape=None): May be used to directly index into an array, e.g. ``img[rr, cc] = 1``. """ - cdef int minr = max(0, cy-b) - cdef int maxr = math.ceil(cy+b) - cdef int minc = max(0, cx-a) - cdef int maxc = math.ceil(cx+a) + cdef int minr = max(0, cy - b) + cdef int maxr = math.ceil(cy + b) + cdef int minc = max(0, cx - a) + cdef int maxc = math.ceil(cx + a) # make sure output coordinates do not exceed image size if shape is not None: - maxr = min(shape[0]-1, maxr) - maxc = min(shape[1]-1, maxc) + maxr = min(shape[0] - 1, maxr) + maxc = min(shape[1] - 1, maxc) cdef int r, c @@ -155,9 +159,9 @@ def ellipse(double cy, double cx, double b, double a, shape=None): cdef list rr = list() cdef list cc = list() - for r in range(minr, maxr+1): - for c in range(minc, maxc+1): - if sqrt(((r - cy)/b)**2 + ((c - cx)/a)**2) < 1: + for r in range(minr, maxr + 1): + for c in range(minc, maxc + 1): + if sqrt(((r - cy) / b)**2 + ((c - cx) / a)**2) < 1: rr.append(r) cc.append(c) From b1628e07adb94258ca0b7de7f466bcf5ca004fca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Boulogne?= Date: Tue, 12 Mar 2013 20:31:33 +0100 Subject: [PATCH 04/10] fix comment --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index f50ba1ac..3a127d3d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ # vim ft=yaml # travis-ci.org definition for skimage build # -# We pretend to be erlang because we need can't use the python support in +# We pretend to be erlang because we can't use the python support in # travis-ci; it uses virtualenvs, they do not have numpy, scipy, matplotlib, # and it is impractical to build them From a3d73bdd61ba1685c1038af6658edb8682eb1a62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Boulogne?= Date: Sat, 16 Mar 2013 13:25:01 +0100 Subject: [PATCH 05/10] DOC: minor fix ellipse_perim. --- skimage/draw/_draw.pyx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/skimage/draw/_draw.pyx b/skimage/draw/_draw.pyx index 4316cadc..ee2b89e0 100644 --- a/skimage/draw/_draw.pyx +++ b/skimage/draw/_draw.pyx @@ -293,12 +293,12 @@ def ellipse_perimeter(Py_ssize_t cy, Py_ssize_t cx, Py_ssize_t yradius, cy, cx : int Centre coordinate of ellipse. yradius, xradius: int - Main radial values. + Minor and major semi-axes. ``(x/xradius)**2 + (y/yradius)**2 = 1``. Returns ------- rr, cc : (N,) ndarray of int - Indices of pixels that belong to the circle perimeter. + Indices of pixels that belong to the ellipse perimeter. May be used to directly index into an array, e.g. ``img[rr, cc] = 1``. From 6b1c809a784d6d8b5c7c45da2fae187801999a01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Boulogne?= Date: Tue, 19 Mar 2013 21:16:34 +0100 Subject: [PATCH 06/10] DOC: add docstring for deprecation --- skimage/transform/hough_transform.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/skimage/transform/hough_transform.py b/skimage/transform/hough_transform.py index e83cecd5..8abcfa32 100644 --- a/skimage/transform/hough_transform.py +++ b/skimage/transform/hough_transform.py @@ -100,6 +100,9 @@ from skimage._shared.utils import deprecated @deprecated('hough_line') def hough(img, theta=None): + """ + This function is deprecated. Use `hough_line` instead. + """ return hough_line(img, theta) from ._hough_transform import _hough_circle From cebc9839e4dd5bf63b98e34be4fe58d23b41b2ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Boulogne?= Date: Tue, 19 Mar 2013 21:18:30 +0100 Subject: [PATCH 07/10] Fix mistake in example --- doc/source/plots/hough_tf.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/source/plots/hough_tf.py b/doc/source/plots/hough_tf.py index 8b745864..42aa9c48 100644 --- a/doc/source/plots/hough_tf.py +++ b/doc/source/plots/hough_tf.py @@ -20,8 +20,8 @@ plt.title('Input image') plt.subplot(1, 2, 2) plt.imshow(out, cmap=plt.cm.bone, - extent=(d[0], d[-1], - np.rad2deg(angles[0]), np.rad2deg(angles[-1]))) + extent=(np.rad2deg(angles[0]), np.rad2deg(angles[-1]), + d[0], d[-1])) plt.title('Hough transform') plt.xlabel('Angle (degree)') plt.ylabel('Distance (pixel)') From 1ddf37b6b620854c5c4ecb2a4bfd535ec526fe7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Boulogne?= Date: Wed, 20 Mar 2013 08:51:04 +0100 Subject: [PATCH 08/10] add note for distance/angle convention. Thanks to Emre Safak. --- skimage/transform/hough_transform.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/skimage/transform/hough_transform.py b/skimage/transform/hough_transform.py index 8abcfa32..d88d7dd0 100644 --- a/skimage/transform/hough_transform.py +++ b/skimage/transform/hough_transform.py @@ -127,6 +127,12 @@ def hough_line(img, theta=None): distances : ndarray Distance values. + Notes + ----- + The origin is the top left corner of the original image. + The angle is counted clockwise from 9 o'clock. + The distance is the minimal algebraic distance from this origin to the line. + Examples -------- Generate a test image: From 30af0a66a24c6a78e74e03db02d2ecfcdb6655f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Boulogne?= Date: Wed, 20 Mar 2013 18:30:19 +0100 Subject: [PATCH 09/10] replace hough() by hough_line() in doc --- doc/examples/plot_hough_transform.py | 4 ++-- doc/source/plots/hough_tf.py | 4 ++-- skimage/transform/hough_transform.py | 12 ++++++------ 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/doc/examples/plot_hough_transform.py b/doc/examples/plot_hough_transform.py index b74132da..b15df92e 100644 --- a/doc/examples/plot_hough_transform.py +++ b/doc/examples/plot_hough_transform.py @@ -59,7 +59,7 @@ References ''' -from skimage.transform import hough, hough_peaks, probabilistic_hough +from skimage.transform import hough_line, hough_peaks, probabilistic_hough from skimage.filter import canny from skimage import data @@ -77,7 +77,7 @@ idx = np.arange(25, 75) image[idx[::-1], idx] = 255 image[idx, idx] = 255 -h, theta, d = hough(image) +h, theta, d = hough_line(image) plt.figure(figsize=(8, 4)) diff --git a/doc/source/plots/hough_tf.py b/doc/source/plots/hough_tf.py index 42aa9c48..ea2fc4bb 100644 --- a/doc/source/plots/hough_tf.py +++ b/doc/source/plots/hough_tf.py @@ -1,7 +1,7 @@ import numpy as np import matplotlib.pyplot as plt -from skimage.transform import hough +from skimage.transform import hough_line img = np.zeros((100, 150), dtype=bool) img[30, :] = 1 @@ -11,7 +11,7 @@ for i in range(90): img[i, i] = 1 img += np.random.random(img.shape) > 0.95 -out, angles, d = hough(img) +out, angles, d = hough_line(img) plt.subplot(1, 2, 1) diff --git a/skimage/transform/hough_transform.py b/skimage/transform/hough_transform.py index d88d7dd0..0d7196c8 100644 --- a/skimage/transform/hough_transform.py +++ b/skimage/transform/hough_transform.py @@ -147,7 +147,7 @@ def hough_line(img, theta=None): Apply the Hough transform: - >>> out, angles, d = hough(img) + >>> out, angles, d = hough_line(img) .. plot:: hough_tf.py @@ -187,12 +187,12 @@ def hough_peaks(hspace, angles, dists, min_distance=10, min_angle=10, Parameters ---------- hspace : (N, M) array - Hough space returned by the `hough` function. + Hough space returned by the `hough_line` function. angles : (M,) array - Angles returned by the `hough` function. Assumed to be continuous + Angles returned by the `hough_line` function. Assumed to be continuous (`angles[-1] - angles[0] == PI`). dists : (N, ) array - Distances returned by the `hough` function. + Distances returned by the `hough_line` function. min_distance : int Minimum distance separating lines (maximum filter size for first dimension of hough space). @@ -213,14 +213,14 @@ def hough_peaks(hspace, angles, dists, min_distance=10, min_angle=10, Examples -------- >>> import numpy as np - >>> from skimage.transform import hough, hough_peaks + >>> from skimage.transform import hough_line, hough_peaks >>> from skimage.draw import line >>> img = np.zeros((15, 15), dtype=np.bool_) >>> rr, cc = line(0, 0, 14, 14) >>> img[rr, cc] = 1 >>> rr, cc = line(0, 14, 14, 0) >>> img[cc, rr] = 1 - >>> hspace, angles, dists = hough(img) + >>> hspace, angles, dists = hough_line(img) >>> hspace, angles, dists = hough_peaks(hspace, angles, dists) >>> angles array([ 0.74590887, -0.79856126]) From 4e0db80c24085f4d18e7482c866870f227f2dbc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Boulogne?= Date: Wed, 20 Mar 2013 18:30:52 +0100 Subject: [PATCH 10/10] remove docstring --- skimage/transform/hough_transform.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/skimage/transform/hough_transform.py b/skimage/transform/hough_transform.py index 0d7196c8..f112a025 100644 --- a/skimage/transform/hough_transform.py +++ b/skimage/transform/hough_transform.py @@ -100,9 +100,6 @@ from skimage._shared.utils import deprecated @deprecated('hough_line') def hough(img, theta=None): - """ - This function is deprecated. Use `hough_line` instead. - """ return hough_line(img, theta) from ._hough_transform import _hough_circle