import numpy as np import torch from torch import nn import torch.nn as nn from torch.autograd import Variable import torch.nn.functional as F from PIL import Image import matplotlib.pyplot as plt # % matplotlib inline im = Image.open('C:/Users/leo/Desktop/text/cat1.png').convert('L')#为灰度图像,每个像素用8个bit表示,0表示黑,255表示白,其他数字表示不同的灰度。 im = np.array(im, dtype='float32')#将灰度图片转化成矩阵 plt.imshow(im.astype('uint8'),cmap='gray')#cmap颜色设置 plt.rcParams['font.sans-serif']=['SimHei'] plt.rcParams['axes.unicode_minus'] = False#标题默认输出英文,如果输出中文,要对字体进行调整 plt.title('转化成灰度图片') plt.show() #将图片矩阵转化为pytorch tensor ,并适配卷积输入的要求 im = torch.from_numpy(im.reshape((1, 1,im.shape[0],im.shape[1])))#shape[0],矩阵行数,shape[1],矩阵列数 conv1 = nn.Conv2d(1,1,3,bias=False) #定义卷积,b为偏置 sobel_kernel = np.array([[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]], dtype='float32')#定义轮廓检测算子 sobel_kernel = sobel_kernel.reshape((1, 1, 3, 3))#适配卷积的输入输出 conv1.weight.data = torch.from_numpy(sobel_kernel)#给卷积的核赋值 edge1 = conv1(Variable(im)) edge1 = edge1.data.squeeze().numpy() plt.imshow(edge1, cmap='gray') plt.title('卷积后的图片') plt.show() pool1 = nn.MaxPool2d(2, 2) print('before max pool, image shape: {} x {}'.format(im.shape[2], im.shape[3]))#shape[0]输入通道数。shape[1]输出通道数,shape[2]高度,shape[3】宽度 small_im1 = pool1(Variable(im)) small_im1 = small_im1.data.squeeze().numpy() print('after max pool, image shape: {} x {} '.format(small_im1.shape[0],small_im1.shape[1])) plt.imshow(small_im1,cmap='gray') plt.show()
原图
操作代码后得到的图片