标签:contour scikit-image shapefile arrays python
我正在尝试在卫星图像上运行后将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)
解决方法:
由rasterio和fiona的主要开发人员改编为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)
标签:contour,scikit-image,shapefile,arrays,python 来源: https://codeday.me/bug/20191111/2022949.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。