I would like to calculate an area of the intersection of a shapely polygon, and a mask (numpy.array):
plt.plot(*building.exterior.xy, color='brown', linewidth=4)
plt.plot(*parcelle.exterior.xy, color='orange', linewidth=4)
plt.imshow(thr, origin='upper', extent=[x_left, x_right, y_bottom, y_top], cmap='Greys')
I would like to be able to calculate the green area below :
For this, I tried using the method described here : How to transform contours obtained from OpenCV to SHP file polygons?
mask_to_polygons(np.flip(thr, 0))
plt.plot(*parcelle.exterior.xy)
plt.plot(*building.exterior.xy)
mask_pol = translate(mask_to_polygons(np.flip(thr, 0)), x_left, y_bottom)
for geo in mask_pol:
plt.plot(*geo.exterior.xy, color = 'green')
However, the result is not satisfactory :
Indeed, the bounds of the MultiPolygon mask_pol are not the dimensions of the thr array :
> mask_pol.bounds
(0.0, 0.0, 511.0, 511.0)
> thr.shape
(256, 256)
So, I'm wondering if the option to switch from mask to multipolynom shapely is the right one. I could also try to switch from shapely polynomials to masks. Are there better methods to accomplish this task?