载入一些必备包:
%matplotlib inline
plt.rcParams['font.sans-serif'] = ['SimHei'] # 指定默认字体, 为在 Matplotlib 中显示中文,设置特殊字体
plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号 '-' 显示为方块的问题
import os
from matplotlib import pyplot as plt
import pandas as pd
import numpy as np
import tifffile # pip install tifffile
root = r'E:\Data\Zip\GF6_WFV'
val_path = os.path.join(root, '验证集原始图像_8波段.tif')
train_ann_path = os.path.join(root, 'train_sha.txt') # 训练的标注信息需要将中文文件名改为英文
train_path = os.path.join(root, 'GF6_WFV_E127.9_N46.8_20180823_L1A1119838015.tif')
train_anns = pd.read_csv(train_ann_path, encoding='utf-16')
train_anns.index = train_anns.pop('FID')
imgs = tifffile.imread(train_path).transpose((1, 2, 0))
imgs.shape
(17810, 50362, 8)
plt.imsave(os.path.join(root, '2.jpg'), imgs[:1000,:1500,1])
x0, y0 = .0, 1e-07
raster = []
for fid in range(len(train_anns)):
a = train_anns.iloc[fid]
x_l = int(round(a.x))
y_l = -int(round(a.y))
r = a['半径']
raster.append([a['作物'], x_l, y_l, x_l + r, y_l + r])
def scale_percentile(matrix):
w, h, d = matrix.shape
matrix = np.reshape(matrix, [w * h, d]).astype(np.float64)
# Get 2nd and 98th percentile
mins = np.percentile(matrix, 1, axis=0) # 1% 分位数
maxs = np.percentile(matrix, 99, axis=0) - mins
matrix = (matrix - mins[None, :]) / maxs[None, :]
matrix = np.reshape(matrix, [w, h, d])
matrix = matrix.clip(0, 1)
return matrix
fig, axes = plt.subplots(ncols=2, nrows=1, figsize=(16, 6))
p1 = plt.subplot(121)
i1 = p1.imshow(scale_percentile(imgs[:1000, :1000,4:7]))
plt.colorbar(i1)
p2 = plt.subplot(122)
i2 = p2.imshow(imgs[:1000, :1000, 3])
plt.colorbar(i2)
plt.show()
ob = raster[0]
ob, imgs.dtype
(['玉米', 12501, 3287, 12504, 3290], dtype('uint16'))
fig, axes = plt.subplots(ncols=2, nrows=1, figsize=(16, 6))
p1 = plt.subplot(121)
i1 = p1.imshow(scale_percentile(imgs[ob[1]:ob[3], ob[2]:ob[4],:3]))
plt.colorbar(i1)
plt.title(ob[0])
p2 = plt.subplot(122)
i2 = p2.imshow(imgs[ob[1]:ob[3], ob[2]:ob[4], 0])
plt.colorbar(i2)
plt.title(ob[0])
plt.show()
各个通道的图像:
fig, axes = plt.subplots(ncols=4, nrows=2, figsize=(14, 10))
for i in range(8):
p = plt.subplot(241+i)
p.imshow(imgs[100:1000, 100:1000, i])
plt.show()
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