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.8-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 17f35d8a84ad7afd2d0f1988ced9f5e01fa8848ce16097e542944e11f45006c8 |
|
MD5 | 9c68245c25e11bfa0aa3289d0188ebb1 |
|
BLAKE2b-256 | ce40405a117f21095ef4b4b2bd3ea9ca58b8805d38acb338521fcfe5ace05795 |
Hashes for upolygon-0.1.8-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0fc91178f7e6e7b8284d424cbef678f26ceb0818f3cc5badf9c47ac4eb4feff2 |
|
MD5 | 07388a607e0bfa23efe9641ac7f869cf |
|
BLAKE2b-256 | 5472be81bdb4ae7bbed0a61513134d580821994d4e6af8923178cdeae889e357 |
Hashes for upolygon-0.1.8-cp310-cp310-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2053a78c4cb8d5371a3710a5775a425fac95c7daf3db7f9e4b1b6c31266afca8 |
|
MD5 | d3b58826bedc7f15887535dc580c587b |
|
BLAKE2b-256 | 786c1c59d3dd3e1f6a52c866ad46240e6918f03e7b6a37f632e5117eb575b02e |
Hashes for upolygon-0.1.8-cp310-cp310-macosx_10_15_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5fb8e3f4ecdd4ac766c9974c6e603400a148041005d1806eea6bcbfcbd106604 |
|
MD5 | 8fbbc987c814dd56f71f01fc6f7f190e |
|
BLAKE2b-256 | 58523e3c656dd2bf8c1d4701d0554f2f1a256c484cb38afaf0be75e10a2a7a60 |
Hashes for upolygon-0.1.8-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 40de6070aa8b6bedbe0c6384825936423a34236321e0b8cb9441bdcce1bcf5d6 |
|
MD5 | f3c599ea0529b252b4017af1fa7bc88e |
|
BLAKE2b-256 | 090e552e66fad3187cdac46caa10a76bd48992a3fa7c413fe8979279d97dc48e |
Hashes for upolygon-0.1.8-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c47fab057b1f6924941177c0bce12c759974d09ddc2bf9134d4df93fbbae48e4 |
|
MD5 | 6423502130dbcbb94dd3cafdaed78ec7 |
|
BLAKE2b-256 | 23126558da1173450f7a1d0584f734ea02e1ed37d41ead3a2048c04a5d0bfadc |
Hashes for upolygon-0.1.8-cp39-cp39-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b646cb046dda5456efe3226b67929243fd128dc71f44b49642dd17060c67c541 |
|
MD5 | b729c6290aca2fd36d8b6c622195f47f |
|
BLAKE2b-256 | d22eae8edbb2dad3c763230de2a8940571b7b0bd82c2595bb1b7808e3ad61086 |
Hashes for upolygon-0.1.8-cp39-cp39-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d7ceb3e45871234d1988372f5444c63e8e15f7d28f594c41e4419aff96378d73 |
|
MD5 | a9554a0034b05de79a51585551525fe8 |
|
BLAKE2b-256 | 493216cb47a009f3287d4980c63c44e4e93bfaa53b46767578f98c837294fc19 |
Hashes for upolygon-0.1.8-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 76fe9849caf92122365fb49ad73929b8ca9d79a1a07e3a953f1067117e01a330 |
|
MD5 | 48f4778412e9b7ddd4496ac10ef1332c |
|
BLAKE2b-256 | 3b83549959dfb0a480a6a0c22ab6198c96b657a5303b2b664b421466cb71f083 |
Hashes for upolygon-0.1.8-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 012c9bffeaef34ab8130ecc968ecf6aa815129abf105bbea646e1bd7e8cae9f3 |
|
MD5 | 5d9dada67de59dc2ea70180e8f0f048a |
|
BLAKE2b-256 | 4311e1a53dacbd9402c4a7a6ff1ad6b86a87960835def8fd53aee51d02595b64 |
Hashes for upolygon-0.1.8-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b36cb8159edb2d214dcc3430c04dad58e436e69f3e3685baa477db35dffda110 |
|
MD5 | e62bbfa3dfd11d25c8125e3a4aaef219 |
|
BLAKE2b-256 | 18e893efa7f18a401965456433c9da255c2ee1901c79a573ed9210abe7a35b1e |
Hashes for upolygon-0.1.8-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0017a176b63ad536d704d8d0041eca3230b76b875da528aae6c950efa49ba530 |
|
MD5 | c84f9e0c3f3c6a7136b5a13af24ec61d |
|
BLAKE2b-256 | 458944a4abb510bbce418ed23a6082f76ff217114e46b3672c5746d9c5b1ce6e |
Hashes for upolygon-0.1.8-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d2dea16ae6b3e5548b0f1b794843c32586e2831c7876fa5f278bb17e44e20e61 |
|
MD5 | 3931cd836122d53c20843e125491191b |
|
BLAKE2b-256 | 106db6c8d33869edeb65b0bf04ff411ac613a8eed0ac0352ce8fbbe470f78812 |
Hashes for upolygon-0.1.8-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7c9c3be4915d3310df34d48fb8c7b1cc4409099e82a32554db32fbd7c1f2931e |
|
MD5 | 2140fa3dd9e4dd03ae865edc8adc170f |
|
BLAKE2b-256 | d00ad5cd2036cad4ea69acdfcc805e78f0d315a8e202e5a1e160ef710028af10 |
Hashes for upolygon-0.1.8-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3cd230dc82a2f3467a6c2a46c621a7bb3186e68e862b096d4265e45585801964 |
|
MD5 | ebbea507ff532fbb3484e4855a1fffef |
|
BLAKE2b-256 | 6a6400e8ea06f1a3ced60f116249e37a7b0ef3915c600644da8589ff0aa186dd |