python
复制代码
import cv2
import numpy as np
from osgeo import gdal
# 定义 Gabor 滤波器的参数
kSize = 31 # 滤波器核的大小
g_sigma = 3.0 # 高斯包络的标准差
g_theta = np.pi / 4 # Gabor 函数的方向
g_lambda = 10.0 # 正弦波的波长
g_gamma = 0.5 # 空间纵横比
g_psi = np.pi / 2 # 相位偏移
# 生成 Gabor 滤波器核
kernel = cv2.getGaborKernel((kSize, kSize), g_sigma, g_theta, g_lambda, g_gamma, g_psi, ktype=cv2.CV_32F)
# 使用gdal读取遥感图像
dataset = gdal.Open("1.tif")
image = dataset.ReadAsArray().transpose((1, 2, 0)) # 将波段维度转置到最后
# 获取图像的波段数
num_bands = image.shape[2]
# 初始化处理后的多波段图像
filtered_image = np.zeros_like(image, dtype=np.float32)
# 遍历每个波段
for band in range(num_bands):
# 提取当前波段
band_image = image[:, :, band]
# 应用 Gabor 滤波器
filtered_band_image = cv2.filter2D(band_image, cv2.CV_32F, kernel)
# 将处理后的波段放回结果图像中
filtered_image[:, :, band] = filtered_band_image
# 将处理后的图像转换为合适的数据类型
filtered_image = np.clip(filtered_image, 0, 255).astype(np.uint8)
# 保存结果
driver = gdal.GetDriverByName('GTiff')
out_dataset = driver.Create('gaofen2_image.tif', dataset.RasterXSize, dataset.RasterYSize, num_bands, gdal.GDT_Byte)
out_dataset.SetProjection(dataset.GetProjection())
out_dataset.SetGeoTransform(dataset.GetGeoTransform())
for band in range(num_bands):
out_band = out_dataset.GetRasterBand(band + 1)
out_band.WriteArray(filtered_image[:, :, band])
out_dataset.FlushCache()
# 关闭数据集
dataset = None
out_dataset = None