首页 文章

如何将scikit-image find_contours中创建的轮廓导出到shapefile或geojson?

提问于
浏览
3

我试图在卫星图像上运行后将scikit-image.measure.find_contours()函数的结果导出为shapefile或geojson .

输出是一个像(行,列)这样的数组,沿着轮廓有坐标,其中有许多 .

如何绘制各种轮廓的坐标,并将其导出到shapefile(可以设置适当的投影等)?

我当前的代码'mask'是我处理过的图片:

from skimage import measure
import matplotlib.pyplot as plt

contours = measure.find_contours(mask, 0.5)

plt.imshow(mask)
for n, contour in enumerate(contours):
    plt.plot(contour[:,1], contour[:, 0], linewidth=1)

3 回答

  • 0

    rasteriofiona 的主要开发人员改编自post的以下内容应该有效,尽管我需要进一步调整 . 它使用 rasterio.features.shapes 来识别图像中具有某些值的连续区域,并根据栅格的变换返回关联的坐标 . 然后使用 fiona 将这些记录写入shapefile .

    import fiona
    import rasterio.features
    
    schema = {"geometry": "Polygon", "properties": {"value": "int"}}
    
    with rasterio.open(raster_filename) as raster:
        image = raster.read()
        # use your function to generate mask
        mask = your_thresholding_function(image)
        # and convert to uint8 for rasterio.features.shapes
        mask = mask.astype('uint8')
        shapes = rasterio.features.shapes(mask, transform=raster.transform)
        # select the records from shapes where the value is 1,
        # or where the mask was True
        records = [{"geometry": geometry, "properties": {"value": value}}
                   for (geometry, value) in shapes if value == 1]
        with fiona.open(shape_filename, "w", "ESRI Shapefile",
                        crs=raster.crs.data, schema=schema) as out_file:
            out_file.writerecords(records)
    

  • 0

    你应该安装python库 geojson 并使用它 .

    为了使用图像中存在的坐标和标记对象,您应该使用库 shapely .

  • 3

    @Cate你可以使用那些 row, column 坐标矩阵并通过http://scikit-image.org/docs/dev/api/skimage.draw.html#skimage.draw.polygon(填充多边形),http://scikit-image.org/docs/dev/api/skimage.draw.html#skimage.draw.polygon_perimeter(仅周长)绘制它们,或者在http://matplotlib.org/api/patches_api.html#matplotlib.patches.Polygon之上创建自定义多边形绘图功能 .

相关问题