欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python基于GDAL鑲嵌拼接遙感影像

 更新時間:2023年10月23日 14:42:21   作者:RS迷途小書童  
這篇文章主要介紹了Python基于GDAL鑲嵌拼接遙感影像, 這里有一點需要注意的就是,用這個方法進行鑲嵌拼接操作時,影像有一條明顯的拼接線,不知道是不是我數(shù)據(jù)的問題,你們可以自己嘗試一下,只要修改主函數(shù)中的路徑即可,需要的朋友可以參考下

        沒啥好說的,處理高分辨率影像時,數(shù)據(jù)高達幾十G。用ENVI或者ArcGIS進行影像的拼接時,往往會出現(xiàn)未響應(yīng)的情況。出現(xiàn)未響應(yīng)的話,運氣好等個一晚上可能會動一動,運氣不好就等著強制關(guān)閉重做吧。

        所以搞了一個Python進行拼接操作的代碼,雖然速度不算快,但至少不會未響應(yīng)。同時如果對代碼進行一些改進,還可以進行批量拼接的操作,百利而無一害。

一、導(dǎo)入GDAL庫

from osgeo import gdal

二、查看影像信息

        為了湊字數(shù)的,可以查看影像的投影、寬度、高度、波段數(shù)等信息。不過需要注意的是在ENVI中沒有投影坐標(biāo)系,只有地理坐標(biāo)系是做不了鑲嵌拼接的。

        這個代碼我還不太清楚能不能不要投影坐標(biāo)系進行拼接,你們可以自己試試。但最好還是用包含投影坐標(biāo)系的影像進行拼接。所以在拼接之前就可以用這段代碼先看一看。

def Get_data(filepath):
    ds = gdal.Open(filepath)  # 打開數(shù)據(jù)集dataset
    ds_width = ds.RasterXSize  # 獲取數(shù)據(jù)寬度
    ds_height = ds.RasterYSize  # 獲取數(shù)據(jù)高度
    ds_bands = ds.RasterCount  # 獲取波段數(shù)
    ds_geo = ds.GetGeoTransform()  # 獲取仿射地理變換參數(shù)
    ds_prj = ds.GetProjection()  # 獲取投影信息
    print("影像的寬度為:" + str(ds_width))
    print("影像的高度為:" + str(ds_height))
    print("仿射地理變換參數(shù)為:" + str(ds_geo))
    print("投影坐標(biāo)系為:" + str(ds_prj))
    # data = ds.ReadAsArray(0, 0, ds_width, ds_height)  # 以數(shù)組的形式讀取整個數(shù)據(jù)集

三、鑲嵌模塊

        這里用到了GDAL中的Warp函數(shù),是不是有點熟悉。之前裁剪也是用的這個函數(shù),不得不說這個函數(shù)是真的強大,之前有文章介紹了其中的函數(shù),大家有興趣可以去看下,同時給個贊吧!【Python&RS】GDAL批量裁剪遙感影像/柵格數(shù)據(jù)

        代碼中的srcSRS,dstSRS分別是輸入投影和輸出投影,這里用一樣的就行了。因為我們做的是鑲嵌操作,肯定是不用動原始坐標(biāo)系的。其他的參數(shù)都在代碼中表明了,這里就不介紹了,如果大家有什么問題,可以留言交流。

def Mosaic_GDAL(path_image1, path_image2, path_out):
    """
    :param path_image1: 需要鑲嵌的影像
    :param path_image2: 需要鑲嵌的影像
    :param path_out: 鑲嵌后輸出的影像路徑
    :return: None
    """
    image1 = gdal.Open(path_image1, gdal.GA_ReadOnly)  # 第一幅影像
    input_proj = image1.GetProjection()
    image2 = gdal.Open(path_image2, gdal.GA_ReadOnly)  # 第二幅影像
    options = gdal.WarpOptions(srcSRS=input_proj, dstSRS=input_proj, format='GTiff',
                               resampleAlg=gdal.GRA_NearestNeighbour,callback=Show_Progress)
    # 輸入投影,輸出投影,輸出格式,重采樣方法
    gdal.Warp(path_out, [image1, image2], options=options)
    # 輸出路徑,需要鑲嵌的數(shù)據(jù),參數(shù)配置
    ds = gdal.Open(path_out, gdal.GA_ReadOnly)
    ds.BuildOverviews(overviewlist=[2, 4, 8, 16])
    # 創(chuàng)建金字塔
    del image1, image2, ds

