gadl python 遥感影像直方图均衡化
时间: 2024-01-25 18:13:14 浏览: 151
遥感影像直方图均衡化是一种用于增强遥感影像对比度的方法。下面是使用Python和GDAL库进行遥感影像直方图均衡化的示例代码:
```python
from osgeo import gdal
import numpy as np
def histogram_equalization(image_path):
# 打开遥感影像
dataset = gdal.Open(image_path)
if dataset is None:
print("无法打开影像文件")
return
# 读取影像数据
band = dataset.GetRasterBand(1)
image_array = band.ReadAsArray()
# 计算直方图
hist, bins = np.histogram(image_array.flatten(), bins=256, range=[0, 255])
# 计算累积分布函数
cdf = hist.cumsum()
cdf_normalized = cdf * hist.max() / cdf.max()
# 对影像进行直方图均衡化
equalized_image_array = np.interp(image_array.flatten(), bins[:-1], cdf_normalized).reshape(image_array.shape)
# 创建输出影像
driver = gdal.GetDriverByName("GTiff")
equalized_image_path = "equalized_image.tif"
equalized_image = driver.Create(equalized_image_path, dataset.RasterXSize, dataset.RasterYSize, 1, gdal.GDT_UInt16)
equalized_image.SetGeoTransform(dataset.GetGeoTransform())
equalized_image.SetProjection(dataset.GetProjection())
equalized_image.GetRasterBand(1).WriteArray(equalized_image_array)
# 释放资源
dataset = None
equalized_image = None
print("直方图均衡化完成,结果保存在", equalized_image_path)
# 调用函数进行直方图均衡化
image_path = "input_image.tif"
histogram_equalization(image_path)
```
请注意,上述代码中的`image_path`需要替换为你实际的遥感影像文件路径。此代码将读取遥感影像,计算其直方图,并对影像进行直方图均衡化。最后,它将保存均衡化后的影像为一个新的GeoTIFF文件。
阅读全文