【发布时间】:2022-01-17 01:43:37
【问题描述】:
我有一个巨大的 2D numpy 数组,其中填充了整数值。我通过 gdal.GetRasterBand() 从 .tif-image 中收集它们。 图像的像素值代表唯一的集群识别号。所以一个簇内的所有像素都具有相同的值。 在我的脚本中,我想检查集群的像素是否超过特定阈值。如果簇大小大于阈值,我想保留簇并给它们一个像素值 1。如果簇的像素小于阈值,则该簇的所有像素都应为 0。
到目前为止,我的代码可以正常工作,但速度非常慢。而且因为我想改变阈值,所以它需要永远。 我将衷心感谢您的帮助。谢谢。
# Import GeoTIFF via GDAL and convert to NumpyArray
data = gdal.Open(image)
raster = data.GetRasterBand(1)
raster = raster.ReadAsArray()
# Different thresholds for iteration
thresh = [0,10,25,50,100,1000,2000]
for threshold in thresh:
clusteredRaster = np.array(raster.copy(), dtype = int)
for clump in np.unique(clusteredRaster): # Unique ids of the clusters in image
if clusteredRaster[np.where(clusteredRaster == clump)].size >= threshold:
clusteredRaster[np.where(clusteredRaster == clump)] = int(1)
else:
clusteredRaster[np.where(clusteredRaster == clump)] = int(0)
'''
[ClusterImage][1]
In the image you can see the cluster image. Each color stands vor a specific clusternumber. I want to delete the small ones (under a specific size) and just keep the big ones.
[1]: https://i.stack.imgur.com/miEKg.png
【问题讨论】:
-
可以将
np.unique(clusteredRaster)移出threshold循环吗? -
很遗憾不是,因为我使用不同的图像,并且每张图像的唯一值都不同
标签: python numpy cluster-computing gdal