diff --git a/doc/examples/plot_shapes.py b/doc/examples/plot_shapes.py index 806049ad..55569016 100644 --- a/doc/examples/plot_shapes.py +++ b/doc/examples/plot_shapes.py @@ -31,7 +31,7 @@ poly = np.array(( (220, 590), (300, 300), )) -rr, cc = polygon(poly, img.shape) +rr, cc = polygon(poly[:,0], poly[:,1], img.shape) img[rr,cc,1] = 255 #: fill circle diff --git a/skimage/draw/_draw.pyx b/skimage/draw/_draw.pyx index ac4b078c..d124f75d 100644 --- a/skimage/draw/_draw.pyx +++ b/skimage/draw/_draw.pyx @@ -72,13 +72,15 @@ def line(int y, int x, int y2, int x2): @cython.boundscheck(False) @cython.wraparound(False) @cython.nonecheck(False) -def polygon(verts, shape=None): +def polygon(y, x, shape=None): """Generate coordinates of pixels within polygon. Parameters ---------- - verts : Nx2 ndarray - (row, col) vertices of polygon + y : (N,) ndarray + y coordinates of vertices of polygon + x : (N,) ndarray + x coordinates of vertices of polygon shape : tuple, optional image shape which is used to determine maximum extents of output pixel coordinates. This is useful for polygons which exceed the image size. @@ -91,11 +93,11 @@ def polygon(verts, shape=None): May be used to directly index into an array, e.g. ``img[rr, cc] = 1``. """ - cdef int nr_verts = verts.shape[0] - cdef int minr = max(0, verts[:,0].min()) - cdef int maxr = math.ceil(verts[:,0].max()) - cdef int minc = max(0, verts[:,1].min()) - cdef int maxc = math.ceil(verts[:,1].max()) + cdef int nr_verts = x.shape[0] + cdef int minr = max(0, y.min()) + cdef int maxr = math.ceil(y.max()) + cdef int minc = max(0, x.min()) + cdef int maxc = math.ceil(x.max()) # make sure output coordinates do not exceed image size if shape is not None: @@ -105,9 +107,17 @@ def polygon(verts, shape=None): cdef int r, c #: make contigous arrays for r, c coordinates - verts = verts.astype('double') - cdef np.ndarray contiguous_rdata = verts[:,0].copy(order='C') - cdef np.ndarray contiguous_cdata = verts[:,1].copy(order='C') + y = y.astype('double') + x = x.astype('double') + cdef np.ndarray contiguous_rdata, contiguous_cdata + if not y.flags['C_CONTIGUOUS']: + contiguous_rdata = y.copy(order='C') + else: + contiguous_rdata = y + if not y.flags['C_CONTIGUOUS']: + contiguous_cdata = x.copy(order='C') + else: + contiguous_cdata = x cdef np.double_t* rptr = contiguous_rdata.data cdef np.double_t* cptr = contiguous_cdata.data diff --git a/skimage/draw/tests/test_draw.py b/skimage/draw/tests/test_draw.py index 42bbafc9..dd1c81e2 100644 --- a/skimage/draw/tests/test_draw.py +++ b/skimage/draw/tests/test_draw.py @@ -52,7 +52,7 @@ def test_polygon_rectangle(): img = np.zeros((10, 10), 'uint8') poly = np.array(((1, 1), (4, 1), (4, 4), (1, 4), (1, 1))) - rr, cc = polygon(poly) + rr, cc = polygon(poly[:,0], poly[:,1]) img[rr,cc] = 1 img_ = np.zeros((10, 10)) @@ -64,7 +64,7 @@ def test_polygon_rectangle_angular(): img = np.zeros((10, 10), 'uint8') poly = np.array(((0, 3), (4, 7), (7, 4), (3, 0), (0, 3))) - rr, cc = polygon(poly) + rr, cc = polygon(poly[:,0], poly[:,1]) img[rr,cc] = 1 img_ = np.array( @@ -86,7 +86,7 @@ def test_polygon_parallelogram(): img = np.zeros((10, 10), 'uint8') poly = np.array(((1, 1), (5, 1), (7, 6), (3, 6), (1, 1))) - rr, cc = polygon(poly) + rr, cc = polygon(poly[:,0], poly[:,1]) img[rr,cc] = 1 img_ = np.array( @@ -108,7 +108,7 @@ def test_polygon_exceed(): img = np.zeros((10, 10), 'uint8') poly = np.array(((1, -1), (100, -1), (100, 100), (1, 100), (1, 1))) - rr, cc = polygon(poly, img.shape) + rr, cc = polygon(poly[:,0], poly[:,1], img.shape) img[rr,cc] = 1 img_ = np.zeros((10, 10))