我正在尝试通过覆盖多边形来提取像素值。我使用了Patrick Gray(http://patrickgray.me/open-geo-tutorial/chapter_5_classification.html)中的代码。当我用形状特征遮盖图像时,我想得到out_image
。然后,下一步将是删除0,这将完全弄乱数组,因为根据频段不存在值。我尝试了多种方法来删除0并根据类保持band值的顺序。在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实际上不是shapefile的一部分:
out_image_trimmed = out_image[:,~np.all(out_image == 0, axis=0)]
消除所有8个波段的255个值的所有像素-AKA实际上不是shapefile的一部分:
out_image_trimmed = out_image_trimmed[:,~np.all(out_image_trimmed == 255, axis=0)]