通过覆盖多边形提取像素值



我正试图通过覆盖多边形来提取像素值。我用的是帕特里克·格雷的密码(http://patrickgray.me/open-geo-tutorial/chapter_5_classification.html)。当我用我想要的形状特征遮蔽图像时,我得到了out_image。然后,下一步将删除0,这会完全打乱数组,因为根据波段不存在值
我尝试了许多不同的方法来删除0并根据类保持带值的顺序。在R中,我可以毫无问题地做到这一点,当我将数据导出为CSV并训练算法时,在Python环境中一切都很好。

我如何提取像素值并保持数字的波段和类别?

X = np.array([], dtype=np.int8).reshape(0,8) # pixels for training
y = np.array([], dtype=np.string_) # labels for training
with rasterio.open(img_fp) as src:
band_count = src.count
for index, geom in enumerate(geoms):
feature = [mapping(geom)]
# the mask function returns an array of the raster pixels within this feature
out_image, out_transform = mask(src, feature, crop=True) 
# eliminate all the pixels with 0 values for all 8 bands - AKA not actually part of the shapefile
out_image_trimmed = out_image[:,~np.all(out_image == 0, axis=0)]
# eliminate all the pixels with 255 values for all 8 bands - AKA not actually part of the shapefile
out_image_trimmed = out_image_trimmed[:,~np.all(out_image_trimmed == 255, axis=0)]
# reshape the array to [pixel count, bands]
out_image_reshaped = out_image_trimmed.reshape(-1, band_count)
# append the labels to the y array
y = np.append(y,[shapefile["Classname"][index]] * out_image_reshaped.shape[0]) 
# stack the pizels onto the pixel array
X = np.vstack((X,out_image_reshaped))        

非常感谢您的提示!

以下是解决方案。我必须按波段对数据进行切片,然后将其转置并按列堆叠。经过这一步np.vstack工作正常,一切正常。

X = np.array([], dtype=np.int8).reshape(0, 9)  # pixels for training
y = np.array([], dtype=np.int8)  # labels for training
# extract the raster values within the polygon 
with rio.open(sentinal_band_paths[7]) as src:
band_count = src.count
for index, geom in enumerate(geoms):
feature = [mapping(geom)]
# the mask function returns an array of the raster pixels within this feature
out_image, out_transform = mask(src, feature, crop=True)
# eliminate all the pixels with 0 values for all 8 bands - AKA not actually part of the shapefile
out_image_trimmed = out_image[:, ~np.all(out_image == 0, axis=0)]
# eliminate all the pixels with 255 values for all 8 bands - AKA not actually part of the shapefile
out_image_trimmed = out_image_trimmed[:, ~np.all(out_image_trimmed == 255, axis=0)]
# reshape the array to [pixel count, bands]
out_image_reshaped = out_image_trimmed.reshape(-1, band_count)
# reshape the array to [pixel count, bands]
trial = np.split(out_image_trimmed, 9)  ##### share it to equally after bands
B1 = trial[0].T  ####transpons columns
B2 = trial[1].T
B3 = trial[2].T
B4 = trial[3].T
B5 = trial[4].T
B6 = trial[5].T
B7 = trial[6].T
B8 = trial[7].T
B9 = trial[8].T
colum_data = np.column_stack((B1, B2, B3, B4, B5, B6, B7, B8, B9))  ####concatenate data colum wise 
# append the labels to the y array
y = np.append(y, [shapefile["id"][index]] * out_image_reshaped.shape[0])
# stack the pizels onto the pixel array
X = np.vstack((X, colum_data))

消除所有8个波段的0值像素-AKA实际上不是形状文件的一部分:

out_image_trimmed = out_image[:,~np.all(out_image == 0, axis=0)]

消除所有8个波段的255个值的所有像素-AKA实际上不是形状文件的一部分:

out_image_trimmed = out_image_trimmed[:,~np.all(out_image_trimmed == 255, axis=0)]

最新更新