如何根据坐标切割卫星图像的一部分(格达尔)

2024-09-28 18:59:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我有7个频道的卫星图像(基本上我有7个.tif文件,每个波段一个)。我有一个.csv文件,上面有卫星拍摄区域内的兴趣点坐标。我想在每个坐标点周围切割图像的一小部分。我怎么能这么做

因为我现在还没有一个完整的工作代码,所以图像中那些小部分的大小并不重要。为了解释这个问题,我们假设它们是15x15像素。所以目前,我的最终目标是获得大量的15x15x7矢量,一个用于.csv文件中的每个坐标点。这就是我被绊倒的原因(“15x15x7”中的“7”是因为图像有7个通道)

为了给大家提供一些背景知识,以备不时之需:我稍后将使用这些向量在keras中训练CNN模型

这就是我目前所做的:(我使用的是jupyter笔记本,anaconda环境)

  • 导入了gdal、numpy、matplotlib、geopandas和其他库

  • 使用gdal打开.gif文件,将其转换为数组

  • 使用pandas打开.csv文件

  • 创建了一个名为“imagen”的形状(79317901,3)的numpy数组,它将承载卫星图像的7个波段(以数字的形式)。此时,我只需要知道数组“imagen”的哪些行和列对应于每个坐标点换句话说,我需要将每个坐标点转换成一对数字(行、列)。这就是我被绊倒的原因

之后,我认为“切割部分”将很容易

#I import libraries

from osgeo import gdal_array
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import geopandas
from geopandas import GeoDataFrame
from shapely.geometry import Point

#I access the satellite images (I just show one here to make it short)

b1 = r"E:\Imágenes Satelitales\2017\226_86\1\LC08_L1TP_226086_20170116_20170311_01_T1_sr_band1.tif"
band1 = gdal.Open(b1, gdal.GA_ReadOnly)

#I open the .csv file

file_svc = "C:\\Users\\Administrador\Desktop\DeepLearningInternship\Crop Yield Prediction\Crop Type Classification model - CNN\First\T28_Pringles4.csv"
df = pd.read_csv(file_svc)
print(df.head())

上面印着这样的东西:

   Lat1        Long1       CropingState
   -37.75737   -61.14537   Barbecho
   -37.78152   -61.15872   Verdeo invierno
   -37.78248   -61.17755   Barbecho
   -37.78018   -61.17357   Campo natural
   -37.78850   -61.18501   Campo natural
#I create the array "imagen" (I only show one channel here to make it short)

imagen = (np.zeros(7931*7901*7, dtype = np.float32)).reshape(7931,7901,7)
imagen[:,:,0] = band1.ReadAsArray().astype(np.float32)

#And then I can plot it:

plt.imshow(imagen[:,:,0], cmap = 'hot')
plt.plot()

它的情节是这样的:

https://github.com/jamesluc007/DeepLearningInternship/blob/master/Crop%20Yield%20Prediction/Crop%20Type%20Classification%20model%20-%20CNN/First/red_band.png

我想把这些(-37,-61)转换成(22301750)。但我还没弄明白是怎么回事。有什么线索吗


Tags: 文件csvthefrom图像importnumpyas