python读取tif文件并可视化

%matplotlib inline
import tifffile as tiff
import numpy as np 
import matplotlib.pyplot as plt
tiff_path = "F:\\image\\000001.tif"

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)
    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

img = tiff.imread(tiff_path)
plt.imshow(scale_percentile(img[:,:,:3]))

 输出结果

python读取tif文件并可视化_第1张图片

原始图像

 

 

参考:

【实验代码】可视化tiff数据 + 提交格式

你可能感兴趣的:(python)