【问题标题】:Extracting pixel values by overlaying polygons通过叠加多边形提取像素值
【发布时间】:2020-04-14 06:45:59
【问题描述】:

我正在尝试通过叠加多边形来提取像素值。我使用来自 Patrick Gray (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))        

非常感谢您的任何提示!

【问题讨论】:

  • 欢迎来到SO;问题实际上与machine-learning 无关 - 请不要向不相关的标签发送垃圾邮件(已删除)。

标签: python numpy gis rasterio data-masking


【解决方案1】:

这里是解决方案。我必须明智地分割数据带,然后将其转置并按列堆叠。在这一步之后 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))

【讨论】:

    【解决方案2】:

    消除所有 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)]
    

    【讨论】:

    • 欢迎来到 SO;请花一点时间看看如何正确格式化您的代码(现在为您完成)。另外,请不要发布与答案无关的外部链接(已删除)。
    猜你喜欢
    • 2021-10-26
    • 2020-04-09
    • 1970-01-01
    • 1970-01-01
    • 2013-03-02
    • 1970-01-01
    • 1970-01-01
    • 2016-09-02
    • 1970-01-01
    相关资源
    最近更新 更多