python应用于遥感图像的拼接(包含向最终图像添加坐标)

图像数量:7000张,图像格式:Geotiff。软件:QGIS,pycharm。依赖项:os,gdal,sys,glob。
项目描述:原图格式为tif,其中左下角对应的坐标信息储存于同名txt文件中,故需在.txt文件中获取坐标信息,然后添加到同名图像中,然后每幅图像依据坐标大小,拼接整幅图像大小。
文件目录:先建立项目文件夹:picture,在下级目录中建立process_picture01文件夹,将原始图像和.txt文件放入process_picture01中,最终图像保存于process_picture01中,最终图像的压缩版保存于picture文件夹下。(ps:代码写的较早,发的较晚。有些解释不到位,可以讨论,谢谢理解。
step01:

# Wordgebei 2020.10.23 处理初始图像,向每幅图像中添加坐标信息
import os, sys, gdal
import glob

def pre_process():
    os.chdir('.\process_picture01')
    in_files = glob.glob('*.gp4')
    for fn in in_files[0:]:
        (path, filename) = os.path.split(fn)
        ds1 = gdal.Open(fn)
        data1 = ds1.GetRasterBand(1).ReadAsArray()
        driver = gdal.GetDriverByName("GTiff")
        tods = driver.Create(filename, 5000, 4000, 1, gdal.GDT_Float32)
        bandOut = tods.GetRasterBand(1)
        bandOut.WriteArray(data1 ^ 1)
        filename  = filename[:-4]
        # print(filename)
        str = ".txt"
        filename01 = filename + str
        print(filename)
        with open(filename01, "r") as f:
            data = f.readlines()
            str = data[0]
            a = str.split()
            b = a[0].strip(",")
            x = int(float(b))
            y = int(float(a[1])) + 800
            # print(x, y)

        geotransform = [x, 0.2, 0, y, 0, -0.2]
        tods.SetGeoTransform(geotransform)
        tods.SetProjection(ds1.GetProjection())
        transform1 = tods.GetGeoTransform()
        # print(transform1)
        # strs01 = ".gp4"
        # filename02 = filename + strs01
        # filename03 = filename + "_result" +strs01
        # compress(filename02, filename03)
        # source_path = filename + ".gp4"
        # target_path = filename + ".gp4"
        # print(source_path ,target_path)
        # compress(source_path, target_path)
pre_process()
print("完成!")

step02:

# from common import gdalfun 把每幅图像格式改为tiff并压缩
import os
from osgeo import gdal
import glob

def get_file_size(file_path):
    """获取文件占空间所少M"""
    fsize = os.path.getsize(file_path)
    fsize = fsize / float(1024 * 1024)
    return round(fsize, 2)

def progress(percent, msg, tag):
    """进度回调函数"""
    print(percent, msg, tag)

def compress(path, target_path):
    """使用gdal进行文件压缩"""
    dataset = gdal.Open(path)
    driver = gdal.GetDriverByName('GTiff')
    driver.CreateCopy(target_path, dataset, strict=1, callback=progress, options=["TILED=YES", "COMPRESS=LZW"])
    # strict=1表示和原来的影像严格一致,0表示可以有所调整
    # callback为进度回调函数
    # PACKBITS快速无损压缩,基于流
    # LZW针对像素点,黑白图像效果好
    del dataset

os.chdir('.\process_picture01')
in_files = glob.glob('*.gp4')
for fn in in_files[0:]:
    (path, filename) = os.path.split(fn)
    print(filename)
    filename01 = filename[:-4]
    print(filename01)
    str = ".tif"
    filename02 = filename01 + str
    print(filename02)
    compress(filename, filename02)
print("完成!")

step03:

# Wordgebei 2020.10.23 依据每幅图像的坐标值,把每幅图像填充至最终图像中
import os, sys, gdal
import glob
# 执行后处理
def get_extent(fn):
    ds = gdal.Open(fn)
    rows = ds.RasterYSize
    cols = ds.RasterXSize
    # 获取图像角点坐标
    gt = ds.GetGeoTransform()
    minx = gt[0]
    maxy = gt[3]
    maxx = gt[0] + gt[1] * cols
    miny = gt[3] + gt[5] * rows
    return (minx, maxy, maxx, miny)

os.chdir('.\process_picture01')
in_files = glob.glob('*.tif')

minX, maxY, maxX, minY = get_extent(in_files[0])
print(minX, maxY, maxX, minY)

for fn in in_files[1:]:
    minx, maxy, maxx, miny = get_extent(fn)
    minX = min(minX, minx)
    maxY = max(maxY, maxy)
    maxX = max(maxX, maxx)
    minY = min(minY, miny)
    print(minX,maxY,maxX,minY)

in_ds = gdal.Open(in_files[0])
gt = in_ds.GetGeoTransform()
rows = int(maxX - minX) / abs(gt[5])
cols = int(maxY - minY) / gt[1]

driver = gdal.GetDriverByName('gtiff')
out_ds = driver.Create("./picture_result.tif", int(rows) , int(cols), 1, gdal.GDT_Float32)
out_ds.SetProjection(in_ds.GetProjection())
out_band = out_ds.GetRasterBand(1)

gt = list(in_ds.GetGeoTransform())
gt[0], gt[3] = minX, maxY
out_ds.SetGeoTransform(gt)

for fn in in_files:
    in_ds = gdal.Open(fn)
    trans = gdal.Transformer(in_ds, out_ds, [])
    success, xyz = trans.TransformPoint(False, 0, 0)
    x, y, z = map(int, xyz)
    # transform = int(fn.GetGeoTransform())
    # x = transform[0] - minX
    # y = maxY - transform[3]
    data = in_ds.GetRasterBand(1).ReadAsArray()
    out_band.WriteArray(data, x, y)

del in_ds, out_band, out_ds
print("完成!")

step04:

# from common import gdalfun 压缩最终图像
import os
from osgeo import gdal

def get_file_size(file_path):
    """获取文件占空间所少M"""
    fsize = os.path.getsize(file_path)
    fsize = fsize / float(1024 * 1024)
    return round(fsize, 2)

def progress(percent, msg, tag):
    """进度回调函数"""
    print(percent, msg, tag)

def compress(path, target_path):
    """使用gdal进行文件压缩"""
    dataset = gdal.Open(path)
    driver = gdal.GetDriverByName('GTiff')
    driver.CreateCopy(target_path, dataset, strict=1, callback=progress, options=["TILED=YES", "COMPRESS=LZW"])
    # strict=1表示和原来的影像严格一致,0表示可以有所调整
    # callback为进度回调函数
    # PACKBITS快速无损压缩,基于流
    # LZW针对像素点,黑白图像效果好
    del dataset

source_path = "./process_picture01/picture_result.tif"
target_path = "./result.tif"
print("处理前", str(get_file_size(source_path)) + "MB")
compress(source_path, target_path)
print("处理后", str(get_file_size(target_path)) + "MB")
print("完成!")

step05:

# 验证坐标是否正确写入,读取最终图像的坐标
from osgeo import gdal
#
# ds1 = gdal.Open('./process_picture01/1-3-11-11.gp4', gdal.GDT_CFloat32)
# transform1 = ds1.GetGeoTransform()
# print(transform1)
#
# ds2 = gdal.Open('./process_picture01/1-3-11-12.gp4', gdal.GDT_CFloat32)
# transform1 = ds2.GetGeoTransform()
# print(transform1)
# #
# ds3 = gdal.Open('./process_picture01/1-3-11-16.gp4', gdal.GDT_CFloat32)
# transform1 = ds3.GetGeoTransform()
# print(transform1)
#
# ds4 = gdal.Open('./process_picture01/1-3-11-17.gp4', gdal.GDT_CFloat32)
# transform1 = ds4.GetGeoTransform()
# print(transform1)

ds4 = gdal.Open('./result.tif', gdal.GDT_CFloat32)
transform1 = ds4.GetGeoTransform()
print(transform1)
print("完成!")

完毕。

你可能感兴趣的:(python图像处理,python,pycharm)