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.5-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e1224e57737d4fd6ead1e16a4941616b1e5aac672b25ba572003fc70070b3d40 |
|
MD5 | 6fe21a11878ec592909111d695692b1b |
|
BLAKE2b-256 | f40446855a905c65f43c1c877be515178244a3b1d2b1652b28f19188ca7c13e7 |
Hashes for upolygon-0.1.5-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ec97ef321591af66053a1fc7784d3d2fd6eaceb9d88cabddc734dedae7fbc14f |
|
MD5 | 2183932247ec58bc80d9b6c8c9758115 |
|
BLAKE2b-256 | 68926368e563012413da8c10f899665ee6d51591c0f03934989cc993b72a2b21 |
Hashes for upolygon-0.1.5-cp38-cp38-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7235ffacca111f0dfc500ac76da538193420d2a26874a15d3baeeebba35e622e |
|
MD5 | 5fae62836ca4f8bb798b3bb0d7d35aab |
|
BLAKE2b-256 | 155c1592dba9763b786b42e79dc5e53b1ba7b52730f5e092c79ede351b1184fb |
Hashes for upolygon-0.1.5-cp38-cp38-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4fccd762542968fc6dd91fd2ae03a499309141c274f5f568354f7fd7a115b088 |
|
MD5 | e40d2b0bcb98def0b379b0c4c7e317c0 |
|
BLAKE2b-256 | d00bd8a685b1a4547466e7c929707ea6875b52d8f49c07eb7ab3ed918510add3 |
Hashes for upolygon-0.1.5-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ff826659f87293ffaeac6fb6973beebbb3fa861dd8e1301b638a6813cf5b629b |
|
MD5 | e297dfae6af4db5fc88590d4d03cd97d |
|
BLAKE2b-256 | d7d6bb34c519d89fb350c936d0ad47416da7ea1d2f55d59a36a2107b94c0f2af |
Hashes for upolygon-0.1.5-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f9cf50183ff7fde8e39294edfc336feb26bec8610e555659d5cc5b6edf6dbc9e |
|
MD5 | a9f740639e4ba7b1771919f78c54f199 |
|
BLAKE2b-256 | 77e96d8da4f07d781013fbb2670986ede2fef017698c4eeabc8cfd3c3aca8bb4 |
Hashes for upolygon-0.1.5-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f1edc8655368bd1fbf1e20b693aff819eaf5d4d3eb5d4d875c4a931f4ef4a983 |
|
MD5 | 729778fc4decfa882c77e1f40650555e |
|
BLAKE2b-256 | 37d8f24f4d71e9cb325fc5fb413684b8a1e5d97006ce6dd428851a759739346b |
Hashes for upolygon-0.1.5-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2d32d70a681480dc370fb49214e54a667780b5a090d9b9d6cf9d549a4b81ba1f |
|
MD5 | f1315c4483ff2df215de658baaa10819 |
|
BLAKE2b-256 | 5c52dd47246006f1785335bb204bf927cd59ca043cfd3f072ed7f8899f749028 |
Hashes for upolygon-0.1.5-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | be9dcae65a8b027de479a34b053c3161789b07912100b45e123934f1d4545e5f |
|
MD5 | 0bd5acfef5186f411f23eed7da3f44fa |
|
BLAKE2b-256 | c0f59cfd55f97e08c4aa2edc489bb02d6d413e8ff97af35b3e16f263f6df82c4 |
Hashes for upolygon-0.1.5-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 466519f5bbbc690138383670c50e8601febab6412dd4f27b42575834774afe60 |
|
MD5 | bd35b0b3a7b907012fb223f79ed3ea85 |
|
BLAKE2b-256 | 21fbe023552d90942bb62e69fbd1853b926a578696656e20c38b20e57d02194c |
Hashes for upolygon-0.1.5-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 57549f21bd8103c08e8db026d2e8f9c822b570892e897cbec6b7ff847b1cbd94 |
|
MD5 | 8b4fa87f6ee9918e22eb86b778958481 |
|
BLAKE2b-256 | c918bf931c391b5904e5ebc3be3336ec416636b0ea6e170d9ca786c4a04aa198 |
Hashes for upolygon-0.1.5-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d845848f7e554b04fc30a46ade8a0ed0ed67e4ec39d3e0a853f349b156547d11 |
|
MD5 | eeeff8b1e14f5318fa3051a8e0a09084 |
|
BLAKE2b-256 | d06690d255f43ef52301d250ac728fddc6059a88358e7391c07dcc1a92822bd9 |
Hashes for upolygon-0.1.5-cp36-cp36m-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b7c53f789fbe140d9a6198a06ca6527a0084f487a6afa55b10110e02ea7febfb |
|
MD5 | 0617bc0a1a45c9cec1a67fbef5201706 |
|
BLAKE2b-256 | f731b070a74b8b037134373e90a13942ea935287e385dc91b03d49ccc3e64e7e |