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.2-cp38-cp38-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 724b28064365ee28d3f17bb6464cd9a91b25e867cac6bac66fefb9f112fbc228 |
|
MD5 | 5a3ecbfeb25f90c9f6cd42eb13d11bcb |
|
BLAKE2b-256 | 419278aad992c67b2085a90295ac83d14fe72d9a04e652ed7eae2b18e8678a34 |
Hashes for upolygon-0.1.2-cp38-cp38-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 02d7130b510395dd5e269d4ad6d17219131d2ec787a396472a57268cc9f2e72a |
|
MD5 | fc70104734ef4e16dd9b0436f036b0ca |
|
BLAKE2b-256 | 228b3639a4c95103aeadeb0be369f65b68a2b1b808347c1e60a41ddf31aaa7c5 |
Hashes for upolygon-0.1.2-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 416f987c95962e9de85feffa2ee6ffe32408dd172c24bc2e13ed1be7400d7ce1 |
|
MD5 | 857b4cdc06823dcba061b05fd0b36bbc |
|
BLAKE2b-256 | c7a9120d55aca19c93e921ba1cbdbf7995c949d1ae9f693116abfb62a2b1dc8d |
Hashes for upolygon-0.1.2-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a59ea4606e8ea296cce300eb9006ffcba93f476826c6da7229f49f011d08ff64 |
|
MD5 | 1162fdf9e8d03fd8e2b985d8875b6895 |
|
BLAKE2b-256 | 983ae2d44cd371bf9935b811ff445d462e341a696b1b1ee52b6e2a9b29051f5c |
Hashes for upolygon-0.1.2-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 59ea2abf63c82e90f41a6464e05db884085aa16870544b89823cc12033069f60 |
|
MD5 | c359aec62250a4034a59f85a34a408bf |
|
BLAKE2b-256 | dbc415c78d4244550d6cf04606639d86539a12d1cd1e1ecdacf709dac2a469a4 |
Hashes for upolygon-0.1.2-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0f7e0063eaa3a73fa803fb122cdc6d48470d15942d0931f6b0ef2397914e3bab |
|
MD5 | db47005a50034b62787f307b3fe1c5a1 |
|
BLAKE2b-256 | 6da8130ea0c608b2811ea614841a186ff24aba9e02c627bff1d1bf76e51bc620 |
Hashes for upolygon-0.1.2-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c90afc4b50426bba880625600a625dbb6707771ec96c46802970eaf0b33ffc61 |
|
MD5 | b3bb1ac3c893817b2f4da251c69a369a |
|
BLAKE2b-256 | 1c9527226a8d3b23e1e118f73cd4978ec9bdbe0f37442349190e55954d501996 |
Hashes for upolygon-0.1.2-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cf6d3a49bf058a2a7c928a66a5052ea1043823fe4c36d43273829d0eb640e21b |
|
MD5 | 216e8762357096d56b98615e6c7f1d82 |
|
BLAKE2b-256 | d6324734e4861f43728f851ef23dca95ba46673c3e23c46d8aa98073329adffd |
Hashes for upolygon-0.1.2-cp36-cp36m-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4abb958257255c0c811e973d475ae4a1363f5604801950850858ce3726b20566 |
|
MD5 | 239ea3ac29a91b9258bf4ae196775055 |
|
BLAKE2b-256 | 97d7b52529114682530e7e04e648f6a2bc1e441c19c2c2da5a4128ef2c06dcde |