Re: rasterio.features.shapes with holes in polygons


wsdaphne.wang@...
 

Hi,

I am encountering a similar problem with getting polygons with holes from a raster image. Hoping someone can help.
The image data that I am working with is a multi-page label matrix in tiff format, I am working with the first page. I have attached it for reprex. My data has no crs.

from skimage import io
import numpy as np
from rasterio import features
import shapely
import geopandas as gpd
import matplotlib.pyplot as plt

# read the first page of the multipage tiff
img = io.imread(seg_map_path, img_num=0)
img_array = np.array(img).astype('int32')

# display the label matrix; value=0 is displayed as purple; (0,0) is in the top left corner
plt.imshow(img_array)
plt.show()


# attempt to get the multipolygon for value=0
(purple region)
mask = (img_array == 0)
polygons = []
for shape, value in features.shapes(img_array, mask):
    polygons.append(shapely.geometry.shape(shape))
polygons = shapely.geometry.MultiPolygon(polygons)
# polygons = polygons.apply(make_valid)
polygons = gpd.GeoSeries(polygons)
polygons.plot()

The resulting geoseries is from the purple geometry from above, reflected across the y axis (because (0,0) is now in the lower left corner), but with no holes.

I would really appreciate any help on this.

Join main@rasterio.groups.io to automatically receive all group messages.