Skip to content

Commit

Permalink
Merge pull request #1123 from stefanv/pnpoly
Browse files Browse the repository at this point in the history
Expose point_in_poly and move to measure module
  • Loading branch information
ahojnnes committed Sep 28, 2014
2 parents c99a148 + 5ba4b7c commit d0fb18f
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 23 deletions.
6 changes: 3 additions & 3 deletions bento.info
Expand Up @@ -37,12 +37,12 @@ Library:
skimage.io._plugins, skimage.measure, skimage.morphology,
skimage.scripts, skimage.restoration, skimage.segmentation,
skimage.transform, skimage.util
Extension: skimage.morphology._pnpoly
Sources:
skimage/morphology/_pnpoly.pyx
Extension: skimage.io._plugins._colormixer
Sources:
skimage/io/_plugins/_colormixer.pyx
Extension: skimage.measure._pnpoly
Sources:
skimage/measure/_pnpoly.pyx
Extension: skimage.measure._find_contours_cy
Sources:
skimage/measure/_find_contours_cy.pyx
Expand Down
4 changes: 2 additions & 2 deletions doc/logo/scipy_logo.py
Expand Up @@ -3,11 +3,11 @@
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.nxutils as nx

from skimage import io
from skimage import data

from skimage.measure import points_in_poly

class SymmetricAnchorPoint(object):
"""Anchor point in a parametric curve with symmetric handles
Expand Down Expand Up @@ -211,7 +211,7 @@ def get_mask(self, shape, region):
y_img, x_img = np.mgrid[:h, :w]
xy_points = np.column_stack((x_img.flat, y_img.flat))

mask = nx.points_inside_poly(xy_points, xy_poly)
mask = points_in_poly(xy_points, xy_poly)
return mask.reshape((h, w))


Expand Down
5 changes: 4 additions & 1 deletion skimage/measure/__init__.py
Expand Up @@ -4,6 +4,7 @@
from ._regionprops import regionprops, perimeter
from ._structural_similarity import structural_similarity
from ._polygon import approximate_polygon, subdivide_polygon
from ._pnpoly import points_in_poly, grid_points_in_poly
from ._moments import moments, moments_central, moments_normalized, moments_hu
from .profile import profile_line
from .fit import LineModel, CircleModel, EllipseModel, ransac
Expand All @@ -30,4 +31,6 @@
'mesh_surface_area',
'correct_mesh_orientation',
'profile_line',
'label']
'label',
'points_in_poly',
'grid_points_in_poly']
12 changes: 10 additions & 2 deletions skimage/morphology/_pnpoly.pyx → skimage/measure/_pnpoly.pyx
Expand Up @@ -8,7 +8,7 @@ cimport numpy as cnp
from skimage._shared.geometry cimport point_in_polygon, points_in_polygon


def grid_points_inside_poly(shape, verts):
def grid_points_in_poly(shape, verts):
"""Test whether points on a specified grid are inside a polygon.
For each ``(r, c)`` coordinate on a grid, i.e. ``(0, 0)``, ``(0, 1)`` etc.,
Expand All @@ -23,6 +23,10 @@ def grid_points_inside_poly(shape, verts):
or anti-clockwise. The first point may (but does not need to be)
duplicated.
See Also
--------
points_in_poly
Returns
-------
mask : (M, N) ndarray of bool
Expand Down Expand Up @@ -50,7 +54,7 @@ def grid_points_inside_poly(shape, verts):
return out.view(bool)


