【GF6-WFVデータ前処理完了後~Pythonバッチ処理ツール~ソースコード含む】

GF6-WFV データの前処理

1. GF6-WFVデータ - 1回目のクロップ(サブ)

この作品は主に、水域境界 (サブ) を抽出してトリミングし、各シーン画像の水域を抽出し、各シーン画像の水域境界 (shp) を視覚的に解釈する前のステップに基づいています。前処理したデータと水境界線を同じフォルダーに保存し、プログラムを使用して一括クロップ(サブ)を実行します。

Data_Rad_Fla_Rpc_bm-XX-JG-sub.dat——3 比率指数計算結果

ここに画像の説明を挿入

2. Data_Rad_Fla_Rpc_bm-XX-JG-sub.dat - 3 つの比率インデックスの計算結果 - これに基づいて、ヒストグラムを通じてほとんどのデータの最大値を見つけ、除去 (0.1%—1%) を実行します。赤いボックスの内容は、その段落を削除することに似ています。

ここに画像の説明を挿入

Data_Rad_Fla_Rpc_bm-XX-JG-sub-q1.dat——3つの比率指数の計算結果から(0.1%——1%)を除いた結果

ここに画像の説明を挿入

Data_Rad_Fla_Rpc_bm-XX-JG-sub-q1.dat—3つの比率指数の計算結果から(0.1%〜1%)を除いた結果—バックグラウンド値が表示されます

ここに画像の説明を挿入

3. GF6-WFV データ - 2 番目のクリッピング (クリップ)

この作品は主に、水域境界の抽出とクリッピング (クリップ) を実行する前のステップに基づいています。前のステップの処理後もデータが存在するため (黒い背景)、水の境界を視覚的に解釈するために前のシーン イメージを引き続き使用します。 (shp) が 2 回クリップされます。
ここに画像の説明を挿入

Data_Rad_Fla_Rpc_bm-XX-JG-sub-q1.dat—3 つの比率インデックスの計算結果から (0.1%—1%) を除去した結果—バックグラウンド値の外観—この問題を解決するのが二次クリッピングです、いちいち回避しないように 背景値を無視する(背景データを無視する)

ここに画像の説明を挿入

4. GF6-WFV データは 2 回目のクリッピング後に別のモデルに保存されます。

ここに画像の説明を挿入

データの3つの比率指数計算結果のうちの1つ

ここに画像の説明を挿入

ようやくこのような結果が出ました

ここに画像の説明を挿入

上記のコンテンツは Python で実装されています。具体的なコードは次のとおりです。

ここに画像の説明を挿入

import os
import numpy as np
from osgeo import gdal
import sys
import cv2
from tqdm import tqdm

# import ipdb

def cv_to_gdal(filename, img, datatype):
    if len(img.shape) < 3:
        img = img.reshape(img.shape[0], img.shape[1], 1)

    im_data = img.transpose(2, 0, 1)

    im_bands, im_height, im_width = im_data.shape

    band_list = [i + 1 for i in range(im_bands)]
    if im_bands == 3:
        band_list = [4 - i for i in band_list]

    driver = gdal.GetDriverByName('GTiff')
    dataset = driver.Create(filename, im_width, im_height, im_bands, datatype)
    for i in range(im_bands):
        dataset.GetRasterBand(band_list[i]).WriteArray(im_data[i])
    del dataset


def write_gdal(filename, img, datatype, img_geotrans, img_proj, big_tiff=False):
    if len(img.shape) < 3:
        im_bands = 1
        im_height, im_width = img.shape
        img = img.reshape( img.shape[0], img.shape[1],1)
    else:
        im_height, im_width, im_bands = img.shape

    band_list = [i + 1 for i in range(im_bands)]
    if im_bands == 3:
        band_list = [4 - i for i in band_list]

    driver = gdal.GetDriverByName('GTiff')
    big_tiff_str = "YES" if big_tiff == True else "NO"

    dataset = driver.Create(filename, im_width, im_height, im_bands, datatype,
                            options=['BigTIFF={}'.format(big_tiff_str), 'COMPRESS=LZW'])
    # ipdb.set_trace()
    # print(img.shape)
    for i in range(im_bands):
        dataset.GetRasterBand(band_list[i]).WriteArray(img[..., i])

    dataset.SetGeoTransform(img_geotrans)
    dataset.SetProjection(img_proj)
    dataset.BuildOverviews('Nearest', [2, 4, 8, 16, 32, 64, 128])

    del dataset


