-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7b4441c
commit c4f45d5
Showing
6 changed files
with
220 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
### v0.1.6 | ||
|
||
* #19 Improve error messages | ||
* #21 Polygons around barcodes | ||
|
||
### v0.1.5 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from collections import namedtuple | ||
from itertools import chain | ||
from operator import itemgetter | ||
|
||
|
||
__all__ = ['bounding_box', 'convex_hull', 'Rect'] | ||
|
||
|
||
Point = namedtuple('Point', ['x', 'y']) | ||
Rect = namedtuple('Rect', ['left', 'top', 'width', 'height']) | ||
|
||
|
||
def bounding_box(locations): | ||
"""Computes the bounding box of an iterable of (x, y) coordinates. | ||
Args: | ||
locations: iterable of (x, y) tuples. | ||
Returns: | ||
`Rect`: Coordinates of the bounding box. | ||
""" | ||
x_values = list(map(itemgetter(0), locations)) | ||
x_min, x_max = min(x_values), max(x_values) | ||
y_values = list(map(itemgetter(1), locations)) | ||
y_min, y_max = min(y_values), max(y_values) | ||
return Rect(x_min, y_min, x_max - x_min, y_max - y_min) | ||
|
||
|
||
def convex_hull(points): | ||
"""Computes the convex hull of an iterable of (x, y) coordinates. | ||
Args: | ||
locations: iterable of (x, y) tuples. | ||
Returns: | ||
`list`: instances of `Point` - vertices of the convex hull in | ||
counter-clockwise order, starting from the vertex with the | ||
lexicographically smallest coordinates. | ||
Andrew's monotone chain algorithm. O(n log n) complexity. | ||
https://en.wikibooks.org/wiki/Algorithm_Implementation/Geometry/Convex_hull/Monotone_chain | ||
""" | ||
|
||
def is_not_clockwise(p0, p1, p2): | ||
return 0 <= ( | ||
(p1[0] - p0[0]) * (p2[1] - p0[1]) - | ||
(p1[1] - p0[1]) * (p2[0] - p0[0]) | ||
) | ||
|
||
def go(points): | ||
res = [] | ||
for p in points: | ||
while 1 < len(res) and is_not_clockwise(res[-2], res[-1], p): | ||
res.pop() | ||
res.append(p) | ||
|
||
# The last point in each list is the first point in the other list | ||
res.pop() | ||
|
||
return res | ||
|
||
# Discard duplicates and sort by x then y | ||
points = sorted(set(points)) | ||
|
||
if len(points) < 2: | ||
return points | ||
else: | ||
return list(map(Point._make, chain(go(points), go(reversed(points))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import unittest | ||
|
||
from pyzbar.locations import bounding_box, convex_hull, Rect | ||
|
||
|
||
class TestLocations(unittest.TestCase): | ||
def test_bounding_box(self): | ||
self.assertRaises(ValueError, bounding_box, []) | ||
self.assertEqual( | ||
Rect(left=0, top=0, width=0, height=0), | ||
bounding_box([(0, 0)]) | ||
) | ||
self.assertEqual( | ||
Rect(left=37, top=550, width=324, height=76), | ||
bounding_box([(37, 551), (37, 625), (361, 626), (361, 550)]) | ||
) | ||
|
||
def test_convex_hull_empty(self): | ||
self.assertEqual([], convex_hull([])) | ||
|
||
def test_convex_square(self): | ||
points = [(0, 0), (0, 1), (1, 1), (1, 0)] | ||
self.assertEqual(points, convex_hull(points)), | ||
|
||
def test_convex_duplicates(self): | ||
points = [(0, 0), (0, 1), (1, 1), (1, 0)] | ||
self.assertEqual(points, convex_hull(points * 10)), | ||
|
||
def test_other(self): | ||
# Taken from | ||
# https://codegolf.stackexchange.com/questions/11035/find-the-convex-hull-of-a-set-of-2d-points | ||
res = convex_hull([(1, 1), (2, 2), (3, 3), (1, 3)]) | ||
self.assertEqual([(1, 1), (1, 3), (3, 3)], res) | ||
|
||
res = convex_hull([ | ||
(4.4, 14), (6.7, 15.25), (6.9, 12.8), (2.1, 11.1), (9.5, 14.9), | ||
(13.2, 11.9), (10.3, 12.3), (6.8, 9.5), (3.3, 7.7), (0.6, 5.1), | ||
(5.3, 2.4), (8.45, 4.7), (11.5, 9.6), (13.8, 7.3), (12.9, 3.1), | ||
(11, 1.1) | ||
]) | ||
|
||
expected = [ | ||
(0.6, 5.1), (2.1, 11.1), (4.4, 14), (6.7, 15.25), (9.5, 14.9), | ||
(13.2, 11.9), (13.8, 7.3), (12.9, 3.1), (11, 1.1), (5.3, 2.4) | ||
] | ||
self.assertEqual(expected, res) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters