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-cp38-cp38-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0dcb571e3c689850bf662106a5561349ffdd9031fb344582827b65b752087c52 |
|
MD5 | 5615ea106ba2837011a93a5b6d3777c2 |
|
BLAKE2b-256 | 5d6e47a5dd157857c6ef75cb1b5eaaea63176df6d655536b85f991ff0adaf4a2 |
Hashes for upolygon-0.1-cp38-cp38-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d08e9a2f02e331fcbabef69db3d062f69c404b17390fff224fd216e7d6cc9aba |
|
MD5 | b57eb6ff3931a9669b042e5a18635800 |
|
BLAKE2b-256 | 374c5070b675f6eafdd1fa0ae67e19063400ac357e9bb0f29ca7fb02518c9078 |
Hashes for upolygon-0.1-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | eb1441f2e6b8df895dcb097d2a89aa927b2b3f07e1d18dbfbe78cd6977aedcad |
|
MD5 | f0abae9daf4b0e70295b3a91e50af861 |
|
BLAKE2b-256 | 68afffb5f202e528d6954c182b6304bb7ea23de7abaf6cc42da7e2b5e0b138bd |
Hashes for upolygon-0.1-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 61370186286ae86c5d526a5798bd4776f5637b932d924ed3d5a79c27accbd8c9 |
|
MD5 | 027d6740880bc72d7320fd8e5b068f37 |
|
BLAKE2b-256 | 22975e3601172b0fa8f5a424ca4ff160559bb30c2bac4564aaf0ae0bc319b729 |
Hashes for upolygon-0.1-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 89b08c58674ee9612e5a868b6d5392b26dee17dd6b7498879b72d0b6bc4f683d |
|
MD5 | c2197e0c662b3a0843f5cf90370016f5 |
|
BLAKE2b-256 | 2b6bb051aaf5edaf4db1036688c801341f99bc2bf9a95941f5550884330c5078 |
Hashes for upolygon-0.1-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c83a9749aa4696c28d81232f09e24635a86101d232ba55b42a112eaee3f3f81d |
|
MD5 | 1e70e2acfc97d4ec79b1114b583bd8c5 |
|
BLAKE2b-256 | 18870c756aff5125a2b644b13c2855f4f5ac3cb9f048293c58dfd8574708d3b4 |
Hashes for upolygon-0.1-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 212743030369f70005b9426d9169fc1f30a29c6d49a0977ca268f3d69cec12b5 |
|
MD5 | 78b7802b6fa6096bad0bc8723b15fd9b |
|
BLAKE2b-256 | 037cf31f8d2f9a0447c9cfae700db5e4bab426526f9f3d403476812ae828ca4c |
Hashes for upolygon-0.1-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e0923dae5736bc655a2f833385d365c37108f1e8f0cb434f3ad40c75f471e3d9 |
|
MD5 | 85b6546dbbb84d9232554cf6073a20ea |
|
BLAKE2b-256 | 66c1440edd9f94c1e5a5860bc2acac28fd45a3f349c9281fc09baa68918e915f |
Hashes for upolygon-0.1-cp36-cp36m-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 19201b341ffacebeee46c43aca1b06881d727f050fb16e3854ee483f99a0af0b |
|
MD5 | e835f76053cc6c82705abfc97f99e08a |
|
BLAKE2b-256 | 98dc42b4888af49eb370ad5c401ac891e308a185fc3e290b273184c0920ebd38 |