四、回調(diào)函數(shù)(沒啥用) 

        上一篇博文已經(jīng)介紹過了,就兩點:1.水字數(shù),2.記錄進度。省的看著代碼發(fā)呆。

def Show_Progress(percent, msg, tag):
    """
    :param percent: 進度,0~1
    :param msg:
    :param tag:
    :return:
    """
    if 25 <= percent*100 <= 26:
        print("進度:" + "%.2f" % (percent*100) + "%")
    if 50 <= percent*100 <= 51:
        print("進度:" + "%.2f" % (percent*100) + "%")
    if 75 <= percent*100 <= 76:
        print("進度:" + "%.2f" % (percent*100) + "%")

五、完整代碼

# -*- coding: utf-8 -*-
"""
@Time : 2023/6/25 16:28
@Auth : RS迷途小書童
@File :Raster Data Mosaic.py
@IDE :PyCharm
@Purpose:遙感影像、柵格數(shù)據(jù)的鑲嵌拼接(有明顯拼接線)
"""
from osgeo import gdal
def Get_data(filepath):
    ds = gdal.Open(filepath)  # 打開數(shù)據(jù)集dataset
    ds_width = ds.RasterXSize  # 獲取數(shù)據(jù)寬度
    ds_height = ds.RasterYSize  # 獲取數(shù)據(jù)高度
    ds_bands = ds.RasterCount  # 獲取波段數(shù)
    ds_geo = ds.GetGeoTransform()  # 獲取仿射地理變換參數(shù)
    ds_prj = ds.GetProjection()  # 獲取投影信息
    print("影像的寬度為:" + str(ds_width))
    print("影像的高度為:" + str(ds_height))
    print("仿射地理變換參數(shù)為:" + str(ds_geo))
    print("投影坐標(biāo)系為:" + str(ds_prj))
    # data = ds.ReadAsArray(0, 0, ds_width, ds_height)  # 以數(shù)組的形式讀取整個數(shù)據(jù)集
def Show_Progress(percent, msg, tag):
    """
    :param percent: 進度,0~1
    :param msg:
    :param tag:
    :return:
    """
    if 25 <= percent*100 <= 26:
        print("進度:" + "%.2f" % (percent*100) + "%")
    if 50 <= percent*100 <= 51:
        print("進度:" + "%.2f" % (percent*100) + "%")
    if 75 <= percent*100 <= 76:
        print("進度:" + "%.2f" % (percent*100) + "%")
def Mosaic_GDAL(path_image1, path_image2, path_out):
    """
    :param path_image1: 需要鑲嵌的影像
    :param path_image2: 需要鑲嵌的影像
    :param path_out: 鑲嵌后輸出的影像路徑
    :return: None
    """
    image1 = gdal.Open(path_image1, gdal.GA_ReadOnly)  # 第一幅影像
    input_proj = image1.GetProjection()
    image2 = gdal.Open(path_image2, gdal.GA_ReadOnly)  # 第二幅影像
    options = gdal.WarpOptions(srcSRS=input_proj, dstSRS=input_proj, format='GTiff',
                               resampleAlg=gdal.GRA_NearestNeighbour, callback=Show_Progress)
    # 輸入投影,輸出投影,輸出格式,重采樣方法
    gdal.Warp(path_out, [image1, image2], options=options)
    # 輸出路徑,需要鑲嵌的數(shù)據(jù),參數(shù)配置
    ds = gdal.Open(path_out, gdal.GA_ReadOnly)
    ds.BuildOverviews(overviewlist=[2, 4, 8, 16])
    # 創(chuàng)建金字塔
    del image1, image2, ds
if __name__ == "__main__":
    path1 = "B:/proj_clip"
    path2 = "B:/2_proj_clip"
    path_output = "warp1.tif"
    print("開始拼接......")
    Mosaic_GDAL(path1, path2, path_output)
    print("拼接完成......")

        這里有一點需要注意的就是,用這個方法進行鑲嵌拼接操作時,影像有一條明顯的拼接線,不知道是不是我數(shù)據(jù)的問題。你們可以自己嘗試一下。只要修改主函數(shù)中的路徑即可。

           如果大家在學(xué)習(xí)Python或者RS時有什么問題,可以隨時留言交流!

到此這篇關(guān)于Python基于GDAL鑲嵌拼接遙感影像的文章就介紹到這了,更多相關(guān)Python遙感影像鑲嵌內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論