lee滤波对于sar影像去噪非常有用

正如之前我说的,遥感影像在深度学习的输入形式一般是RGB格式,而遥感影像的原始数据一般由多个波段组合。

所以,我们需要做的一个步骤的:把多波段的遥感影像转换为RGB遥感影像。

RGB,三个字母说明了这个文件由三个矩阵组成,它们的顺序是RED、GREEN、BLUE。也叫做红绿蓝波段。

以国产遥感影像高分一号为例,GF1的原始数据的波段顺序是BLUE、GREEN、RED、NIR。即蓝、绿、红、近红外。

所以把多波段的遥感影像转换为RGB遥感影像的原理很简单,我们只需要保留前三个波段,并且把1、3波段调换顺序即可完成此步骤。

image-20250624142111928

具体的python代码如下:

from osgeo import gdal
import os


def convert_4band_to_rgb(input_tif, output_tif):
    """
    Convert a 4-band (RGB+NIR) TIFF to a 3-band RGB TIFF.

    Args:
        input_tif (str): Path to the input 4-band TIFF file.
        output_tif (str): Path to the output 3-band RGB TIFF file.

    Returns:
        bool: True if conversion is successful, False otherwise.
    """
    try:
        os.makedirs(os.path.dirname(output_tif), exist_ok=True)
        # Open the input TIFF file
        dataset = gdal.Open(input_tif, gdal.GA_ReadOnly)
        if dataset is None:
            print(f"Error: Could not open input file {input_tif}")
            return False

        # Check if the input has at least 4 bands
        if dataset.RasterCount < 4:
            print(f"Error: Input file has {dataset.RasterCount} bands, expected at least 4")
            return False

        # Get raster dimensions
        width = dataset.RasterXSize
        height = dataset.RasterYSize

        # Read the first three bands (RGB)
        band1 = dataset.GetRasterBand(1).ReadAsArray()  # Red
        band2 = dataset.GetRasterBand(2).ReadAsArray()  # Green
        band3 = dataset.GetRasterBand(3).ReadAsArray()  # Blue

        # Get the data type from the first band
        data_type = dataset.GetRasterBand(1).DataType

        # Create output TIFF driver
        driver = gdal.GetDriverByName("GTiff")
        out_dataset = driver.Create(
            output_tif,
            width,
            height,
            3,  # 3 bands for RGB
            data_type,
            options=["BIGTIFF=YES"]  # Optional compression
        )

        if out_dataset is None:
            print(f"Error: Could not create output file {output_tif}")
            return False

        # Copy georeferencing and projection
        out_dataset.SetGeoTransform(dataset.GetGeoTransform())
        out_dataset.SetProjection(dataset.GetProjection())

        # Write the RGB bands to the output file
        out_dataset.GetRasterBand(3).WriteArray(band1)  # Red
        out_dataset.GetRasterBand(2).WriteArray(band2)  # Green
        out_dataset.GetRasterBand(1).WriteArray(band3)  # Blue

        # Set no-data value if present in input
        for i in range(1, 4):
            nodata = dataset.GetRasterBand(i).GetNoDataValue()
            if nodata is not None:
                out_dataset.GetRasterBand(i).SetNoDataValue(nodata)

        # Flush data and close the output file
        out_dataset.FlushCache()
        out_dataset = None
        dataset = None

        print(f"Successfully converted {input_tif} to {output_tif}")
        return True

    except Exception as e:
        print(f"Error during conversion: {str(e)}")
        return False

if __name__ == "__main__":
    input_file = r"D:\GF2_PMS1-PAN1.tif"
    output_file = r"D:\GF2_PMS1-PAN1.tif_rgb.tif"
    convert_4band_to_rgb(input_file, output_file)

怎么判断是否转换成功

首先,可以成功拖拽到acrgis显示。

其次,我们可以观察地物的颜色是否正常。一般来说,屋顶的颜色大多数是蓝色,尤其是那些较大的建筑物(工厂、仓库)

image-20250624142506807