【问题标题】:Python GDAL: Count the number of pixels of the raster image using a range of pixel valuesPython GDAL:使用一系列像素值计算光栅图像的像素数
【发布时间】:2022-01-12 17:52:17
【问题描述】:

我有一些光栅文件,我想计算每个文件中像素值落在某个特定范围之间的像素数。我尝试了以下代码,虽然它运行但产生了错误的像素数。

代码:

import os, sys
import numpy as np
import gdalnumeric
path = ('dir')
os.chdir(path)
for rasters in os.listdir(path):
    raster_file = gdalnumeric.LoadFile(rasters)
    pixel_count = (raster_file =>0 & <= 25).sum()
    print(pixel_count)

【问题讨论】:

    标签: python count range pixel gdal


    【解决方案1】:

    你的语法有点不对劲——我不认为它可以编译,但是有

    ((raster_file >= 0) & (raster_file <= 25)).sum()
    

    你应该得到正确的结果

    【讨论】:

    • 不,它确实运行但输出错误。这里要提一下的是,当我使用等号时,代码给出了正确的结果,例如`(raster_file == 25)`,但是当我使用范围时,它提供了错误的结果。
    • 数据类型是什么?如果你的代码确实运行了,你没有正确复制它,这个函数有语法错误
    • 这是一个 tiff 格式的光栅文件。我能够找出问题所在。我更改了文件的no-data 值,不幸的是该值设置为0。现在结果很完美。感谢您的回复@mmomtchev
    猜你喜欢
    • 2018-11-30
    • 2013-08-21
    • 1970-01-01
    • 2022-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多