在Python中将高光谱.HDR文件转换为栅格数组以创建镜像

问题描述 投票:0回答:1

我正在尝试逐波段解析我的高光谱波段,以创建每个带宽切片的光栅文件。但是,每当我使用频谱命令 .read_band() 时,我都会收到错误“EOFError:read() 没有返回足够的字节”。总的来说,我正在尝试创建 .HDR 文件的镜像,我认为翻转光栅矩阵将是最直接的方法。

谢谢!

我尝试了以下代码:

import spectral.io.envi as envi
import os 
import numpy as np
import matplotlib.pyplot as plt
import rasterio
from rasterio.transform import Affine

os.environ['SPECTRAL_DATA']='C:/Users/Desktop/HyperspectralData'
import spectral as sp
hdr_file_path = 'C:/Users/Desktop/HyperspectralData'
hdr_data = envi.open(hdr_file_path, image="raw_rd_rf.hdr")
wvl = hdr_data.bands.centers
rows, cols, bands = hdr_data.nrows, hdr_data.ncols, hdr_data.nbands
meta = hdr_data.metadata

for z in range(bands):
   band_img = hdr_data.read_band(z)
  

我希望得到一个栅格数组,其中每个元素都为我的波段中的像素进行编码。

python raster spectral
1个回答
0
投票

下面我展示了如何使用 Rasterio 包以及 Spectrum 包读取 ENVI 兼容文件。它们以相反的方式加载数据: rasterio:创建 [z,y,x] 形式的数组,其中 z 是光谱带。 Spectrum:创建一个 [y,x,z] 形式的数组。显然,标记为 x 或 y 的内容是任意的。我发现使用 Rasterio 数组更容易达到我的目的。

披露:我确实使用了人工智能工具来帮助解决这个问题,但我必须重写代码才能使其工作,而且我已经使用它几天了。

import rasterio
import matplotlib.pyplot as plt
import numpy as np
import spectral

# ENVI compatible files
file = '/Users/kjim/data/a.hsi'
header_file = '/Users/kjim/data/a.hdr'

# Open the ENVI image using rasterio
with rasterio.open(file) as src:
    # Read the hyperspectral data into a NumPy array
    hyperspectral_data = src.read()

    print('Shape of hyperspectral data:', hyperspectral_data.shape)
    print('Number of bands:', src.count)

高光谱数据形状:(150, 100, 682) 乐队数量:150

# Load the hdr file info using spectral
img = spectral.io.envi.open(header_file, file)

# Access the wavelengths associated with each band
wavelengths = img.bands.centers

# Display size and order of info and wavelengths in the .hdr file
print('Shape of hyperspectral data:', img.shape)
print('Number of bands:', img.shape[2])
print('Wavelengths:', wavelengths)

高光谱数据形状:(100, 682, 150) 频段数量:150 波长:[399.4、403.4、407.4、...

# Display one band (band 3)
plt.imshow(hyperspectral_data[3,:,:])

# flip one band image left-right (this is one kind of a mirror image):
mirror_hypserspectral_band = np.fliplr(hyperspectral_data[3,:,:])
plt.imshow(mirror_hyperspectral_band)

这是我从 Python 得到的:沿垂直轴镜像图像的最有效方法

© www.soinside.com 2019 - 2024. All rights reserved.