Created
December 22, 2017 19:41
-
-
Save krosaen/f8e81a7dd0d932eec6ef57a12cdfb43e to your computer and use it in GitHub Desktop.
how to count number of points within a polygon
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def count_kps_within_poly(polygon, keypoints): | |
from matplotlib.collections import PolyCollection | |
path = PolyCollection([polygon]).get_paths()[0] | |
return np.sum(path.contains_points(keypoints)) | |
assert 2 == count_kps_within_poly( | |
[(0, 0), (100, 0), (200, 100), (100, 100), (0, 100)], | |
[(50, 50), (200, 0), (1, 1)] | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment