mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-20 12:40:31 +08:00
154 lines
4.0 KiB
Cython
154 lines
4.0 KiB
Cython
import numpy as np
|
|
import math
|
|
from libc.stdlib cimport malloc, free
|
|
cimport numpy as np
|
|
cimport cython
|
|
|
|
cdef extern from "../morphology/_pnpoly.h":
|
|
int pnpoly(int nr_verts, double *xp, double *yp,
|
|
double x, double y)
|
|
|
|
@cython.boundscheck(False)
|
|
@cython.wraparound(False)
|
|
def bresenham(int y, int x, int y2, int x2):
|
|
"""Generate line pixel coordinates.
|
|
|
|
Parameters
|
|
----------
|
|
y, x : int
|
|
Starting position (row, column).
|
|
y2, x2 : int
|
|
End position (row, column).
|
|
|
|
Returns
|
|
-------
|
|
rr, cc : (N,) ndarray of int
|
|
Indices of pixels that belong to the line.
|
|
May be used to directly index into an array, e.g.
|
|
``img[rr, cc] = 1``.
|
|
|
|
"""
|
|
cdef np.ndarray[np.int32_t, ndim=1, mode="c"] rr, cc
|
|
|
|
cdef int steep = 0
|
|
cdef int dx = abs(x2 - x)
|
|
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 dy > dx:
|
|
steep = 1
|
|
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)
|
|
cc = np.zeros(int(dx) + 1, dtype=np.int32)
|
|
|
|
for i in range(dx):
|
|
if steep:
|
|
rr[i] = x
|
|
cc[i] = y
|
|
else:
|
|
rr[i] = y
|
|
cc[i] = x
|
|
while d >= 0:
|
|
y = y + sy
|
|
d = d - (2 * dx)
|
|
x = x + sx
|
|
d = d + (2 * dy)
|
|
|
|
rr[dx] = y2
|
|
cc[dx] = x2
|
|
|
|
return rr, cc
|
|
|
|
@cython.boundscheck(False)
|
|
@cython.wraparound(False)
|
|
def _polygon_area(np.ndarray[np.double_t, ndim=1] x, np.ndarray[np.double_t, ndim=1] y):
|
|
"""Calculate area of polygon.
|
|
|
|
Parameters
|
|
----------
|
|
x : ndarray
|
|
X coordinates of polygon
|
|
y : ndarray
|
|
Y coordinates of polygon
|
|
|
|
Returns
|
|
-------
|
|
area : double
|
|
area of polygon
|
|
"""
|
|
cdef double area
|
|
cdef int i
|
|
cdef int j = x.shape[0]-1
|
|
for i in xrange(x.shape[0]):
|
|
area += (x[j]+x[i])*(y[j]-y[i])
|
|
j = i
|
|
return abs(0.5*area)
|
|
|
|
@cython.boundscheck(False)
|
|
@cython.wraparound(False)
|
|
def polygon(verts, shape=None):
|
|
"""Generate coordinates of pixels within polygon.
|
|
|
|
Parameters
|
|
----------
|
|
verts : Nx2 ndarray
|
|
(row, col) coordinates
|
|
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,
|
|
default None
|
|
|
|
Returns
|
|
-------
|
|
rr, cc : ndarray of int
|
|
Pixel coordinates of polygon.
|
|
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())
|
|
|
|
# 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)
|
|
|
|
cdef int r, c
|
|
cdef int i = 0
|
|
|
|
#: 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')
|
|
cdef np.double_t* rptr = <np.double_t*>contiguous_rdata.data
|
|
cdef np.double_t* cptr = <np.double_t*>contiguous_cdata.data
|
|
|
|
# use area of polygon to determine the rough size of the output arrays
|
|
cdef double area = _polygon_area(contiguous_cdata, contiguous_rdata)
|
|
|
|
#: output coordinate arrays
|
|
cdef np.ndarray[np.int32_t, ndim=1, mode="c"] rr, cc
|
|
rr = np.zeros(int(area), dtype=np.int32)
|
|
cc = np.zeros(int(area), dtype=np.int32)
|
|
|
|
for r in range(minr, maxr+1):
|
|
for c in range(minc, maxc+1):
|
|
if pnpoly(nr_verts, cptr, rptr, c, r):
|
|
rr[i] = r
|
|
cc[i] = c
|
|
i += 1
|
|
|
|
# area >= number of points in polygon, so crop actual points
|
|
return rr[:i], cc[:i]
|