我有一个 7 通道的卫星图像(基本上我有七个 .tif 文件,每个波段一个)。我有一个 .csv 文件,其中包含卫星拍摄区域内的兴趣点坐标。我想在每个坐标点的周围切割一小部分图像。我怎么能那样做?
由于我现在没有完整的工作代码,因此这些小部分图像的大小实际上并不重要。为了解释这个问题,假设我希望它们是 15x15 像素。所以目前,我的最终目标是获取大量 15x15x7 向量,一个对应于 .csv 文件中的每个坐标点。这就是我所坚持的。(“15x15x7”中的“7”是因为图像有7个通道)
只是为了提供一些相关的背景信息:稍后我将使用这些向量在 keras 中训练 CNN 模型。
这是我到目前为止所做的:(我使用的是jupyter notebook,anaconda环境)
导入 gdal、numpy、matplotlib、geopandas 等库。
使用 gdal 打开 .gif 文件,将它们转换为数组
使用熊猫打开 .csv 文件。
创建了一个名为“imagen”的形状为 (7931, 7901, 3) 的 numpy 数组,它将承载卫星图像的 7 个波段(以数字的形式)。此时我只需要知道数组“imagen”的哪些行和列对应于每个坐标点。换句话说,我需要将每个坐标点转换为一对数字 (row,colum)。这就是我所坚持的。
在那之后,我认为“切割部分”会很容易。
#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()
哪个情节是这样的:
我想将那些 (-37,-61) 转换为 (2230,1750) 之类的东西。但我还没想好怎么弄。有什么线索吗?