import cv2
import numpy as np
from osgeo import gdal
kSize = 31
g_sigma = 3.0
g_theta = np.pi / 4
g_lambda = 10.0
g_gamma = 0.5
g_psi = np.pi / 2
kernel = cv2.getGaborKernel((kSize, kSize), g_sigma, g_theta, g_lambda, g_gamma, g_psi, ktype=cv2.CV_32F)
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]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