如何通过 python 读取 16 位或 32 位图像并将正确的图像值作为数组获取?
我尝试使用 PIL + Numpy 和 GDAL + Numpy 等读取 SRTM(航天飞机雷达地形任务)。我只需要一小块,所以我使用 ArcGIS 来获取我需要的 tif 文件范围。
但是,通过上述测试,数组中的值是不正确的。比如我在CA用了一个SRTM,它的高程范围是136到737,但是当我用numpy获取最大值和最小值时,数组值范围是-3.40到737,而且出现了很多负值。我能做些什么?我会犯一些错误吗?
我使用的代码如下使用 ipython:
import Image
import numpy as num
im=Image.open('srtm.tif')
imarray=num.array(im)
imarray
array([[ -3.40282347e+38, -3.40282347e+38, 4.00000000e+02, ...,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[ -3.40282347e+38, -3.40282347e+38, 4.00000000e+02, ...,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[ -3.40282347e+38, -3.40282347e+38, 4.00000000e+02, ...,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
...,
[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00]], dtype=float32)
import gdal
tif = gdal.Open('srtm.tif')
arr = tif.ReadAsArray()
arr
array([[ -3.40282347e+38, -3.40282347e+38, 4.00000000e+02, ...,
-3.40282347e+38, -3.40282347e+38, -3.40282347e+38],
[ -3.40282347e+38, -3.40282347e+38, 4.00000000e+02, ...,
6.11000000e+02, 6.17000000e+02, 6.17000000e+02],
[ -3.40282347e+38, -3.40282347e+38, 4.00000000e+02, ...,
6.11000000e+02, 6.17000000e+02, 6.17000000e+02],
...,
[ 1.50000000e+02, 1.50000000e+02, 1.48000000e+02, ...,
1.73000000e+02, 1.73000000e+02, 1.73000000e+02],
[ 1.49000000e+02, 1.49000000e+02, 1.48000000e+02, ...,
1.73000000e+02, 1.73000000e+02, 1.73000000e+02],
[ 1.49000000e+02, 1.49000000e+02, 1.48000000e+02, ...,
1.73000000e+02, 1.73000000e+02, 1.73000000e+02]], dtype=float32)
num.max(arr)
737.0
num.min(arr)
-3.4028235e+38