def points_inside_poly(points, verts):
def points_in_poly(points, verts):
"""Test whether points lie inside a polygon.
Parameters
Expand All @@ -61,6 +65,10 @@ def points_inside_poly(points, verts):
Vertices of the polygon, sorted either clockwise or anti-clockwise.
The first point may (but does not need to be) duplicated.
See Also
--------
grid_points_in_poly
Returns
-------
mask : (N,) array of bool
Expand Down
3 changes: 3 additions & 0 deletions skimage/measure/setup.py
Expand Up @@ -16,6 +16,7 @@ def configuration(parent_package='', top_path=None):
cython(['_find_contours_cy.pyx'], working_path=base_path)
cython(['_moments.pyx'], working_path=base_path)
cython(['_marching_cubes_cy.pyx'], working_path=base_path)
cython(['_pnpoly.pyx'], working_path=base_path)

config.add_extension('_ccomp', sources=['_ccomp.c'],
include_dirs=[get_numpy_include_dirs()])
Expand All @@ -26,6 +27,8 @@ def configuration(parent_package='', top_path=None):
config.add_extension('_marching_cubes_cy',
sources=['_marching_cubes_cy.c'],
include_dirs=[get_numpy_include_dirs()])
config.add_extension('_pnpoly', sources=['_pnpoly.c'],
include_dirs=[get_numpy_include_dirs(), '../_shared'])

return config

Expand Down
@@ -1,8 +1,7 @@
import numpy as np
from numpy.testing import assert_array_equal

from skimage.morphology._pnpoly import points_inside_poly, \
grid_points_inside_poly
from skimage.measure import points_in_poly, grid_points_in_poly


class test_npnpoly():
Expand All @@ -11,29 +10,29 @@ def test_square(self):
[0, 1],
[1, 1],
[1, 0]])
assert(points_inside_poly([[0.5, 0.5]], v)[0])
assert(not points_inside_poly([[-0.1, 0.1]], v)[0])
assert(points_in_poly([[0.5, 0.5]], v)[0])
assert(not points_in_poly([[-0.1, 0.1]], v)[0])

def test_triangle(self):
v = np.array([[0, 0],
[1, 0],
[0.5, 0.75]])
assert(points_inside_poly([[0.5, 0.7]], v)[0])
assert(not points_inside_poly([[0.5, 0.76]], v)[0])
assert(not points_inside_poly([[0.7, 0.5]], v)[0])
assert(points_in_poly([[0.5, 0.7]], v)[0])
assert(not points_in_poly([[0.5, 0.76]], v)[0])
assert(not points_in_poly([[0.7, 0.5]], v)[0])

def test_type(self):
assert(points_inside_poly([[0, 0]], [[0, 0]]).dtype == np.bool)
assert(points_in_poly([[0, 0]], [[0, 0]]).dtype == np.bool)


def test_grid_points_inside_poly():
def test_grid_points_in_poly():
v = np.array([[0, 0],
[5, 0],
[5, 5]])

expected = np.tril(np.ones((5, 5), dtype=bool))

assert_array_equal(grid_points_inside_poly((5, 5), v),
assert_array_equal(grid_points_in_poly((5, 5), v),
expected)

if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions skimage/morphology/convex_hull.py
@@ -1,7 +1,7 @@
__all__ = ['convex_hull_image', 'convex_hull_object']

import numpy as np
from ._pnpoly import grid_points_inside_poly
from ..measure import grid_points_in_poly
from ._convex_hull import possible_hull
from ..measure._label import label
from skimage.util import unique_rows
Expand Down Expand Up @@ -72,7 +72,7 @@ def convex_hull_image(image):

# For each pixel coordinate, check whether that pixel
# lies inside the convex hull
mask = grid_points_inside_poly(image.shape[:2], v)
mask = grid_points_in_poly(image.shape[:2], v)

return mask

Expand Down
3 changes: 0 additions & 3 deletions skimage/morphology/setup.py
Expand Up @@ -15,7 +15,6 @@ def configuration(parent_package='', top_path=None):
cython(['cmorph.pyx'], working_path=base_path)
cython(['_watershed.pyx'], working_path=base_path)
cython(['_skeletonize_cy.pyx'], working_path=base_path)
cython(['_pnpoly.pyx'], working_path=base_path)
cython(['_convex_hull.pyx'], working_path=base_path)
cython(['_greyreconstruct.pyx'], working_path=base_path)

Expand All @@ -25,8 +24,6 @@ def configuration(parent_package='', top_path=None):
include_dirs=[get_numpy_include_dirs()])
config.add_extension('_skeletonize_cy', sources=['_skeletonize_cy.c'],
include_dirs=[get_numpy_include_dirs()])
config.add_extension('_pnpoly', sources=['_pnpoly.c'],
include_dirs=[get_numpy_include_dirs(), '../_shared'])
config.add_extension('_convex_hull', sources=['_convex_hull.c'],
include_dirs=[get_numpy_include_dirs()])
config.add_extension('_greyreconstruct', sources=['_greyreconstruct.c'],
Expand Down

0 comments on commit d0fb18f

Please sign in to comment.