%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的`tifffile`库读取Tiff图像,并通过`scale_percentile`函数进行预处理,该函数利用2nd和98th百分位数进行归一化,确保像素值在0到1之间。然后使用`matplotlib`进行图像显示,适合于对Tiff格式的科学图像进行分析和可视化。
3601





