def double_silts(length=20, distance=4, object=2):
m = np.zeros(shape=(28, 28))
print(m)
length = length # 缝的长度
distance = distance # 中间的间距
object = object # 物体的宽度
h = (28 - length) // 2 # 4
w = (28 - distance - object * 2) // 2 # 10
b = m.copy()
c = m.copy()
for i in range(28):
for j in range(28):
if j in range(w, w + 2) or j in range(26-w, 28-w):
c[i][j] = 10
else:
c[i][j] = 0
for i in range(28):
if i in range(0, h) or i in range(h + 20, 28):
b[i] = 0
else:
b[i] = 1
d = c * b
return d #生成一个28*28的nparray
images = double_silts()
images = torch.tensor(images,dtype=torch.float32).unsqueeze(0).unsqueeze(0)
#生成一个tensor.size(1,1,28,28)
images = double_silts()
images = torch.tensor(images,dtype=torch.float32).unsqueeze(0).unsqueeze(0)
GBlur = MyGaussianBlur(radius=radius, sigema=sigema) # 声明高斯模糊类--自定义
temp = GBlur.template() # 得到滤波模版
blur1_imgs = GBlur.filter(images, temp) # 高斯模糊滤波,得到新的图片
blur1_imgs = torch.clamp(image, 0., 1.)
images2= blur1_imgs*c
output = model(images2)
output = output.detach().numpy()
def subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True,
subplot_kw=None, gridspec_kw=None, **fig_kw):
fig = figure(**fig_kw)
axs = fig.subplots(nrows=nrows, ncols=ncols, sharex=sharex, sharey=sharey,
squeeze=squeeze, subplot_kw=subplot_kw,
gridspec_kw=gridspec_kw)
return fig, axs
subplots的底层代码:
nrows, ncols行和列数-----默认1
sharex, sharey : True,false, row ,col
fig, axes = plt.subplots(nrows=3, ncols=1, sharex=True, sharey=True, figsize=(5, 8))
img = np.squeeze(images[0])
axes[0].imshow(img, cmap='gray')
axes[0].xaxis.set_ticks([])
axes[0].yaxis.set_ticks([])
img11 = np.squeeze(blur1_imgs[0])
axes[1].imshow(img11, cmap='gray')
axes[1].xaxis.set_ticks([])
axes[1].yaxis.set_ticks([])
img22 = np.squeeze(output[i])
axes[2].imshow(img22 ,cmap='gray')
axes[1].xaxis.set_ticks([])
axes[2].yaxis.set_ticks([])
plt.show()
隐藏子图xy坐标
axes[0].xaxis.set_ticks([])
axes[0].yaxis.set_ticks([])
my_font = font_manager.FontProperties(fname="C:\\Windows\\Fonts\\msyh.ttc", size='x-large')
fig, axes = plt.subplots(nrows=3, ncols=1, sharex=True, sharey=True, figsize=(5, 8))
img = np.squeeze(images[0])
axes[0].imshow(img, cmap='gray')
axes[0].xaxis.set_ticks([])
axes[0].yaxis.set_ticks([])
axes[0].set_xlabel('原 图',fontproperties=my_font,rotation=0) #设置x的label
img11 = np.squeeze(blur1_imgs[0])
axes[1].imshow(img11, cmap='gray')
axes[1].xaxis.set_ticks([])
axes[1].yaxis.set_ticks([])
axes[1].set_xlabel('模 糊 图',fontproperties=my_font,rotation=0)
img22 = np.squeeze(output[0])
axes[2].imshow(img22 ,cmap='gray')
axes[2].xaxis.set_ticks([])
axes[2].yaxis.set_ticks([])
axes[2].set_xlabel('重 建 图',fontproperties=my_font,rotation=0)
plt.show()
?
|