def read_img(filepath):
    dataset = gdal.Open(filepath)
    if dataset is None:
        print('FATAL: GDAL open file failed. [%s]' % filepath)
        sys.exit(1)
    img_width = dataset.RasterXSize
    img_height = dataset.RasterYSize
    img_nbands = dataset.RasterCount

    img_geotrans = dataset.GetGeoTransform()
    img_proj = dataset.GetProjection()

    # print(img_nbands)

    band_list = [i + 1 for i in range(img_nbands)]
    if img_nbands == 3:
        band_list = [4 - i for i in band_list]

    data_type = gdal.GDT_Byte
    for i in range(img_nbands):
        band = dataset.GetRasterBand(band_list[i])
        data_type = band.DataType
        if data_type == gdal.GDT_Byte:
            img_arr = band.ReadAsArray(0, 0, img_width, img_height).astype(np.uint8)
        elif data_type == gdal.GDT_UInt16:
            img_arr = band.ReadAsArray(0, 0, img_width, img_height).astype(np.uint16)
        elif data_type == gdal.GDT_Int16:
            img_arr = band.ReadAsArray(0, 0, img_width, img_height).astype(np.int16)
        elif data_type == gdal.GDT_UInt32:
            img_arr = band.ReadAsArray(0, 0, img_width, img_height).astype(np.uint32)
        elif data_type == gdal.GDT_Int32:
            img_arr = band.ReadAsArray(0, 0, img_width, img_height).astype(np.int32)
        elif data_type == gdal.GDT_Float32:
            img_arr = band.ReadAsArray(0, 0, img_width, img_height).astype(np.float32)
        elif data_type == gdal.GDT_Float64:
            img_arr = band.ReadAsArray(0, 0, img_width, img_height).astype(np.float64)
        else:
            print('ERROR: GDAL unknown data type. []')

        if i == 0:
            img_array = img_arr.reshape((img_height, img_width, 1))
        else:
            img_arr_reshape = img_arr.reshape((img_height, img_width, 1))
            img_array = np.append(img_array, img_arr_reshape, axis=2)

    return img_array, data_type, img_geotrans, img_proj
if __name__ == '__main__':
    img_path= r"E:\4研究区——20230317备份\test\新建文件夹"
    out_path_med=r"E:\4研究区——20230317备份\test\1-一次裁剪"
    out_path_med_med=r"E:\4研究区——20230317备份\test\2-去除99.9以外150"
    out_path_last= r"E:\4研究区——20230317备份\test\3-二次裁剪"
    number_th=99.5

#20190403_1119863972.shp
    for file in tqdm(os.listdir(img_path)):
        file_name=os.path.join(img_path,file)
        for file_name_name in os.listdir(file_name):
            if file_name_name.endswith(".shp"):
                shp_path=os.path.join(file_name,file_name_name)
            if file_name_name.endswith(".dat"):
                img_path_out_med=os.path.join(out_path_med,file_name_name.replace(".dat","-sub.tif"))
                file_name_name_img=os.path.join(file_name,file_name_name)
                # 第一次裁剪
                ds = gdal.Warp(img_path_out_med, file_name_name_img, format='GTiff', cutlineDSName=shp_path,
                               cropToCutline=True, dstNodata=0)

                #过滤异常值
                img_array, data_type, img_geotrans, img_proj = read_img(img_path_out_med)

                #百分比剔除
                high = np.percentile(img_array, number_th)

                #固定值剔除
                # high = number_th


                img_array[img_array > high] = 0
                out_path_med_med_abs=os.path.join(out_path_med_med,file_name_name.replace("-sub.tif","-sub-tc.tif"))
                write_gdal(out_path_med_med_abs,img_array,data_type,img_geotrans,img_proj)

                #第二次裁剪
                out_path_last_abs=os.path.join(out_path_last,file_name_name.replace("-sub-tc.tif","-sub-tc-clip.tif"))
                ds = gdal.Warp(out_path_last_abs, out_path_med_med_abs, format='GTiff', cutlineDSName=shp_path,
                               cropToCutline=True, dstNodata=0)


Python は本当に使いやすいです。コード全体はクラスメイトの協力を得て作られています。処理時間は大幅に短縮されています。Python —— YYDS、Python はすごいです!

おすすめ

転載: blog.csdn.net/qq_36253366/article/details/129960695