python - 如何使用 python 中的地面控制点对未引用的航拍图像进行地理配准

标签 python image gis gdal rasterio

我有一系列未引用的航拍图像,我想使用 python 对其进行地理引用。这些图像在空间上是相同的(它们实际上是从视频中提取的帧),我通过在 ArcMap 中手动对一帧进行地理配准来获得它们的地面控制点。我想将我获得的地面控制点应用于所有后续图像,结果为每个处理过的图像获得一个 geo-tiff 或 jpeg 文件以及相应的世界文件 (.jgw)。我知道使用 arcpy 可以做到这一点,但我无法访问 arcpy,如果可能的话,我真的很想使用免费的开源模块。

我的坐标系是 NZGD2000 (epsg 2193),下面是我希望应用于我的图像的控制点表:

176.412984, -310.977264, 1681255.524654, 6120217.357425

160.386905, -141.487145, 1681158.424227, 6120406.821253

433.204947, -310.547238, 1681556.948690, 6120335.658359

这是一个示例图片:https://imgur.com/a/9ThHtOz

我已经阅读了很多关于 GDAL 和 rasterio 的信息,但我对它们没有任何经验,并且无法根据我的特定情况调整我发现的代码。

栅格尝试:

import cv2
from rasterio.warp import reproject
from rasterio.control import GroundControlPoint
from fiona.crs import from_epsg

img = cv2.imread("Example_image.jpg")

# Creating ground control points (not sure if I got the order of variables right):
points = [(GroundControlPoint(176.412984, -310.977264, 1681255.524654, 6120217.357425)),
          (GroundControlPoint(160.386905, -141.487145, 1681158.424227, 6120406.821253)),
          (GroundControlPoint(433.204947, -310.547238, 1681556.948690, 6120335.658359))]

# The function requires a parameter "destination", but I'm not sure what to put there.
#   I'm guessing this may not be the right function to use
reproject(img, destination, src_transform=None, gcps=points, src_crs=from_epsg(2193),
                        src_nodata=None, dst_transform=None, dst_crs=from_epsg(2193), dst_nodata=None,
                        src_alpha=0, dst_alpha=0, init_dest_nodata=True, warp_mem_limit=0)

GDAL 尝试:

from osgeo import gdal 
import osr

inputImage = "Example_image.jpg"
outputImage = "image_gdal.jpg"

dataset = gdal.Open(inputImage) 
I = dataset.ReadAsArray(0,0,dataset.RasterXSize,dataset.RasterYSize)

outdataset = gdal.GetDriverByName('GTiff') 
output_SRS = osr.SpatialReference() 
output_SRS.ImportFromEPSG(2193) 
outdataset = outdataset.Create(outputImage,dataset.RasterXSize,dataset.RasterYSize,I.shape[0]) 
for nb_band in range(I.shape[0]):
    outdataset.GetRasterBand(nb_band+1).WriteArray(I[nb_band,:,:])

# Creating ground control points (not sure if I got the order of variables right):
gcp_list = [] 
gcp_list.append(gdal.GCP(176.412984, -310.977264, 1681255.524654, 6120217.357425))
gcp_list.append(gdal.GCP(160.386905, -141.487145, 1681158.424227, 6120406.821253))
gcp_list.append(gdal.GCP(433.204947, -310.547238, 1681556.948690, 6120335.658359))

outdataset.SetProjection(srs.ExportToWkt()) 
wkt = outdataset.GetProjection() 
outdataset.SetGCPs(gcp_list,wkt)

outdataset = None

我不太清楚如何让上面的代码工作,我真的很感激任何帮助。

最佳答案

我最终阅读了“使用 Python 进行地理处理”一书,并最终找到了适合我的解决方案。这是我针对我的问题改编的代码:

import shutil
from osgeo import gdal, osr

orig_fn = 'image.tif'
output_fn = 'output.tif'

# Create a copy of the original file and save it as the output filename:
shutil.copy(orig_fn, output_fn)
# Open the output file for writing for writing:
ds = gdal.Open(output_fn, gdal.GA_Update)
# Set spatial reference:
sr = osr.SpatialReference()
sr.ImportFromEPSG(2193) #2193 refers to the NZTM2000, but can use any desired projection

# Enter the GCPs
#   Format: [map x-coordinate(longitude)], [map y-coordinate (latitude)], [elevation],
#   [image column index(x)], [image row index (y)]
gcps = [gdal.GCP(1681255.524654, 6120217.357425, 0, 176.412984, 310.977264),
gdal.GCP(1681158.424227, 6120406.821253, 0, 160.386905, 141.487145),
gdal.GCP(1681556.948690, 6120335.658359, 0, 433.204947, 310.547238)]

# Apply the GCPs to the open output file:
ds.SetGCPs(gcps, sr.ExportToWkt())

# Close the output file in order to be able to work with it in other programs:
ds = None

关于python - 如何使用 python 中的地面控制点对未引用的航拍图像进行地理配准,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55681995/

相关文章:

python - Keras 深度学习简介 : 'X_train' is not defined?

c# - 如何使用 MEF 从 dll 文件中提取图片

javascript - 使用按钮刷新任意图像

python - 使用 Python 的 matplotlib/basemap 的颜色状态

r - 从 rstudio 中的 shapefile 绘制图层的难度

python - 如何退出Python解释器?

python - Python处理负整数的问题

python zeroconf 显示 IPv4 地址

javascript - 以一定的精度同时运行图像转换和动画?

google-cloud-platform - Multipolygon 无法在 Google BigQuery GIS 中上传