1

I have x and y numpy matrixes of coordinates. .shape of each is (1000,1200). x and y have been created in fact with np.meshgrid(). I also created z, a numpy matrix of zeros same size as the fore-mentioned x,y. I have a polygon defined by its vertices. What's the fastest way to substitute in z the zeros inside that polygon to ones values?

I tried matplotlib.path and Shapely functions, but all solutions seem to take too long. Can you provide me with a quick implementation that can easily handle [1000x1200] matrixes?

giosans
  • 1,136
  • 1
  • 12
  • 30
  • You could try the [ray casting](https://en.wikipedia.org/wiki/Point_in_polygon) algorithm from [Rosettacode](https://rosettacode.org/wiki/Ray-casting_algorithm#Python). – Chris Mueller Aug 11 '16 at 12:07
  • You could use this [`post`](http://stackoverflow.com/questions/38674027/find-the-row-indexes-of-several-values-in-a-numpy-array) to get the row indices whose `z's` are to be updated. – Divakar Aug 11 '16 at 12:19
  • hi what should I do if my points in polygons are of float type – Dexter Jan 22 '18 at 09:48
  • @MeghaRawat Can't remember exactly this, but points in polygons should be of float type as well. In that case you would substitute `0.` to the to-be-masked points, if that it is still your goal... Or are you referring to a specific algorithm in particular? – giosans Jan 23 '18 at 10:33
  • my scenario is I have used labelme annotation tool to annotate my images and those annotations are ploygons which are in float type and I have use polygon_to_mask() function from here (https://pypkg.com/pypi/labelme/f/labelme/utils.py) to convert my annotations to nparray but it is returning me (0.) in whole array. – Dexter Jan 23 '18 at 11:22
  • 1
    I think you can convert it to mask or logical zeroes, as in the `def polygons_to_mask` of the `utils.py` function you refer to. If that is not enough, then I would suggest to public a new question out of your post, perhaps with more context, code snippet and, above all, YOUR GOAL. Good luck – giosans Jan 23 '18 at 13:01
  • @giosans https://github.com/cocodataset/cocoapi/issues/111 any suggestions? thank you – Dexter Jan 30 '18 at 05:50

0 Answers0