Collection of fast polygon operations for DL
Project description
uPolygon (WIP)
Library of handy polygon related functions to speed up machine learning projects.
It was born as a replacement for cv2.fillPoly
when generating masks for instance segmentation, without having to bring in all of opencv.
TODO
- draw_polygon
- find_contours
- polygon_area
- point_in_polygon
Usage
This library expects all polygons to be model as a list of paths, each path is a list of alternating x and y coordinates ([x1,y1,x2,y2,...]
).
A simple triangle would be declared as:
triangle = [[50,50, 100,0, 0,0]]
Complex polygons (holes and/or disjoints) follow the even-odd rule.
draw_polygon
draw_polygon(mask: array[:, :], paths: path[]) -> array[:, :]
from upolygon import draw_polygon
import numpy as np
mask = np.zeros((100,100), dtype=np.int32)
draw_polygon(mask, [[50,50, 100,0, 0,0]], 1)
Equivalent of calling cv2.fillPoly(mask, [np.array([[50,50], [100,0], [0,0]])], 1)
or cv2.drawContours(mask, [np.array([[50,50], [100,0], [0,0]])], -1, 1, cv2.FILLED)
when using opencv.
uPolygon is ~ 6 times faster than opencv for large random polygons with many intersecting lines. For smaller polygons or few intersections, uPolygon is half as fast as opencv.
find_contours
find_contours(mask: array[:, :]) -> (array[:, :], path[:], path[:])
0 is treated as background, 1 is treated as foreground.
from upolygon import find_contours
import numpy as np
mask = np.array([
[0, 0, 0, 0, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0]
], dtype=np.uint8)
_labels, external_paths, internal_paths = find_contours(mask)
Similar to OpenCV's cv2.findContours
but lacking hierarchies. Also similar to BoofCV's LinearContourLabelChang2004
which is based on the same algorithm.
Note that currently the input mask to find_contour needs to be uint8.
rle_encode
rle_encode(mask: array[:,:]) -> list
Takes a 2-dim binary mask and generates a run length encoding according to the coco specs
~ 15 times faster than written in plain python
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for upolygon-0.1.6-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 02686e5f116ea999f2c0b023b3e36a303bf9194c16d5a22bed0624faeb97cd85 |
|
MD5 | 74f881d41c950ef78bdeb4248c2263db |
|
BLAKE2b-256 | 4ac2cbaf40ef0e8df590f89960752a86f38043c23a09656d5f96757a17656905 |
Hashes for upolygon-0.1.6-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f46399b9476bb8f617efc316d5c1fda3df975ddfdcd4653174b4312e2d96504f |
|
MD5 | 4270b539a83f007400a1bb4687a9b5f7 |
|
BLAKE2b-256 | bb366fdac026769488628753050e725d88934ce10df710f8475b5fe7a1179572 |
Hashes for upolygon-0.1.6-cp38-cp38-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 273890111325ddee1a09a821a478084dfc857446d7a067475ff93dd319238858 |
|
MD5 | 51936b09c21b11feed96dfb2657452bb |
|
BLAKE2b-256 | 029dda42b75b8dd0e8baa648ae6624330772e18e43870cf154a3f1086863cbd2 |
Hashes for upolygon-0.1.6-cp38-cp38-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d487d72290c3bd405b1480e1773748dd91e4a84f56a000d149b369e16e93cc13 |
|
MD5 | 5b9df34866a54e14d9afa269c8fcfb4a |
|
BLAKE2b-256 | 5e77605804f45791cce1c8e69db943e4cd813c85a595718bc284ec72a76042e2 |
Hashes for upolygon-0.1.6-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2874c142191c1980fa949ef1ccc2f62dc90255a6721e2322323e4d5659a4f6ea |
|
MD5 | b84b972465bd579f1c01cc81242f4e17 |
|
BLAKE2b-256 | e1033335db8ed9224bbd880bde452ed578fb7c9762b44837302e8c00211f54ce |
Hashes for upolygon-0.1.6-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8203144391eba29042d452bc84365d501359a7bc7d2e9dee3e78cf84798f8e32 |
|
MD5 | bb4652ead99114eff0991d2afe54405f |
|
BLAKE2b-256 | 584cc44e391c025eb09738897a6dabe4ac19e12243163fb5a4773e1baa893d3e |
Hashes for upolygon-0.1.6-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d9acfa2defa872b9068f9b565b30321992ff5134ad66b9adf7a747f75a9e9cff |
|
MD5 | 655d2a26ea31791c2315a52a85379e06 |
|
BLAKE2b-256 | bc72af75dfa3f18608dd008438d905c1cebd813bc4389ffe0f99f65d0d49e411 |
Hashes for upolygon-0.1.6-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 66ab0a816287ee821d5f82515f6c31ef93de81c4dbc58265a65a8e28ee87a667 |
|
MD5 | cd31dcf6612ef5c1469257b8c8af815c |
|
BLAKE2b-256 | 1b10a52060e5fe53c98386ae50ef3c112ade8e0f02262cca66f2d7acd4596b77 |
Hashes for upolygon-0.1.6-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5eaef5a0051390fc64faf1dafc36caa70d658deae8b60d397e9e17187664af58 |
|
MD5 | ec0b1419561c127277ee2f28bd013f7f |
|
BLAKE2b-256 | 5564d8fc2fe94f18445a351799ca9b44ca8bf1f5eec758d454af6f2923892186 |
Hashes for upolygon-0.1.6-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9a9afc9b646699657528f39fab79e2166e698e899942fa3aa898f9edf3247467 |
|
MD5 | fa6cd20d5aabead59fd68457a3d134ff |
|
BLAKE2b-256 | 7840af5375db716e8f0709c5abd4dfaa9bd6b440c1467aaad4d5ea836f48ba83 |
Hashes for upolygon-0.1.6-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0ce36438425d98e69e4937c7a3edc63539dfade0a038a5befc81e5cbbd154903 |
|
MD5 | 930282f564727809deb0d5d0b31c31f5 |
|
BLAKE2b-256 | 0c2a85bd1d87ead80fbed2349c27abc71cb19e0276d2b58ab60fa3c470200024 |
Hashes for upolygon-0.1.6-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e7456774d1866d4ae7a633378a77a7d3628b3d68e6ee0a0c4702c5e51daf9ce6 |
|
MD5 | 964d84e9b313f52892eb1df11ff270ac |
|
BLAKE2b-256 | 758c56a94a82b9900bc734fe0db832a604570287bd27f3a20b11de884e80cdf7 |
Hashes for upolygon-0.1.6-cp36-cp36m-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 921f90aa15647525ac663263caf4e95482994becd3c2e032a18e20ae199eec39 |
|
MD5 | ee8a46edcff6b0b2c645b2e832d26f32 |
|
BLAKE2b-256 | 3d9abb82d40fb5e5a84a910aef4afec54bdfb3c5dd0e21a2db6d9f715e23a6a8 |