I need to create a numpy array that is that will indicate a filled polygon with 1s and outside is 0s. Before you say its a duplicate question. I have already checked many other questions and answers. finally got this to work with one of my cases. but when changing the 4 vertices that function is not working. i have tried to change many things in it and nothing seems to work and i simply cant understand what is the difference and why this cant work.
first i worked with
c = [[47, 187], [55, 47], [184, 186], [186, 46]]
in the first case i had some errors in the beginning. the order of the points mattered. after adding the following codes, it was working fine. here x
and y
are half of lenX
and lenY
.
t = []
for i in range(4):
for j in c:
if j[0]<x and j[1]<y:
t.append(j)
c.pop(c.index(j))
break
for j in c:
if j[0]<x and j[1]>y:
t.append(j)
c.pop(c.index(j))
break
for j in c:
if j[0]>x and j[1]>y:
t.append(j)
c.pop(c.index(j))
break
for j in c:
t.append(j)
c.pop(c.index(j))
break
c= np.array(t)
called the function like this where lenX
and lenY
are always around 200 and points are within the range.
pol = create_polygon([lenX,lenY],c)
in the second case my vertices are
c = [[96, 208],[97, 91], [221, 206], [221, 85]]
now this doesn't work and i don't know why. please take a look to see if you can find something i missed.