Python遥感影像拼接
生活随笔
收集整理的這篇文章主要介紹了
Python遥感影像拼接
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
? ? ?對于柵格影像,我們一般可以采用ENVI或ArcGIS平臺進行拼接,也可以通過GEE或PIE Engine云平臺進行處理。如果我們想利用人工神經網絡進行操作,就需要云平臺中的數(shù)據(jù)導出到本地。對于大尺度的遙感影像而言,就會存在每一景影像大小受限的問題,就會分成許多景影像,如下圖所示:
? ? ? ? 因此,在對其進行操作之前,我們需要對其進行拼接。接下來我們就介紹如何利用Python進行柵格影像的拼接。
? ? ? ? 在Python中有兩個強大的模塊,一個是raster,一個是gdal,二者都可以對柵格數(shù)據(jù)進行處理:
Raster?
import rasterio from rasterio.merge import merge from rasterio.plot import show import glob import os import matplotlib.pyplot as plt-
Importing required modules and find all?tif?files from the folder
# File and folder paths dirpath = r"E:\數(shù)據(jù)集\祁連山L8" out_fp = os.path.join(dirpath, "qilianshanL8.tif") tif_file = glob.glob(os.path.join(dirpath, "*.tif")) print(tif_file) -
create a list for the source raster datafiles?
src_files_to_mosaic = [] for tif_f in tif_file:src = rasterio.open(tif_f)src_files_to_mosaic.append(src) print('src_files_to_mosaic', src_files_to_mosaic) -
update the metadata with our new dimensions, transform and CRS and write to our computer
out_meta.update({"driver": "GTiff","height": mosaic.shape[1],"width": mosaic.shape[2],"transform": out_trans,"crs": "EPSG:4326"}) # Write the mosaic raster to disk with rasterio.open(out_fp, "w", **out_meta) as dest:dest.write(mosaic)
? ? 在處理完之后,我們也可以plt出拼接影像的效果圖?
mosaic, out_trans = merge(src_files_to_mosaic) # Plot the result show(mosaic, cmap='terrain')? ? 當然,需要注意的是,在拼接過程中,參與拼接的影像的投影參數(shù)應該保持一致,我們既可以手動輸入,也可以采用自動計算的方式?
"crs": "+proj=utm +zone=35 +ellps=GRS80 +units=m +no_defs " "crs": "EPSG:4326"? 但是,對于大尺度遙感影像而言,分幅影像的投影參數(shù)往往是不同的,因此我們先要將其投影參數(shù)統(tǒng)一化,然后再進行拼接操作。
for i in range(len(tif_file)):dstfilename = "EPGSG32649" + str(i) + ".tif" # 根據(jù)自己的需求設置文件名with rasterio.open(tif_file[i]) as src:transform, width, height = calculate_default_transform(src.crs, dst_crs, src.width, src.height, *src.bounds)kwargs = src.meta.copy()kwargs.update({"crs": dst_crs, "transform": transform, "width": width, "height": height, "compress":'lzw'})with rasterio.open(dstfilename, "w", **kwargs) as dst:for i in range(1, src.count + 1):reproject(source=rasterio.band(src, i),destination=rasterio.band(dst, i),src_transform=src.transform,src_crs=src.crs,dst_transform=transform,dst_crs=dst_crs,resampling=Resampling.nearest,)? ? ? ? 下面是官方關于Creating a raster mosaic的鏈接:??
Rasterhttps://autogis-site.readthedocs.io/en/latest/notebooks/Raster/raster-mosaic.html
GDALGDALhttps://www.neonscience.org/resources/learning-hub/tutorials/merge-lidar-geotiff-py
?
總結
以上是生活随笔為你收集整理的Python遥感影像拼接的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OSChina 周三乱弹 —— 哽住
- 下一篇: Python 生成一组随机数列表