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.
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.3-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5f52576f2ac8b74093f9a6c496596853310ab3312421e20deb6f7d282cda8ac9 |
|
MD5 | 6f61920fd893cc0eb3ea2598e7c7b433 |
|
BLAKE2b-256 | d8220ed5f383da4b71af9620589c56c9e50d983173a123eb8d1b171a3e29311e |
Hashes for upolygon-0.1.3-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7cea274ce904bd70029e86fa5bf77c04ab8dcb82ecf556d76155708f176c7907 |
|
MD5 | 36142b8c416314a140aa67f1e0cd688c |
|
BLAKE2b-256 | 575a1e9e6934acbbf541a2a992464111f9dba216628b072095c0fabca9f5d1ab |
Hashes for upolygon-0.1.3-cp36-cp36m-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0b3436792a3a5b6c44009072e16d2adaaa547c3bdd2b0f444a392c3e5f48db20 |
|
MD5 | 2c5dd0c0429380ab1285ab055d734530 |
|
BLAKE2b-256 | d40ba30263363d9790c7385cd5b86de979a2f5b265d582e84fc43aecb5e8fce0 |