Skip to content

Commit

Permalink
Merge pull request #1 from v7labs/simplify-polygon
Browse files Browse the repository at this point in the history
Simplify polygons
  • Loading branch information
simedw authored Oct 19, 2020
2 parents e60103c + 2dfde9c commit 42b1f83
Show file tree
Hide file tree
Showing 8 changed files with 5,090 additions and 398 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
extensions = [
setuptools.Extension("draw_polygon", ["upolygon/draw_polygon" + ext], extra_compile_args=["-O3", "-Wall"]),
setuptools.Extension("find_contours", ["upolygon/find_contours" + ext], extra_compile_args=["-O3", "-Wall"]),
setuptools.Extension("simplify_polygon", ["upolygon/simplify_polygon" + ext], extra_compile_args=["-O3", "-Wall"]),
]

if USE_CYTHON:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_find_contours.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np

from upolygon import draw_polygon, find_contours
from upolygon import draw_polygon, find_contours, simplify_polygon


def test_single_pixel():
Expand Down
32 changes: 32 additions & 0 deletions tests/test_simplify_polygon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import numpy as np

from upolygon import simplify_polygon


def test_empty_path():
assert len(simplify_polygon([], 1)) == 0


def test_empty_sub_path():
assert len(simplify_polygon([[]], 1)) == 1


def test_removes_linear_points():
path = [0, 0, 0, 5, 0, 10, 0, 15]
assert simplify_polygon([path], 0) == [[0, 0, 0, 15]]


def test_keeps_non_linear_points():
path = [0, 0, 0, 5, 0, 7, 10, 10, 0, 15]
assert simplify_polygon([path], 1) == [[0, 0, 0, 7, 10, 10, 0, 15]]


def test_respects_epsilon():
path = [0, 0, 1, 1, 0, 10]
assert simplify_polygon([path], 1) == [[0, 0, 0, 10]]
assert simplify_polygon([path], 0.1) == [[0, 0, 1, 1, 0, 10]]


def test_handles_repeats():
path = [0, 0, 0, 0, 0, 0, 1, 1]
assert simplify_polygon([path], 1) == [[0, 0, 1, 1]]
1 change: 1 addition & 0 deletions upolygon/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from draw_polygon import draw_polygon # noqa
from find_contours import find_contours # noqa
from simplify_polygon import simplify_polygon # noqa
916 changes: 520 additions & 396 deletions upolygon/find_contours.c

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion upolygon/find_contours.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

cimport cython
import numpy as np
from simplify_polygon import simplify_polygon

# This implementation is based on https://www.iis.sinica.edu.tw/papers/fchang/1362-F.pdf

Expand Down Expand Up @@ -108,4 +109,6 @@ def find_contours(unsigned char[:,:] image):
if px > width-1:
px = 1
py = py + 1
return labels, outer_paths, inner_paths

# return labels, outer_paths, inner_paths
return labels, simplify_polygon(outer_paths, 0), simplify_polygon(inner_paths, 0)
Loading

0 comments on commit 42b1f83

Please sign in to comment.