changed interface of draw.polygon function for a more consistent usage

This commit is contained in:
Johannes Schönberger
2012-04-23 18:38:20 +02:00
parent 48328cda8a
commit 5b854d9b3c
3 changed files with 26 additions and 16 deletions
+1 -1
View File
@@ -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
+21 -11
View File
@@ -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 = <int>max(0, verts[:,0].min())
cdef int maxr = <int>math.ceil(verts[:,0].max())
cdef int minc = <int>max(0, verts[:,1].min())
cdef int maxc = <int>math.ceil(verts[:,1].max())
cdef int nr_verts = x.shape[0]
cdef int minr = <int>max(0, y.min())
cdef int maxr = <int>math.ceil(y.max())
cdef int minc = <int>max(0, x.min())
cdef int maxc = <int>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 = <np.double_t*>contiguous_rdata.data
cdef np.double_t* cptr = <np.double_t*>contiguous_cdata.data
+4 -4
View File
@@ -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))