卷积核(滤波器)及其特征映射
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200221154431597.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NTYwODk0,size_16,color_FFFFFF,t_70)
github地址
代码github地址
代码
import torch
import torch.nn as nn
import matplotlib.pyplot as plt
import cv2
import numpy as np
img = cv2.imread('picture.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img0 = np.array(img).reshape(img.shape[0], img.shape[1], 1)
img = np.transpose(img0, (2, 0, 1))
img = img[np.newaxis, :]
inp = torch.Tensor(img)
conv1 = nn.Conv2d(1, 1, (3, 3))
conv2 = nn.Conv2d(1, 1, (3, 3))
conv3 = nn.Conv2d(1, 1, (3, 3))
w1 = torch.Tensor(np.array([[1/16, 1/8, 1/16], [1/8, 1/4, 1/8], [1/16, 1/8, 1/16]]).reshape(1, 1, 3, 3))
w2 = torch.Tensor(np.array([[0, 1, 0], [1, -4, 1], [0, 1, 0]]).reshape(1, 1, 3, 3))
w3 = torch.Tensor(np.array([[0, 1, 1], [-1, 0, 1], [-1, -1, 0]]).reshape(1, 1, 3, 3))
conv1.weight = nn.Parameter(w1)
conv2.weight = nn.Parameter(w2)
conv3.weight = nn.Parameter(w3)
out1 = conv1(inp).detach().numpy()
out2 = conv2(inp).detach().numpy()
out3 = conv3(inp).detach().numpy()
out1 = np.transpose(out1, (0, 2, 3, 1))
out2 = np.transpose(out2, (0, 2, 3, 1))
out3 = np.transpose(out3, (0, 2, 3, 1))
plt.figure(figsize=(10, 5))
plt.subplot(141)
plt.imshow(img0.squeeze(), cmap="gray")
plt.subplot(142)
plt.imshow(out1.squeeze(), cmap='gray')
plt.subplot(143)
plt.imshow(out2.squeeze(), cmap='gray')
plt.subplot(144)
plt.imshow(out3.squeeze(), cmap='gray')
plt.show()
效果
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200221154556198.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NTYwODk0,size_16,color_FFFFFF,t_70)