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.10-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 480749a637feaab7da667be4c62f64472082ef74c9059ec2130ab8a3e2f4411d |
|
MD5 | a8003745499291738530f3909efc022c |
|
BLAKE2b-256 | e684df9abd502be6fd27ab5fded2cc511f4d1d921f6db2784a827c99de5936e1 |
Hashes for upolygon-0.1.10-cp310-cp310-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 363c0e10768c64da5563242d4c4b0a83ff001d7d482b41bf8817381e31d61e26 |
|
MD5 | 5d08350501bf934d8364ed4553ec9ff5 |
|
BLAKE2b-256 | e80e9705f473fc04bee73e542ed7bcb3096dd1dbdc91774f4b3607073a12af88 |
Hashes for upolygon-0.1.10-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fc73ae4db1551b0d433f98a4092ec71a64bf0eee5da4fff5ca0d95a410e07549 |
|
MD5 | 78362a5846f536e6ca87e79eeae38b9c |
|
BLAKE2b-256 | 04be861d21ae0463a99232d2461762850ad38689b4a7974140b5a70bc9f9836b |
Hashes for upolygon-0.1.10-cp39-cp39-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7c6735d14d7c2c5296e1c94c1a5a539ad4d7716974e433265c62d7734d9e37be |
|
MD5 | b33f2810fcc06ad85b8963bbc6c7858e |
|
BLAKE2b-256 | b66367f59a62a71a22d5a8675e6a74a64d45c0d9ee4475e9ddae163129722f55 |
Hashes for upolygon-0.1.10-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8349feb3c1c49e17caccf572861d022c9fd6d9e06fdad13662167fd561483f61 |
|
MD5 | ad9f44ae442cae6cba817beb1873e997 |
|
BLAKE2b-256 | ef3e895cec87a5dc18d6557feaf3e7e20d5627da682a2a7847d0b43ae2156970 |
Hashes for upolygon-0.1.10-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 364d4dbf4b495aeb167cb00c256519a856a1216ec8be9eb3f1b91af3188c2146 |
|
MD5 | 078495bb52ebf6d388fb46b5828a7f7e |
|
BLAKE2b-256 | 756f818429dffa860b73e9ddbd38e15dc9f67e58ec4125378dfa9fb7f9f0d00d |
Hashes for upolygon-0.1.10-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b44da1bd58ada96362a8e2c77ba748dd45f8e590e029576402d85d6fb0a39c8b |
|
MD5 | e60b569a4efabdee1cbed704be323cbb |
|
BLAKE2b-256 | e5b0ca7be1bb394d13cacd09dfc5301974673db4df2e26b8a5ce0e022f124b36 |
Hashes for upolygon-0.1.10-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c0604ce5536f9fc43f17020e0d71b090cc706bc9a2e6f2527e6915fa8789f58a |
|
MD5 | 905b7a6c2860b6df5bc1df60da4a5034 |
|
BLAKE2b-256 | d3a9e7a813121c48e7e3b9651d17ad16797ef132c95ca04fa535c5969f596464 |