every blog every motto: The shortest answer is doing.
不同方法读取高分遥感影像的比较,实验所用高分影像含有4个波段:R、G、B、Nir(近红外)波段
比较的主要方法:读取高分影像(256,256,4),分别获取每个通道(256,256),对每个通道取唯一值(np.unique()),最后,打印每个通道唯一值的前10个进行比较。
导入包
import cv2 as cv
import numpy as np
import gdal
from PIL import Image
path = r'./data/input/000001.tif'
打印三维数组(256,256,4)的每个通道上的值
def print_result(arr, name):
""" 对三维数组(256,256,4)的每个通道进行打印前10个唯一值
:param arr: 三维数组
:param name: 使用的方法名
:return:
"""
ch1 = arr[:, :, 0]
ch2 = arr[:, :, 1]
ch3 = arr[:, :, 2]
ch4 = arr[:, :, 3]
print(ch1.shape)
print('{} unqiue 1:'.format(name), np.unique(ch1)[:10])
print('{} unqiue 2:'.format(name), np.unique(ch2)[:10])
print('{} unqiue 3:'.format(name), np.unique(ch3)[:10])
print('{} unqiue 4:'.format(name), np.unique(ch4)[:10])
def cv_load():
arr = cv.imread(path, cv.IMREAD_UNCHANGED)
print_result(arr, 'cv')
cv_load()
def Image_load():
img = Image.open(path)
arr = np.array(img)
print_result(arr, 'Image')
Image_load()
def gdal_load_img():
dataset = gdal.Open(path)
arr = dataset.ReadAsArray()
arr = arr.reshape((256, 256, 4))
print_result(arr, 'gdal')
gdal_load_img()
def gdal_load2():
dataset = gdal.Open(path)
bands = dataset.RasterCount
for band in range(1, bands + 1):
# print(band)
# 读取波段
src_band = dataset.GetRasterBand(band)
# 波段转数组
band_arr = src_band.ReadAsArray()
if band == 1:
height = band_arr.shape[0]
width = band_arr.shape[1]
arr = np.zeros((height, width, bands), dtype=np.uint8)
arr[:, :, band - 1] = band_arr
# ------------------
# 查看结果
print_result(arr, 'gdal2')
gdal_load2()