本文主要是介绍GDAl 之绘制栅格图像的大致直方图和精准直方图(8),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
gdal的绘制大致直方图是仅查看概览或者抽样像素的一个子集
import os
from osgeo import gdal
import matplotlib.pyplot as plt
import numpy as np# Don't forget to change directory.
os.chdir(r'D:\DeskTop\learn_py_must\Learn_GDAL\osgeopy-data\osgeopy-data\Switzerland')ds = gdal.Open('dem_class2.tif')
band = ds.GetRasterBand(1)# 获取直方图数据
approximate_hist = band.GetHistogram()
exact_hist = band.GetHistogram(approx_ok=False)
for i in range(len(approximate_hist[1:6])):print(i)print(approximate_hist[i+1:i+2])print(exact_hist[i+1:i+2])x = np.arange(len((approximate_hist[1:6]))) # the label locations
width = 0.35 # the width of the barsfig, ax = plt.subplots()
rects1 = ax.bar(x - width/2-0.05, approximate_hist[1:6], width, label='Approximate Histogram')
rects2 = ax.bar(x + width/2+0.05, exact_hist[1:6], width, label='Exact Histogram')plt.xlabel('Pixel Value')
plt.ylabel('Frequency')
plt.title('Raster Histogram')
plt.legend()
plt.show()# # 将数据转换为列表以便绘制
# bins = [i+1 for i in range(len(approximate_hist[1:6]))]
# plt.figure(figsize=(10, 5))
# plt.bar(bins, approximate_hist[1:6], alpha=0.5, color='b', label='Approximate Histogram')
# plt.bar(bins, exact_hist[1:6], alpha=0.5, color='r', label='Exact Histogram')
#
# plt.xlabel('Pixel Value')
# plt.ylabel('Frequency')
# plt.title('Raster Histogram')
# plt.legend()
# plt.show()
结果
这篇关于GDAl 之绘制栅格图像的大致直方图和精准直方图(8)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!