import numpy as np
import matplotlib.pyplot as plt
'''
显示单幅图像
'''
def showSingleImg(img, path, name=None, isgray=False):
plt.figure('') # 图像窗口名称 figsize=(,) 设置窗口大小
# 单幅图像
plt.title('')
if isgray:
plt.imshow(img, cmap='gray') # 灰度图
else:
plt.imshow(img) # 彩图
plt.axis('off')
# 去除白边保存图片
# 获得当前的figure对象 Get Current Figure
if name is not None:
savePath = path + name
fig = plt.gcf()
width, height = img.shape[0], img.shape[1]
fig.set_size_inches(width / 96.0, height / 96.0) # 图片的dpi:96.0
plt.subplots_adjust(top=1, bottom=0, left=0, right=1, hspace=0, wspace=0)
plt.margins(0, 0)
plt.savefig(savePath + '.tif', dpi=96.0, pad_inches=0.0)
plt.show()
def get_row_col(num_pic):
squr = num_pic ** 0.5
row = round(squr)
col = row + 1 if squr - row > 0 else row
return row, col
'''
显示多张特征图 多通道
img:shape = [batch, height, width, channels] type:numpy.ndarray
'''
def showMultipleFeatureMap(img, name=None):
# 从数组的形状中删除单维条目,即把shape中为0的维度去掉
img = np.squeeze(img, axis=0)
num_pic = img.shape[2]
row, col = get_row_col(num_pic)
plt.figure("特征提取:") # 图像窗口名称 figsize=(,) 设置窗口大小
plt.suptitle('')
for i in range(0, num_pic):
plt.subplot(row, col, i)
plt.title('')
plt.imshow(img[:, :, i], cmap='gray')
plt.axis('off')
if name is not None:
savePath = './' + name + '.png'
plt.savefig(savePath)
plt.show()
'''
显示多幅图像 channels = 1 或 3
img:shape = [batch, height, width, channels] type:numpy.ndarray
'''
def showMultipleImg(img, name=None):
num_pic = img.shape[0]
row, col = get_row_col(num_pic)
plt.figure("多幅图像:") # 图像窗口名称 figsize=(,) 设置窗口大小
plt.suptitle('')
for i in range(0, num_pic):
plt.subplot(row, col, i)
plt.title('')
if img.shape[3] == 1:
plt.imshow(img[i, :, :, :], cmap='gray') # 灰度图
else:
plt.imshow(img) # 彩图
plt.axis('off')
if name is not None:
savePath = './' + name + '.png'
plt.savefig(savePath)
plt.show()
if __name__ == '__main__':
path = './data/'
name = '0000_cluster'
img = np.load(path + name + '.npy')
print(img)
showSingleImg(img, path, name, isgray=False)