首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()?

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()?

提问于 2023-12-27 21:50:01
回答 1关注 0查看 102

我正在编写代码生成进行直方图统计,遇到了这个问题,想请问一下?

代码语言:javascript
复制
import os
from osgeo import gdal
import numpy as np
import matplotlib.pyplot as plt

# 通过GDAL读取栅格影像
os.chdir(r'D:\\Homework\遥感图像课程设计\示例数据\Landsat')
filename = 'nat_color.tif'
dataset = gdal.Open(filename)
im_width = dataset.RasterXSize
im_height = dataset.RasterYSize
im_data = dataset.ReadAsArray(0, 0, im_width, im_height)
print(im_data.shape)

###########显示灰度直方图########
# 遍历影像中的每一个像元的像元值
data = []
for i in range(im_data.shape[0]):
   for j in range(im_data.shape[1]):
      # print(j)
      data.append(im_data[i][j])
data.sort()

# 统计最大最小值
data = np.array(data)
print(data.min(), data.max())

# 由于空值通常用-3.4028235e+38表达,避免错误统计需要提前剔除
a = sum(data == 0)
print(a)
print(data.shape)
# 剔除小于0的异常值
data = data[a:]
# 根据影像中最大最小值设定坐标轴
bins = np.linspace(10, 100, 100)
# 绘制直方图,设定直方图颜色
plt.hist(data, bins, facecolor="blue")
# 横坐标轴名称
plt.xlabel('像元值')
# 纵坐标轴名称
plt.ylabel('频数')
# 图表头名称
plt.title('灰度分布直方图')
# 显示中文字体
plt.rcParams['font.sans-serif'] = ['Source Han Sans CN']
# 导出绘制得到的图片
plt.savefig('./test2.jpg')
plt.show()
band1_data = im_data
相关文章

相似问题

相关问答用户
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档