随笔记~
内容来自吴恩达老师的Coursera课程 Convolutional Neural Networks。
参考代码:
import torch
import torch.nn.functional as F
from PIL import Image
from torchvision import tranforms
tfs = [transforms.ToTensor(), transforms.ToPILImage()]
src = Image.open(r'C:\Users\ykx\Desktop\mess\1\cat.jpg').convert('L')
src_tensor = tfs[0](src).unsqueeze(0)
kernel = torch.tensor([[1, 1, 0], [0, 0, 0], [-1, -1, -1]]).float()
kernel = (kernel.unsqueeze(0)).unsqueeze(0)
dst_tensor = F.conv2d(src_tensor, kernel)
dst = tfs[1](dst_tensor.squeeze(0))
dst.show()
原图 👇
垂直边缘检测 👇
水平边缘检测 👇
|