Add drawing module with Bresenham line.

This commit is contained in:
Stefan van der Walt
2011-09-26 00:40:23 -07:00
parent 0bf8ecc65a
commit 8b522cfa70
7 changed files with 162 additions and 0 deletions
+3
View File
@@ -70,3 +70,6 @@
- Brian Holt
Histograms of Oriented Gradients
- David-Warde Farley, Sturla Molden
Bresenheim line drawing, from snippets on numpy-discussion.
+4
View File
@@ -122,3 +122,7 @@ Implement Algorithms
- Graph cut segmentation
- Probabilistic Hough transform
Drawing
```````
- Wu's algorithm for lines and circles
+1
View File
@@ -0,0 +1 @@
from draw import *
+66
View File
@@ -0,0 +1,66 @@
import numpy as np
cimport numpy as np
cimport cython
cdef extern from "math.h":
int abs(int i)
@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
+6
View File
@@ -0,0 +1,6 @@
"""
Methods to draw on arrays.
"""
from _draw import bresenham
+30
View File
@@ -0,0 +1,30 @@
#!/usr/bin/env python
import os
from scikits.image._build import cython
base_path = os.path.abspath(os.path.dirname(__file__))
def configuration(parent_package='', top_path=None):
from numpy.distutils.misc_util import Configuration, get_numpy_include_dirs
config = Configuration('draw', parent_package, top_path)
config.add_data_dir('tests')
cython(['_draw.pyx'], working_path=base_path)
config.add_extension('_draw', sources=['_draw.c'],
include_dirs=[get_numpy_include_dirs()])
return config
if __name__ == '__main__':
from numpy.distutils.core import setup
setup(maintainer = 'Scikits-image developers',
author = 'Scikits-image developers',
maintainer_email = 'scikits-image@googlegroups.com',
description = 'Drawing',
url = 'https://github.com/scikits-image/scikits.image',
license = 'SciPy License (BSD Style)',
**(configuration(top_path='').todict())
)
+52
View File
@@ -0,0 +1,52 @@
from numpy.testing import assert_array_equal
import numpy as np
from scikits.image.draw import bresenham
def test_bresenham_horizontal():
img = np.zeros((10, 10))
rr, cc = bresenham(0, 0, 0, 9)
img[rr, cc] = 1
img_ = np.zeros((10, 10))
img_[0, :] = 1
assert_array_equal(img, img_)
def test_bresenham_vertical():
img = np.zeros((10, 10))
rr, cc = bresenham(0, 0, 9, 0)
img[rr, cc] = 1
img_ = np.zeros((10, 10))
img_[:, 0] = 1
assert_array_equal(img, img_)
def test_reverse():
img = np.zeros((10, 10))
rr, cc = bresenham(0, 9, 0, 0)
img[rr, cc] = 1
img_ = np.zeros((10, 10))
img_[0, :] = 1
assert_array_equal(img, img_)
def test_diag():
img = np.zeros((5, 5))
rr, cc = bresenham(0, 0, 4, 4)
img[rr, cc] = 1
img_ = np.eye(5)
assert_array_equal(img, img_)
if __name__ == "__main__":
from numpy.testing import run_module_suite