Collection of fast polygon operations for DL
Project description
uPolygon
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.7-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9608cdeec596e74ac145e218056094383bf042a5382ecc18e1e170d37b9e436b |
|
MD5 | 8a56dc2749f60825d4290dfc5d815c72 |
|
BLAKE2b-256 | 1e4bf97ce413b1b1e18f1bb52f5bf426cb68a6328d3aca547b90fc58b1ca8fc0 |
Hashes for upolygon-0.1.7-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7c84b830a57492ef4577efaa9724b7ca041753a4cb1bfe0388cb38753900db7f |
|
MD5 | 2a7461f18970ccc4046203a3f535935b |
|
BLAKE2b-256 | 512c85038f1e2250e8ebf7da732272845e97614cf9b7cef8e7f80d6af06bbe87 |
Hashes for upolygon-0.1.7-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ab0375739ebc73caf2695ca8bfeb9e5705b685589c933f02bb2af6df93d7eebc |
|
MD5 | ffaef83b268d9ab3ac1aa7c59e32265b |
|
BLAKE2b-256 | 8337ca80e9b39523ba856d606d9830c9a773e122249a867576216fc5c53ecb36 |
Hashes for upolygon-0.1.7-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cbd538cbf501b6db254ba85eff8bf210c7d648f9b9e918779105c2c3ea55b3eb |
|
MD5 | 04b29a39f2e428b5c63d2aa6005b990c |
|
BLAKE2b-256 | 72622ccbba0efa39d3dddc233a7c3861da45d8720b9256b2b768b538fb0d0bde |
Hashes for upolygon-0.1.7-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0ecbceb22b64c2a962b2e760909948144bbb11102a7a8860051fed339c0bb60d |
|
MD5 | 32888818e4cc6e87a84cef529a8c2242 |
|
BLAKE2b-256 | 2925ddf6cb7eafe0c620839198ed64d70efdf35e78ad51587382b498c37a2bc4 |
Hashes for upolygon-0.1.7-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9171b67bce487447bc57b6eec8a48bb137bb69e40c18782171809a6577e01912 |
|
MD5 | bd478cd8b5a9d4a254c948bb8dbe66cc |
|
BLAKE2b-256 | 00a9119810d6303a8254b90deb603028b1fbfa0491527b10cd2d983f3c501b31 |
Hashes for upolygon-0.1.7-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ec960bb7c9e191d305dc8434ba7118b967c9d96b4807a6aaa3174178c3af0737 |
|
MD5 | 2e3855994aa33c6fef2920ead49941a2 |
|
BLAKE2b-256 | 6b0e401dd5553221cf691ec2eafa3ef381e34c09dc02bd2e6139c95752af91e0 |
Hashes for upolygon-0.1.7-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3abf25d8b0ef634dcc78e5e5eb8c156665616c33eabf9082c6a74359d3c0f0fe |
|
MD5 | 99449ed01e2e6052d00481251f9d6735 |
|
BLAKE2b-256 | c0ce48c837352718e8b236368302682ec60a401c34c521911eb64e4ba715e3ac |
Hashes for upolygon-0.1.7-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d45215486510fe42006c4619c8240ddf4fd693d94b6446523091e9e74742b9cc |
|
MD5 | 8bea1cce852d57d51db0acb20185913a |
|
BLAKE2b-256 | ec16b2a40bfa80d93ced6ec11469ece2fb43b8c2493ce0ea8fd6985c677a8d54 |