every blog every motto: The shortest answer is doing.
0. Preface
Comparison of different methods for reading high-resolution remote sensing images. The high-resolution image used in the experiment contains 4 bands: R, G, B, Nir (near infrared) bands
1. Text
The main method of comparison: read high score images (256, 256, 4), obtain each channel (256, 256) separately, take a unique value for each channel (np.unique()), and finally print the front of the unique value of each channel 10 for comparison.
1.1 Shared code
Import package
import cv2 as cv
import numpy as np
import gdal
from PIL import Image
path = r'./data/input/000001.tif'
Print the value on each channel of the three-dimensional array (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])
1.2 opencv
1.2.1 Code
def cv_load():
arr = cv.imread(path, cv.IMREAD_UNCHANGED)
print_result(arr, 'cv')
cv_load()
1.2.2 Results
1.3 PIL
1.3.1 Code
def Image_load():
img = Image.open(path)
arr = np.array(img)
print_result(arr, 'Image')
Image_load()
1.3.2 Results
1.4 gdal method one
1.4.1 Code
def gdal_load_img():
dataset = gdal.Open(path)
arr = dataset.ReadAsArray()
arr = arr.reshape((256, 256, 4))
print_result(arr, 'gdal')
gdal_load_img()
1.4.2 Results
1.5 gdal method two
1.5.1 Code
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()
1.5.2 Results
1.6 Summary
The overall result:
- PIL and gdal's second method have the same result
- The last channel of opencv is the same as the last channel of the two results mentioned above
- Why are the results different after reading the same picture? This point to be solved