【pytorch卷1】=3=卷积提取提取图片轮廓

目录

1. 用mping载入图片

2. 用transforms.ToTensor图片转为张量,并(自动)归一化

3. 定义卷积核

4. 图片卷积函数处理


1. 用mping载入图片

import matplotlib.pyplot as plt
import torch
import matplotlib.image as maping
import torchvision.transforms as transforms

#import the underlying picture by apply maping
myimg = maping.imread('C:/Users/liyzc/Desktop/胖胖/和胖胖在宁波/IMG_20201007_122412.jpg')

#show the picture
plt.imshow(myimg)
plt.axis('off')
plt.show()
print(myimg.shape)

2. 用transforms.ToTensor图片转为张量,并(自动)归一化

pil2tensor = transforms.ToTensor()
rgb_image = pil2tensor(myimg)
print(rgb_image)
print(rgb_image[0][0])
print(rgb_image.shape)

3. 定义卷积核(方法参考

sobelfilter = torch.tensor([[-1., 0, 1.],
                       [-2., 0, 2.],
                       [-1., 0, 1.]]*3).reshape([1,3,3,3])

print(sobelfilter)
#note: *3 means tripling this ARRAY. compare:
# tensor([[-1., 0, 1.],
          [-2., 0, 2.],
          [-1., 0, 1.]])*3

4. 图片卷积函数处理

op = torch.nn.functional.conv2d(rgb_image.unsqueeze(0),soberfilter, stride=3,padding=(1,1))
#tensor转化为图片数据
ret = (op - op.min())/(op.max()-op.min())
ret = (ret.clamp(0.,1.)*255).int()#逆归一化搞回去,并转化乘整数
plt.imshow(ret.squeeze(), cmap='ocean_r')
plt.axis('off')
plt.show()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值